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
- A Sauce Labs account (Log in or sign up for a free trial license)
- Your Sauce Labs Username and Access Key
Setting Up Environment Variables
- macOS and Linux
- Windows
.bash_profile
or .zshrc
.- Open
~/.bash_profile
or~/.zshrc
in your preferred text editor. - Add the variables
export SAUCE_USERNAME="your Sauce username"
export SAUCE_ACCESS_KEY="your Sauce access key" - Start a new shell or a new terminal.
- To confirm that your environment variables are set, enter
echo $SAUCE_USERNAME
in your terminal. The expected response is your username value.
- Open the Control Panel and click the System icon to open the System Properties dialog.
- Click Environment Variables to open the Environment Variables dialog.
- In the User variables section, click New to open the New System Variable dialog.
- For Variable name, enter SAUCE_USERNAME and for Variable value, enter your Sauce username and then click OK.
- Repeat 3-4 to set up the SAUCE_ACCESS_KEY or any other environment variable.
- 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:
- Java
- Node.js
- Python
- Ruby
- C#
loading...
loading...
loading...
loading...
loading...