Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process of establishing private environment variables for Cypress tests involves the following steps:

  1. Create a new file named .env at the root of your project directory.

  2. Add the private environment variables one per line in the following format:

MY_VARIABLE=value
ANOTHER_VARIABLE=value
  1. Use the dotenv package to load the environment variables in your test code.
require('dotenv').config()

const myVariable = process.env.MY_VARIABLE
const anotherVariable = process.env.ANOTHER_VARIABLE
  1. Use these environment variables in your Cypress tests, for example:
it('should log in with private credentials', () => {
  cy.visit('https://example.com/login')
    .get('#username').type(process.env.MY_USERNAME)
    .get('#password').type(process.env.MY_PASSWORD)
    .get('#login-button').click()
})

Note that you should never include .env in your Git repository, and you should never share your private environment variables with others.