Skip to end of metadata
Go to start of metadata
As a best practice, we recommend setting your Sauce Labs authentication credentials as environment variables that can be referenced from within your tests, as shown in this code example. 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.

 

 Setting Environment Variables for Mac OS X/Linux
  1. In Terminal mode, enter vi ~/.bash_profile, and then press Enter.
  2. Press i to insert text into your profile file.
  3. Enter these lines:

    export SAUCE_USERNAME="your Sauce username"
    export SAUCE_ACCESS_KEY="your sauce access key"
  4. Press Escape.
  5. Hold Shift and press Z twice (z z) to save your file and quit vi.

  6. In the terminal, enter source ~/.bash_profile.

 

 Setting Environment Variables for Authentication Credentials on Windows
  1. Click Start on the task bar.
  2. For Search programs and fields, enter Environment Variables.
  3. Click Edit the environment variables.
    This will open the System Properties dialog.
  4. Click Environment Variables.
    This will open the Environment Variables dialog.
  5. In the System variables section, click New.
    This will open the New System Variable dialog.
  6. For Variable name, enter SAUCE_USERNAME.
  7. For Variable value, enter your Sauce username.
  8. Click OK.
  9. Repeat 4 - 8 to set up the SAUCE_ACCESS_KEY.

    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. You should also check out our topic on setting your username and access key as environment variables.

 

 Java Example of Using Environment Variables for Authentication
public static final String USERNAME = System.getenv("SAUCE_USERNAME");
public static final String ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
public static final URL ENDPOINT = new URL("http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub");
 PHP Example of Using Environment Variables for Authentication
define('SAUCE_HOST', SAUCE_USERNAME.':'.SAUCE_ACCESS_KEY.'@ondemand.saucelabs.com');
 Python Example of Using Environment Variables for Authentication
from os import environ
 
username = environ.get('SAUCE_USERNAME', None)
access_key = environ.get('SAUCE_ACCESS_KEY', None)
selenium_endpoint = "http://%s:%s@ondemand.saucelabs.com:80/wd/hub" % (username, access_key)
 Ruby Example of Using Environment Variables for Authentication
username = ENV["SAUCE_USERNAME"]
access_key = ENV["SAUCE_ACCESS_KEY"]
remote_server_url = "http://#{username}:#{access_key}@ondemand.saucelabs.com:80/wd/hub"
 C# Example of Using Environment Variables for Authentication
using System; //Importing the System library

internal readonly static string SAUCE_LABS_ACCOUNT_NAME = System.Environment.GetEnvironmentVariable("SAUCE_USERNAME");
internal readonly static string SAUCE_LABS_ACCOUNT_KEY = System.Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");