admin管理员组

文章数量:1387317

I try to update/export global variable (firstString) to use and validate it in 'Then' step. How do I export it correctly? When I'm doing it this way, the firstString is undefined. It works only when I export/import it inside steps. How can I update it globally and use it in 'Then' file?

helpers.js:

let firstString;

given.js:

let { firstString } = require('./helpers')

Given('I have first {string}', function (stringValue) {
    return stringRequest(stringValue).then(response => {
        firstString = response
    });
});

module.exports = { firstString }

then.js:

firstString = require('./helpers').firstString
Then('blablabla {string}', function (stringType) {
    console.log(firstString)
});

I try to update/export global variable (firstString) to use and validate it in 'Then' step. How do I export it correctly? When I'm doing it this way, the firstString is undefined. It works only when I export/import it inside steps. How can I update it globally and use it in 'Then' file?

helpers.js:

let firstString;

given.js:

let { firstString } = require('./helpers')

Given('I have first {string}', function (stringValue) {
    return stringRequest(stringValue).then(response => {
        firstString = response
    });
});

module.exports = { firstString }

then.js:

firstString = require('./helpers').firstString
Then('blablabla {string}', function (stringType) {
    console.log(firstString)
});
Share Improve this question asked Sep 24, 2019 at 7:30 TgralakTgralak 1473 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

If I am correct in understanding what you want to do you are wanting to store data across steps. Todo that you will want to use the world instance that cucumber provides for you. You can access the world instance in steps via the this keyword.

So what you would do is

Given('I have first {string}', function (stringValue) {
return stringRequest(stringValue).then(response => {
    this.firstString = response
});

});

Then('blablabla {string}', function (stringType) {
    console.log(this.firstString)
});

For more information on the world instance check out https://github./cucumber/cucumber-js/blob/master/docs/support_files/world.md

本文标签: javascriptCucumber JS How to exportupdate global variable outside GivenWhenThen stepStack Overflow