Test configuration per environment helps you run tests with settings that match different places like development, testing, or production. This makes sure your tests work correctly everywhere.
Test configuration per environment in Cypress
cypress.config.js const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'https://default-url.com', env: { username: 'defaultUser', password: 'defaultPass' } } })
The baseUrl sets the main URL for tests.
The env object holds environment-specific variables like usernames or passwords.
const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'https://dev.example.com', env: { username: 'devUser', password: 'devPass' } } })
const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'https://prod.example.com', env: { username: 'prodUser', password: 'prodPass' } } })
npx cypress run --config baseUrl=https://test.example.com --env username=testUser,password=testPassThis test uses the username and password from the Cypress environment variables set in the config file. You can change these values per environment to test different setups.
// cypress.config.js const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'https://default.example.com', env: { username: 'defaultUser', password: 'defaultPass' } } }) // cypress/e2e/login.cy.js describe('Login Test', () => { it('logs in using environment config', () => { cy.visit('/') cy.get('#username').type(Cypress.env('username')) cy.get('#password').type(Cypress.env('password')) cy.get('button[type=submit]').click() cy.url().should('include', '/dashboard') }) })
You can pass environment variables via command line to override config values.
Use Cypress.env('variableName') to access environment variables in tests.
Keep sensitive data like passwords out of config files by using environment variables or CI secrets.
Test configuration per environment helps run tests with different settings easily.
Use baseUrl and env in cypress.config.js to set environment-specific values.
Access environment variables in tests with Cypress.env().
Practice
baseUrl in Cypress configuration for different environments?Solution
Step 1: Understand
baseUrlrolebaseUrldefines the main website address for tests to run against.Step 2: Relate
Setting differentbaseUrlto environmentbaseUrlvalues per environment lets tests run on different sites without changing test code.Final Answer:
To run tests against different websites without changing test code -> Option BQuick Check:
baseUrl changes target site = A [OK]
- Confusing baseUrl with test timeout
- Thinking baseUrl changes test framework version
- Assuming baseUrl enables debugging
apiKey inside a Cypress test?Solution
Step 1: Recall method to get env variables
Cypress providesCypress.env()to access environment variables.Step 2: Match method with variable name
UsingCypress.env('apiKey')correctly retrieves theapiKeyvalue.Final Answer:
const key = Cypress.env('apiKey') -> Option AQuick Check:
Cypress.env() gets env vars = C [OK]
- Using Cypress.config() instead of Cypress.env()
- Trying Cypress.get() which does not exist
- Using Cypress.variable() which is invalid
cypress.config.js snippet:export default defineConfig({
env: {
username: 'user1',
password: 'pass1'
}
})What will
console.log(Cypress.env('username')) print during a test?Solution
Step 1: Check env object in config
The config setsenv.usernameto 'user1'.Step 2: Understand Cypress.env() behavior
Cypress.env('username')returns the value of theusernamekey from env, which is 'user1'.Final Answer:
'user1' -> Option CQuick Check:
Env username value = 'user1' [OK]
- Expecting the key name instead of its value
- Assuming undefined if not set in test code
- Confusing Cypress.env() with Cypress.config()
cypress.config.js:env: {
apiUrl: 'https://api.example.com'
}But inside your test,
Cypress.env('apiUrl') returns undefined. What is the most likely cause?Solution
Step 1: Check config structure
Theenvobject must be inside the exported config object for Cypress to read it.Step 2: Identify common mistake
Ifenvis outside or not properly nested,Cypress.env()returns undefined.Final Answer:
The env object is not inside the exported config -> Option AQuick Check:
env must be inside exported config = B [OK]
- Not exporting the config at all
- Using process.env instead of Cypress.env
- Thinking Cypress.env only works for baseUrl
staging and production. You set baseUrl and env.apiKey differently for each in cypress.config.js. How can you run tests on staging without changing the config file each time?Solution
Step 1: Understand environment config switching
You can pass environment variables or flags via command line to select config variants.Step 2: Use command line flag to select config
Using--env configFile=staginglets you load staging config without editing files.Final Answer:
Use command line flag like cypress run --env configFile=staging and load config accordingly -> Option DQuick Check:
Command line env flags switch configs = A [OK]
- Editing config file manually each time
- Hardcoding URLs inside tests
- Ignoring CLI environment options
Ready to prove it?
Pass the test and this pattern moves unknown
