Skip to end of metadata
Go to start of metadata

This script illustrates the basic set up of a test that runs in the Sauce Labs virtual browser cloud. It includes using environment variables for authentication credentials, using tags and build numbers for test result management, and reporting the Pass/Fail status of the test to the Sauce Labs dashboard.

Example Only

The code in this topic is presented as an example only, since your tests and testing environments may require specialized scripting. This information should be taken only as an illustration of how you could set up your tests with Sauce Labs, and is not directly supported by Sauce.


Example Script

When this script runs, it opens the browser specified in the script, navigates to the saucelabs.demo web app, and then closes the browser. If you want to run this script using your Sauce Labs credentials and have it navigate to your own web app:

  1. Follow the instructions in Best Practice: Use Environment Variables for Authentication Credentials to set up your Sauce Labs account access.
  2. Enter the URL for the web app you want to test in the place of
    If your website is hosted on a local network or behind a firewall, you'll need to set up sauce connect proxy to launch secure tunnels between between it and the Sauce Labs browser cloud.
  3. Once you've been able to run the test against your web app, you can use the Platform Configurator to experiment with changing the desired capabilities of the test. 

You can clone this script from the saucelabs-training repository on GitHub:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class InstantSauceTestNGTest4 {

    private WebDriver driver;

    public void shouldOpenChrome() throws MalformedURLException {
        /** Here we set environment variables from your local machine, or IntelliJ run configuration,
         *  and store these values in the variables below. Doing this is a best practice in terms of test execution
         *  and security. If you're not sure how to use env variables, refer to this guide -
         * or check */

        String sauceUserName = System.getenv("SAUCE_USERNAME");
        String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY");
        String sauceURL = System.getenv("SAUCE_URL");

         * Here we set DesiredCapabilities, in this exercise we set additional capabilities below that align with
         * testing best practices such as timeouts, tags, and build numbers
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("username", sauceUserName);
        capabilities.setCapability("accessKey", sauceAccessKey);
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("platform", "Windows 10");
        capabilities.setCapability("version", "59.0");
        capabilities.setCapability("name", "4-best-practices");

        /** Tags are an excellent way to control and filter your test automation
         * in Sauce Analytics. Get a better view into your test automation.
        List<String> tags = Arrays.asList("sauceDemo", "demoTest", "module4");
        capabilities.setCapability("tags", tags);

        /** Another of the most important things that you can do to get started
         * is to set timeout capabilities for Sauce based on your organizations needs. For example:
         * How long is the whole test allowed to run?*/
        capabilities.setCapability("maxDuration", 3600);
        /** A Selenium crash might cause a session to hang indefinitely.
         * Below is the max time allowed to wait for a Selenium command*/
        capabilities.setCapability("commandTimeout", 600);
        /** How long can the browser wait for a new command */
        capabilities.setCapability("idleTimeout", 1000);

        /** Setting a build name is one of the most fundamental pieces of running
         * successful test automation. Builds will gather all of your tests into a single
         * 'test suite' that you can analyze for results.
         * It's a best practice to always group your tests into builds. */
        capabilities.setCapability("build", "SauceDemo");

        /** If you're accessing the EU data center, use the following endpoint:.
         * */
        driver = new RemoteWebDriver(new URL(sauceURL), capabilities);
        /** Don't forget to enter in your application's URL in place of ''. */

     * Below we are performing 2 critical actions. Quitting the driver and passing
     * the test result to Sauce Labs user interface using the Javascript Executor.
    public void cleanUpAfterTestMethod(ITestResult result) {
        ((JavascriptExecutor)driver).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));