【问题标题】:Is it possible to access a variable declared in wdio.conf?是否可以访问在 wdio.conf 中声明的变量?
【发布时间】:2019-10-19 12:57:29
【问题描述】:

在 wdio.conf onPrepare 中,我将所有功能文件存储在一个数组中。

let listOfFiles = fs.readdirSync(process.cwd() + '/features');
var featureFiles = [];

listOfFiles.map((file) => {
    featureFiles.push(file)
});

是否可以在另一个文件中使用featureFiles 数组?


我想在执行期间生成功能文件列表并将其分配给变量“featureFiles”(已声明但没有任何值开始)。 从我目前看到的情况来看,在 wdio.conf 中是不可能做到这一点的,因为你总是会在开头得到为你的变量声明的值。在我的例子中是一个空数组。

【问题讨论】:

标签: javascript node.js selenium-webdriver webdriver webdriver-io


【解决方案1】:

因此,如果您想在整个测试脚本中使用 featureFiles,那么我们遵循的选项之一就是使用全局对象。

在您的 wdio.conf 文件中,尝试以下操作:

let featureFiles = [];
...
export.config = {
    ... //some line of code

    //OnPrepare hook
    onPrepare(){
        let listOfFiles = fs.readdirSync(process.cwd() + '/features');

        listOfFiles.map((file) => {
            featureFiles.push(file);
        });
    }

    //before session hook
    beforeSession(){
        global.featureFiles = featureFiles; //assigning the value of featureFiles to a global variable
    }
}

完成上述操作后。变量featureFiles 将在所有测试文件中可用。

注意:vscode intellisense 可能无法识别该变量,但您仍然可以使用它。

【讨论】:

  • 我已经在 wdio.conf 中完成了上述操作,然后在 randomNumber.js 中我正在尝试 console.log(global.featureFiles),但是我在 featureFiles 上收到错误,因为“featureFiles 没有存在于“全球”类型上”。不确定如何从测试文件中调用 featureFiles?
  • 嗨@Daredevi1 试试console.log(featureFiles) 忽略global
  • 只打印一个空行
  • 哦 .. featureFile 在分配给全局之前是否具有价值?你能通过传递一个硬编码的值来试试吗
  • 如果我将一个值硬编码到 featureFile 然后它会打印出硬编码的值,否则它是空白的。
【解决方案2】:

是的,在您在此处发布的文件中,添加:

module.exports = featureFiles

在您想使用featureFiles 的其他文件中,您可以执行以下操作:

const featureFiles = require('path/to/your/first/file`);

【讨论】:

  • 当我在 wdio.conf 中添加 module.exports = featureFiles 时,运行时出现以下错误:2019-10-21T08:05:55.056Z INFO @wdio/cli:Launcher: Run onPrepare hook 2019-10-21T08:05:55.057Z ERROR @wdio/cli:Launcher: Missing capabilities, exiting with failure 2019-10-21T08:05:55.058Z INFO @wdio/cli:Launcher: Run onComplete hook
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多