question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to persist data on Helpers

See original GitHub issue

What are you trying to achieve?

I’m trying to persist data from one step to another on my own helper.

What I did?

I create a helper that helps me with REST calls (not completed yet have just started working on it)

How my helper looks like

'use strict';
let Request = require('es6-request'); 

class REST extends Helper {

    constructor(config){
        super(config);
        
        this.results = {};
    }
    
  // add custom methods here
  // If you need to access other helpers
  // use: this.helpers['helperName']

  sendGet(url, params){
      return Request.get(url)
      .then((body) => {
          return body;
      });
  }
  
  getHttpResult(){
      console.log(this.results);
  }   
}

module.exports = REST;

The configuration for it have nothing special

{
  "tests": "./tests/*/rest_test.js",
  "timeout": 10000,
  "output": "./output",
  "helpers": {
    "REST": {
      "require": "./helpers/rest_helper.js"
    }
  },
  "include": {
    "I": "./steps_file.js",
    "loginPage": "./pages/loginPage.js"
  },
  "bootstrap": false,
  "mocha": {},
  "name": "codeceptjs-poc"
}

I have this on my test

Feature('REST http test');

Scenario('Testing helper for rEST testing', (I) => {
    I.sendGet("https://randomuser.me/api/", "whatever");
    I.getHttpResult();
});

I just run this using

# node_modules/codeceptjs/bin/codecept.js run 

The results of what I have on the console are

➜  codeceptjs-poc git:(master) ✗ node_modules/codeceptjs/bin/codecept.js run
CodeceptJS v0.4.11
Using test root "/Users/atrevino/Projects/codeceptjs-poc"

REST http test --
{}
 ✓ Testing helper for rEST testing in 440ms

  OK  | 1 passed   // 446ms

So I’m getting an empty object {}

As I said before, I just want to know how to persist data, in this situation it will be the GET response on this.results = {};. I’m very new to JS also codeceptjs and would like you guys can share your thoughts, suggestions or a way to do it.

Thanks a lot !

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
hubiducommented, Feb 15, 2017

You can do the following:

  1. Write your helper like so
let request = require('es6-request'); 

class RESTHelper extends Helper {
    doHttpGet (url){
        return request.get(url)
        .then((body) => {
            return body;
        });
    }
}

module.exports = RESTHelper;
  1. Then use it from your feature file like so
Feature('Call REST APIs')

Scenario('I can make a GET request', function* (I) {
    let body = yield I.doHttpGet('http://www.google.com')
    I.say(`Got body ${body} from google`)
})

If you really need to collect http responses you can do

  1. Helper
let request = require('es6-request'); 

class RESTHelper extends Helper {
    constructor (config) {
        super(config)
        this.responses = []
    }

    doHttpGet (url){
        return request.get(url)
        .then((body) => {
            this.responses.push(body)
            return body;
        });
    }
    
    clearResponses () {
        this.responses = []
    }

    getResponses () {
        return Promise.resolve(this.responses)
    }
}

module.exports = RESTHelper;
  1. Feature
Scenario('I can make multiple GET requests and collect responses', function* (I) {
    I.clearResponses()
    I.doHttpGet('http://www.google.com')
    I.doHttpGet('http://www.yahoo.com')
    I.doHttpGet('http://exploringjs.com/')

    let responses = yield I.getResponses()
    I.say(`Got ${responses.length} http responses`)
    I.say(`Response from exploringjs ${responses[2]}`)
})
1reaction
DavertMikcommented, Feb 15, 2017

I create a helper that helps me with REST calls (not completed yet have just started working on it)

Once you get it working, please send a Pull Request with it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question: How to persist data on Helpers · Issue #421 - GitHub
I'm trying to persist data from one step to another on my own helper. What I did? I create a helper that helps...
Read more >
Data Persistance and/or Data Store? - Scripting Helpers
You need to ask you only one main question which will do in most cases: ... Data Persistence is easier to use as...
Read more >
Meteor - How can I pass data between helpers and events for ...
Dependency; Template.tenantsBlock.tenantsList = function() { tenants = []; var property = $properties.findOne({userId: Meteor.userId(), propertyId: Session.get ...
Read more >
Persistent storage - web.dev
To request persistent storage for your site's data, call navigator.storage.persist() . It returns a Promise that resolves with a boolean, ...
Read more >
Client-side storage - Learn web development | MDN
The Web Storage API is very easy to use — you store simple name/value pairs of data (limited to strings, numbers, etc.) and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found