问题
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