0

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.

Daredevi1
  • 87
  • 1
  • 13
  • Possible duplicate of [WebdriverIO: How to read baseURL value from wdio.conf.js. inside step definition file](https://stackoverflow.com/questions/44571364/webdriverio-how-to-read-baseurl-value-from-wdio-conf-js-inside-step-definition) – iamdanchiv Oct 22 '19 at 10:04

2 Answers2

0

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`);
Aulis Ronkainen
  • 152
  • 2
  • 2
  • 9
schu34
  • 821
  • 8
  • 21
  • When I put a `module.exports = featureFiles` in the wdio.conf I get the following error on run: ```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``` – Daredevi1 Oct 21 '19 at 08:07
0

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.