Ask Your Question
3

What is the process for establishing private environment variables for Cypress tests?

asked 2022-04-17 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-07-21 16:00:00 +0000

david gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-17 11:00:00 +0000

Seen: 9 times

Last updated: Jul 21 '21