Specify Environment Variables for Your Cypress Tests

Specify Environment Variables for Your Cypress Tests

Learn how to specify environment variables on BrowserStack for your Cypress tests

We support the following ways of specifying environment variables for your Cypress tests:

  1. Using the env key in your cypress.json config file
  2. Creating a cypress.env.json file as supported by Cypress
  3. Using the --env param of our CLI
  4. Defining environment variables starting with CYPRESS_ in your system
  5. Specifying environment variables in a .env file and using dotenv npm package to read the values
  6. Setting any system level environment variable in your/CI system and using the same in your Cypress tests

Using your Cypress config file

Any key/value you set in your Cypress configuration file (cypress.json by default) under the env key will become an environment variable that you can access using Cypress.env in your tests.

Learn more about this option on Cypress docs.

For example:

cypress.json

{
  ...
  "env": {
    "login_url": "/login",
    "products_url": "/products",
  }
}

Using the cypress.env.json file

You can create your own cypress.env.json file that Cypress will automatically check. Values in here will overwrite conflicting environment variables in your configuration file (cypress.json by default). Learn more about this option on Cypress docs.

For example:

cypress.env.json

{
  "host": "veronica.dev.local",
  "api_server": ""
}
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.