Skip to main content

Specify environment variables for 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": "http://localhost:8888/api/v1/"
}

Using the CLI

You can pass the environment variables for your tests using the --env argument while running your tests on BrowserStack using the run command. This is similar to Cypress’ --env option. Learn more about it on Cypress docs.

For example:

Command Line
browserstack-cypress run --sync --env api_host=http://example.com,products_api_path=/api/v1/products.json

Env variables beginning with CYPRESS_

browserstack-cypress CLI will automatically read any environment variable in your system that starts with CYPRESS_*. The variable and its value will be made available in your Cypress tests after stripping off the CYPRESS_ part from the name of the variable.

The variable will be set as an OS level environment variable in the BrowserStack machines that run your tests and hence you can access such variables using process.env.ENV_VAR_NAME. Learn more about it on Cypress docs.

Env variables in .env file

You can also define all your environment variables in a .env file in the same directory where browserstack.json resides and then use something like a dotenv npm package to read that file and set all the variables as system level environment variables on the BrowserStack machine.

Note: The environment variables set through this method will be system level variables and can be accessed in your Cypress tests using process.env.ENV_VAR_NAME.

Use any system level env variables

This is a unique feature that BrowserStack provides wherein you can choose to use any other system level environment variable that you have set in your system, also in the BrowserStack machines where your Cypress tests will run.

For e.g. you have generated an npm token for your private repository and have it saved in an environment variable or you want to run your tests from a CI environment where passwords and other secrets are saved as environment variables. You can specify the names of all those environment variables that are available in your system, in the browserstack.json as shown below:

browserstack.json
{
  ...
  "run_settings": {
    ...
    "system_env_vars": ["NPM_TOKEN", "GITHUB_SECRET", "ANY_OTHER_ENV_VAR_AVAILABLE_IN_YOUR_SYSTEM"],
  }
}

As you define the system_env_vars, the browserstack-cypress CLI will read these environment variables from your system and save them as an environment variable in the BrowserStack machine that would run your Cypress tests. So, you would be able to seamlessly use these variables in your Cypress test runs on BrowserStack using process.env.ENV_VAR_NAME.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy