Is it possible to access a variable declared in wdio.conf?

时间秒杀一切 提交于 2019-12-11 14:42:54

问题


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)
});

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


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.


回答1:


yes, in the file you posted here, add

module.exports = featureFiles

in the other file where you'd like to use featureFiles you can do something like

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



回答2:


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

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
    }
}

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

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



来源:https://stackoverflow.com/questions/58463916/is-it-possible-to-access-a-variable-declared-in-wdio-conf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!