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().