- how to control Cypress parameters
- how to pass environment variables to tests
+++
baseUrl
env
reporter
video
- and many, many more
+++
Cypress options can be set via:
cypress.json
- command line arguments
- environment variables
- in plugin code
- at run-time
+++
Where is the configuration documentation?
Note: You should find docs at https://on.cypress.io/configuration
+++
Open cypress.json
and check which options are set in this project.
{
"viewportWidth": 400,
"viewportHeight": 800,
"ignoreTestFiles": "answer.js",
"baseUrl": "http://localhost:3000"
}
+++
Tip: if you have a lot of options to overwrite, use --config-file <...>
argument to replace cypress.json
.
+++
You can have IntelliSense in cypress.json
in a modern editor, like VSCode.
+++
In the user settings, global or workspace set
{
"json.schemas": [
{
"fileMatch": ["cypress.json"],
"url": "https://on.cypress.io/cypress.schema.json"
}
]
}
Read: https://glebbahmutov.com/blog/json-schema-for-the-win/
+++
Add $schema
property to cypress.json
{
"viewportWidth": 600,
"viewportHeight": 800,
"ignoreTestFiles": "answer.js",
"baseUrl": "http://localhost:3000",
"$schema": "https://on.cypress.io/cypress.schema.json"
}
Read: https://glebbahmutov.com/blog/json-schema-for-the-win/
+++
You can override default and cypress.json
settings using --config
flag
npx cypress open \
--config baseUrl=http://todomvc.com/examples/dojo/,defaultCommandTimeout=10000
Note:
Try running cypress/integration/02-adding-items/demo.js
spec.
Commonly used with cypress run
command (specific spec, longer timeouts)
+++
Warning --
to add CLI arguments.
{
"scripts": {
"cy:open": "cypress open",
"cy:run": "cypress run"
}
}
npm run cy:run -- --config baseUrl=http://todomvc.com/examples/dojo/
+++
You can override cypress.json
settings using environment variables that start with CYPRESS_
CYPRESS_baseUrl=http://todomvc.com/examples/dojo/ npx cypress open
# same
CYPRESS_BASE_URL=http://todomvc.com/examples/dojo/ npx cypress open
Note:
cypress.json
< environment variables < CLI parameter
+++
Use environment variables on CI. Especially to pass the private record key!
# bad practice, can accidentally show up in STDOUT
npx cypress run --record --recordKey abc...
# good
CYPRESS_RECORD_KEY=abc...
npx cypress run --record
+++
In cypress/plugins/index.js
module.exports = (on, config) => {
config.baseUrl = 'http://todomvc.com/examples/dojo/'
// change more options ...
return config
}
Docs: https://on.cypress.io/configuration-api
+++
You can return a resolved config as a promise.
module.exports = (on, config) => {
return new Promise((resolve, reject) => {
// load config from file or network
resolve(loadedConfig)
})
}
+++
You can change current setting per spec using Cypress.config call.
Cypress.config('baseUrl', 'http://todomvc.com/examples/dojo/')
beforeEach(function visitSite () {
cy.log('Visiting', Cypress.config('baseUrl'))
cy.visit('/')
})
Note: Use at your own risk, because the order of mutations and the final config in each test can be confusing.
+++
+++
cypress.json
< environment variable < CLI parameter < plugin < run-time
+++
Run a single spec in headless mode against:
localhost
http://todomvc.com/examples/dojo/
+++
That are not Cypress configuration - username, passwords, etc.
Guide https://on.cypress.io/environment-variables
+++
{
"baseUrl": "http://localhost:3000",
"env": {
"todoTitle": "env todo"
}
}
it('has env item', function () {
expect(Cypress.env('todoTitle'))
.to.equal('env todo')
})
+++
{
"eyes": "brown",
"t-shirt": "large"
}
Environment variables will be merged.
+++
Cypress.env() // returns entire merged object
Cypress.env(name) // returns single value
+++
Given cypress.env.json
{
"person": {
"name": "Joe"
}
}
Assert from the test that name is indeed Joe
.
Note:
Use Cypress._.get
or cy.wrap(Cypress.env()).its('person.name')
+++
npx cypress open --env todoTitle="env todo",life=42
+++
+++
Pass an object via command-line argument and see it in the configuration
npx cypress open --env ???
+++
CYPRESS_todoTitle="env todo" CYPRESS_name=CyBot \
npx cypress open
Unknown CYPRESS_
variables will be added to env
object.
+++
+++
module.exports = (on, config) => {
config.env.fooBar = 'baz'
return config
}
+++
it('has env', () => {
Cypress.env('life', 1)
expect(Cypress.env('life')).to.equal(1)
// change multiple values
Cypress.env({
life: 1,
state: 'busy'
})
})
+++
🛑 Cannot change env variables at run-time using Cypress.config('env', ...)
it('has env', () => {
expect(Cypress.env('life')).to.equal(42)
Cypress.config('env', {
life: 1
})
// nope, remains the same
expect(Cypress.env('life')).to.equal(42)
})
✅ Always use Cypress.env(name, value)
to change.
+++
Problem: let's create config settings per environment and load them using CLI argument.
npx cypress open --env staging
npx cypress open --env prod
Should load options from configs/staging.json
or from configs/prod.json
.
Note:
What options would you set in each JSON file?
Would they be merged with other settings in cypress.json
?
Answer at https://on.cypress.io/configuration-api
+++
--config-file <json filepath>
config |
env |
---|---|
cypress.json |
cypress.json |
command line | command line |
environment | environment |
plugin | plugin |
run-time | run-time |
cypress.env.json |