0
0
Cypresstesting~5 mins

Test configuration per environment in Cypress

Choose your learning style9 modes available
Introduction

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.

You want to test your app on a development server with test data.
You need to run tests on a staging environment before release.
You want to avoid using real user data in tests on production.
You want to change URLs or credentials depending on the environment.
You want to run the same tests but with different settings automatically.
Syntax
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.

Examples
Configuration for development environment with custom URL and credentials.
Cypress
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'https://dev.example.com',
    env: {
      username: 'devUser',
      password: 'devPass'
    }
  }
})
Configuration for production environment with secure credentials.
Cypress
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'https://prod.example.com',
    env: {
      username: 'prodUser',
      password: 'prodPass'
    }
  }
})
Override configuration and environment variables from command line for a test run.
Cypress
npx cypress run --config baseUrl=https://test.example.com --env username=testUser,password=testPass
Sample Program

This 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
// 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')
  })
})
OutputSuccess
Important Notes

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.

Summary

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