Reputation: 185
In my Cypress setup I have the following values (plus others) in my cypress.json file:
{
"PREPROD": {
"url": "https://preprod.com",
"username": "admin",
"password": "admin"
},
"PROD": {
"url": "https://prod.com",
"username": "admin",
"password": "admin"
}
}
I have my test file located in cypress/integration/test.js
Based on what environment I want to run my test, I'd like to use the values relevant to that environment in my test file
I've tried running like this:
npx cypress run --env environment="PREPROD" --spec "cypress/integration/test.js"
This returns an error saying that environment is not defined.
In my test file I have the following to use the variable from command line:
Cypress.config().environment.username
How do I go about using the variables that I define in command line? I've tried reading the documentation here but its not working for me: Cypress Documentation
Upvotes: 1
Views: 4543
Reputation: 7615
According to the documentation, you can access the environment
variable like this:
Cypress.env('environment')
If you want to get the options depending on this you could do as follow:
const CypressConfiguration = require('cypress.json')
const environment = Cypress.env('environment') || 'PREPROD'
const options = CypressConfiguration[environment]
One other approach would be to use the config-file
option:
cypress.prod.json
{
"url": "https://prod.com",
"username": "admin",
"password": "admin"
}
cypress.preprod.json
{
"url": "https://preprod.com",
"username": "admin",
"password": "admin"
}
Then:
npx cypress run --config-file cypress.prod.json --spec "cypress/integration/test.js"
Upvotes: 2