javascript - cucumber JS : How to export/update global variable outside Given/When/Then step?

标签 javascript cucumberjs

我尝试更新/导出全局变量(firstString)以在“Then”步骤中使用和验证它。 如何正确导出?当我这样做时,第一个字符串是未定义的。 仅当我在步骤内导出/导入它时它才有效。如何全局更新它并在“Then”文件中使用它?

helpers.js:

let firstString;

给定的.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)
});

最佳答案

如果我对您想要执行的操作的理解是正确的,那么您希望跨步骤存储数据。 Todo,您将需要使用 Cucumber 为您提供的世界实例。您可以通过 this 关键字逐步访问世界实例。

所以你会做的是

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

});

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

有关世界实例的更多信息,请查看 https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/world.md

关于javascript - cucumber JS : How to export/update global variable outside Given/When/Then step?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58075190/

相关文章:

Protractor 测试已通过但未执行测试

jenkins - 如何在 Jenkins Pipeline 中使用 cucumberSendSlack

JavaScript 模块 - 语法错误 : Unexpected reserved word at exports. runInThisContext

javascript - CucumberJS 控制台输出给定、何时、然后在运行时测试步骤

javascript - Accordion 菜单第一张幻灯片默认无法关闭

javascript - 拒绝 promise 未定义

javascript - 土地无故消失

c# - 从代码隐藏添加文本到网页

javascript - 如何过滤表格?

javascript - 如何验证元素是否存在于 CucumberJS 和 Protractor 中?