是否可以访问在wdio.conf中声明的变量? [英] Is it possible to access a variable declared in wdio.conf?

查看:86
本文介绍了是否可以访问在wdio.conf中声明的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the wdio.conf onPrepare I am storing all my feature file's in a array.

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

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

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

Is it possible to use the featureFiles array in another file?

我想在执行过程中生成功能文件列表,并将其分配给变量"featureFiles"(已声明但没有任何开头的值). 从目前为止我所看到的,不可能在wdio.conf中执行此操作,因为您总是会在开始时获得为变量声明的值.在我的情况下,这是一个空数组.

I want to generate an list of feature files during execution and assign it to the variable "featureFiles" (which is declared but has no value to begin with). From what I've seen so far it's not possible to do this in wdio.conf as you will always get the value declared for your variable at the beginning.. which in my case is an empty array.

推荐答案

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

So if you want to use featureFiles throughout your test script, then one of the options that we follow is using global object.

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

In your wdio.conf file, try the following:

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将在所有测试文件上可用.

Once the above is done. The variable featureFiles would be available on all the test files.

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

NOTE: vscode intellisense might not recognize the variable but you can still use it.

这篇关于是否可以访问在wdio.conf中声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆