Sauce Labs makes it easy to automate your tests across a wide range of OS/browser combinations on desktop and mobile devices. This topic provides an overview of how to set up and execute an automated Selenium test: connecting and authenticating your test with Sauce Labs, specifying desired capabilities for your tests, and ensuring the Sauce Labs browser cloud can connect to your app.

See the following sections for more information:

What You'll Need

The sample scripts assume you have access to:

You can find example tests ready to run against the Sauce Labs demo app in the demo directories (for example, demo-java) of our training repository.

Log in to Sauce Labs to Run the Tests

Before you modify your existing test scripts or the examples, you should log in to your Sauce Labs account and navigate to Dashboard > Automated Tests. As you modify the scripts, you can verify your changes by running the script and monitoring its progress from the dashboard.

Set the RemoteWebDriver Connection

If you have an existing Selenium script that you want to update to run on Sauce Labs, the first thing you need to do is to set the RemoteWebDriver to connect to the Sauce Labs browser cloud. For example, if your Selenium test is written in Java, you would set the connection to Sauce Labs like this:

 driver = new RemoteWebDriver(new URL("http://ondemand.saucelabs.com:80/wd/hub"), capabilities);

The example scripts in the next section are already configured to connect to the Sauce Labs US data center. 

Verify Authentication Credentials

You need to update your test script to include your username and access key for Sauce Labs, as shown in the examples below:

You can find your access key on your Account Profile page:

  1. Sign in to https://saucelabs.com with the username you set up when you created your account. 
    You will use this same username in your test script. 
  2. To find your access key, click your name in the Account Profile menu in the upper-right corner.
    1. Click User Settings.
    2. Scroll down to Access Key and click Show, and enter the password you used to sign in.
    3. Click the Copy icon.

To ensure your credentials work without having to update an existing script, copy and paste your credentials into the code as indicated in the appropriate example script, and then run it from your local machine. This will launch a test to open a browser, connect to the Sauce Labs sample app, and then close the browser and quit the test. 

We recommend using environment variables for your credentials instead of hard-coding them into your scripts. This makes it easier share tests, and avoids security issues that arise from exposing authentication credentials in your script. Once you've run your first test with hard-coded credentials, set up the environment variables on your local machine, and update your tests as demonstrated in the following examples:

Add Desired Capabilities

Desired capabilities dictate the platform, operating system, and browser you want to test against. You can use our Platform Configurator to automatically generates code snippets for these capabilities. 

  1. Go to the Platform Configurator
  2. Under API, select Selenium

    To run a test against a mobile native browser, select Appium, select the mobile device you want to test with, and then select Web Testing.

  3. Select the operating system and browser you want to test against
  4. Under Copy Code, select the language you are using
    The Platform Configurator will generate the code snippet for the desired capabilities
  5. Click Copy
  6. Paste the code into your Selenium test script, or into one of the example test scripts, and then run the test to see how the web app performs on that platform/OS/browser combination. In our example scripts, our demo test will open the browser, load the Sauce Labs sample web app, and then close the browser. 

Set the Web App to Test

Finally, you'll need to set the URL of the web app you want to test with driver.get. The example tests scripts are all set to run against the Sauce Labs demo app, so you can paste the URL for your web app in place of the saucedemo URL to ensure that the Sauce Labs browser cloud can connect to your app. Use Sauce Connect Proxy to establish a connection to an app hosted on your local machine or behind a firewall.

driver.navigate().to("https://www.saucedemo.com");

Best Practices and Reporting Test Results

Now that you've been able to get a test running on Sauce, there are a few other modifications you can make to your tests. These example scripts show how to:

You can find more information under Best Practices for Running Tests and Setting Test Status to Pass or Fail.