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 within 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 will authenticate against a single account.
Finding Your Username and Access Key
You can find your Sauce Labs username and access key in the User Profile > User Settings section of your Sauce Labs dashboard.
Setting Up Environment Variables on Mac OSX/Linux Systems
- In Terminal mode, enter
.bash_profile, and then press Enter.
i to insert text into your profile file.
Enter these lines:
export SAUCE_USERNAME="your Sauce username"
export SAUCE_ACCESS_KEY="your sauce access key"
- Press Escape.
Hold Shift and press Z twice (z z) to save your file and quit
In the terminal, enter
Setting Up Environment Variables on Windows Systems
- Click Start on the task bar.
- For Search programs and fields, enter E
- Click Edit the environment variables.
This will open the System Properties dialog.
- Click Environment Variables.
This will open the Environment Variables dialog.
- In the User variables section, click New.
This will open the New System Variable dialog.
- For Variable name, enter
- For Variable value, enter your Sauce username.
- Click OK.
- Repeat 4 - 8 to set up the
Referencing Environment Variables in Test Scripts
Once you've set up the environment variables for your credentials, you need to reference them within 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 repo on GitHub. This example is for C#.
C# Example Script Using Environment Variables
[Category("InstantSauceTest"), Category("NUnit"), Category("Instant")]
public class InstantSauceTest2
private IWebDriver _driver;
public void ShouldOpenOnSafari()
* Best Practice
* Instead of using hardcoded username and access key, you should store
* the credentials in environment variables on your system. Not sure how to do this?
* This document will help:
var sauceUserName =
var sauceAccessKey =
* In this section, we will configure our test to run on some specific
* browser/os combination in Sauce Labs
var capabilities = new DesiredCapabilities();
capabilities.SetCapability("platform", "macOS 10.13");
_driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
//TODO Please supply your publicly available web url in place of saucedemo.com
//In the future, you will learn how to access private URLs
//Hardcoded sleep just for demonstration purposes
//This test will always pass for demonstration purposes
*Below we are performing 2 critical actions. Quitting the driver and passing
* the test result to Sauce Labs user interface.
public void CleanUpAfterEveryTestMethod()
var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;