How to set environment variable to run webdriverIO script

5.3k views Asked by At

I am running my webdriverIO test cases using command npx wdio run .\wdio.conf.js --spec .\test\intel.test.js

Now I want to pass environment variable for the same like ENV=qa

If I run command like this ENV=qa npx wdio run .\wdio.conf.js --spec .\test\intel.test.js Getting error:

ENV=qa : The term 'ENV=qa' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

  • ENV=qa npx wdio run .\wdio.conf.js --spec .\test\intel.test.js

is there any way to set environment variable when we are using npx to run test cases?

3

There are 3 answers

1
Sowthanya On BEST ANSWER

you can pass as like in the below command for user,key and baseurl which are all the allowed keys in wdio.conf.js.

npx wdio run .\wdio.conf.js --spec .\test\intel.test.js --user=automation

if you want to use any other parameters (like ENV as you mentioned), pls go with dotenv.

0
Gencho Kerkeniakov On
0
Giuseppe Salvatore On

What I would do is set an environment variable in your console (that would work in CI as well depending on the tool your are using)

// In linux for example
export MY_VAR=foo

and in Webdriver.io simply access it with process.env like this

console.log(`MY_VAR value is: ${process.env.MY_VAR}`);

I get (last line)

...
[0-0]     alwaysMatch: { browserName: 'chrome', acceptInsecureCerts: true },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: { browserName: 'chrome', acceptInsecureCerts: true }
[0-0] }
[0-0] 2022-11-18T16:59:50.392Z INFO webdriver: COMMAND maximizeWindow()
[0-0] 2022-11-18T16:59:50.393Z INFO webdriver: [POST] http://localhost:9515/session/d70c54abaefa7549da6e3946f2b26ce8/window/maximize
[0-0] MY_VAR value is: foo

Quite handy and easy.