最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cucumber JS: How to exportupdate global variable outside GivenWhenThen step? - Stack Overflow

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论