Test configuration per environment in Cypress - Build an Automation Script
/// <reference types="cypress" /> describe('Login tests per environment', () => { const environments = [ { name: 'development', baseUrl: Cypress.env('devBaseUrl'), username: Cypress.env('devUsername'), password: Cypress.env('devPassword') }, { name: 'production', baseUrl: Cypress.env('prodBaseUrl'), username: Cypress.env('prodUsername'), password: Cypress.env('prodPassword') } ]; environments.forEach(({ name, baseUrl, username, password }) => { context(`Login test on ${name} environment`, () => { beforeEach(() => { cy.visit(baseUrl + '/login'); }); it('should login successfully and redirect to dashboard', () => { cy.get('[data-test=username]').clear().type(username); cy.get('[data-test=password]').clear().type(password); cy.get('[data-test=login-button]').click(); cy.url().should('include', '/dashboard'); }); }); }); });
This test script uses Cypress to automate login tests for two environments: development and production.
We define an array environments holding the environment name, base URL, username, and password. These values come from Cypress environment variables, so no sensitive data is hardcoded.
For each environment, we create a test context. Before each test, we visit the login page using the environment's base URL.
The test enters the username and password into fields identified by data-test attributes, clicks the login button, and asserts that the URL includes /dashboard to confirm successful login.
This approach keeps tests clean, reusable, and environment-aware.
Now add data-driven testing with 3 different sets of user credentials per environment to verify login success and failure cases.
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
