Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Cypress.io read the Windows environment variables?

I have set my environment variables in 'Cypress.env.json' file, while running the cypress test, it read the Cypress.env variables successfully. But to be more on the security aspect, rather than 'hard-cording' the values, my team asked me to keep this variable as separate 'parameters' which read from Windows 10 Environment variables. How do I achieve this ? It would be really helpful if someone could advise on this.

{
"QA_Server": "https://sometestingsite.com",
"username": "testQA",
"password": "Password1234!"
}
like image 431
soccerway Avatar asked Sep 20 '18 01:09

soccerway


People also ask

How do you call an environment variable in Cypress?

Using your Cypress config fileAny key/value you set in your Cypress configuration file ( cypress. json by default) under the env key will become an environment variable that you can access using Cypress. env in your tests.

How does Cypress manage the environment?

We can set environment by passing these arguments in the cypress run or cypress open command e.g. cypress open --env configFile=test. This command looks lengthy. Also, sometimes we need to pass more command line arguments along with configFile. We can create short and handy commands by configuring the package.

How do I access baseUrl in Cypress?

To get baseUrl value use Cypress. config(). baseUrl or Cypress. config('baseUrl') .


1 Answers

From cypress documentation here:

Any environment variable on your machine that starts with either CYPRESS_ or cypress_ will automatically be added and made available to you.

Conflicting values will override values from cypress.json and cypress.env.json files.

Cypress will strip off the CYPRESS_ when adding your environment variables.

Exporting cypress env variables from the command line:

export CYPRESS_HOST=laura.dev.local

export cypress_api_server=http://localhost:8888/api/v1/

If you're using Windows you can set env variables using set or setx commands.

And in your test files you can call this:

Cypress.env()             // {HOST: "laura.dev.local", api_server: "http://localhost:8888/api/v1"}
Cypress.env("HOST")       // "laura.dev.local"
Cypress.env("api_server") // "http://localhost:8888/api/v1/"
like image 71
Diogo Rocha Avatar answered Sep 22 '22 05:09

Diogo Rocha