Environment variables let you change settings without changing your test code. This helps tests run in different places easily.
Environment variables for configuration in Cypress
Cypress.env('VARIABLE_NAME') // or set in cypress.config.js // env: { VARIABLE_NAME: 'value' }
Use Cypress.env('VARIABLE_NAME') to get the value of an environment variable in your test.
You can set environment variables in cypress.config.js or via command line when running tests.
Cypress.env('baseUrl')baseUrl.cy.visit(Cypress.env('baseUrl'))username and password in the config file.// cypress.config.js module.exports = { e2e: { env: { username: 'testuser', password: 'mypassword' } } }
baseUrl when running tests.// Run test with env variable from command line npx cypress run --env baseUrl=https://example.com
This test uses environment variables for the website URL, username, and password. It visits the URL, fills in the login form, and checks if login was successful by verifying the URL.
// cypress.config.js const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { env: { baseUrl: 'https://staging.example.com', username: 'user123', password: 'pass123' }, setupNodeEvents(on, config) { // implement node event listeners here if needed } } }) // cypress/e2e/login.cy.js describe('Login Test', () => { it('logs in using environment variables', () => { cy.visit(Cypress.env('baseUrl')) 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') }) })
Environment variables help keep sensitive data out of your test code.
You can override environment variables from the command line for flexibility.
Always use Cypress.env() to access variables inside tests for consistency.
Environment variables let you configure tests without changing code.
Set variables in cypress.config.js or via command line.
Use Cypress.env('VAR_NAME') to get values in tests.
Practice
Solution
Step 1: Understand environment variables role
Environment variables store configuration data separately from code.Step 2: Identify their use in Cypress
They allow changing test settings without modifying test scripts.Final Answer:
To configure tests without changing the test code -> Option AQuick Check:
Environment variables = configure tests without code change [OK]
- Confusing environment variables with test assertions
- Thinking environment variables locate page elements
- Assuming environment variables generate reports
API_URL inside a Cypress test?Solution
Step 1: Recall Cypress environment variable syntax
Cypress usesCypress.env('VAR_NAME')to access env variables.Step 2: Match the correct syntax
Only Cypress.env('API_URL') uses this correct method.Final Answer:
Cypress.env('API_URL') -> Option AQuick Check:
Access env vars with Cypress.env('VAR_NAME') [OK]
- Using Node.js process.env inside Cypress tests
- Trying to access env vars as plain variables
- Using incorrect function names like getEnv
cypress.config.js snippet:export default defineConfig({
env: {
USERNAME: 'testUser',
PASSWORD: 'pass123'
}
})What will
cy.log(Cypress.env('USERNAME')) print during test execution?Solution
Step 1: Check environment variable definition
USERNAME is set to 'testUser' in cypress.config.js under env.Step 2: Understand Cypress.env usage
Cypress.env('USERNAME') returns the value 'testUser'.Final Answer:
'testUser' -> Option CQuick Check:
Cypress.env('USERNAME') = 'testUser' [OK]
- Expecting the key name instead of the value
- Assuming undefined if not set in test file
- Thinking Cypress.env throws error if variable exists
const apiUrl = Cypress.env.API_URL; cy.visit(apiUrl);
But the test fails with
TypeError: Cannot read property 'API_URL' of undefined. What is the problem?Solution
Step 1: Identify how Cypress.env is accessed
Cypress.env is a function, not an object, so use parentheses and quotes.Step 2: Understand the error cause
Using dot notation like Cypress.env.API_URL causes undefined error.Final Answer:
You should useCypress.env('API_URL')instead ofCypress.env.API_URL-> Option DQuick Check:
Access env vars with Cypress.env('VAR_NAME') [OK]
- Using dot notation instead of function call
- Assuming env vars are properties, not accessed by function
- Ignoring error message details
cypress.config.js. Which command line option correctly sets the environment variable API_URL to https://api.test.com?Solution
Step 1: Understand command line env variable setting
Use--env VAR_NAME=valueto set env variables temporarily.Step 2: Identify correct command for running tests
cypress runruns tests in headless mode;--envsets env vars.Final Answer:
cypress run --env API_URL=https://api.test.com -> Option BQuick Check:
Use --env to set env vars on command line [OK]
- Using --config instead of --env for environment variables
- Trying to set env vars with cypress open command
- Confusing config options with env variables
Ready to prove it?
Pass the test and this pattern moves unknown
