Skip to main content

Using Environment Variables for Authentication Credentials

As a best practice, we recommend setting your Sauce Labs authentication credentials as environment variables on your local system, that can then be referenced from in your tests. This provides an extra layer of security for your tests, and also enables other members of your development and testing team to write tests that authenticate against a single account.

For a list of Sauce Connect Proxy environment variables, see Environment Variables.

What You’ll Need

Setting Up Environment Variables

You can persist Sauce Connect Proxy environment variables by adding them to one of your user environment configuration files, such as .bash_profile or .zshrc.
  1. Open ~/.bash_profile or ~/.zshrc in your preferred text editor.
  2. Add the variables
    export SAUCE_USERNAME="your Sauce username"
    export SAUCE_ACCESS_KEY="your Sauce access key"
  3. Start a new shell or a new terminal.
  4. To confirm that your environment variables are set, enter echo $SAUCE_USERNAME in your terminal. The expected response is your username value.

Referencing Environment Variables in Test Scripts

Once you've set up the environment variables for your credentials, you need to reference them in the test scripts that you want to run on Sauce. You can find examples of test scripts that use environment variables for authentication in the demo directory for each language in the Sauce Labs Training repository on GitHub.

Below are examples of how to set environment variables in a given language/framework:

Authenticating with Environment Variables