Skip to end of metadata
Go to start of metadata

Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications). This topic covers the basic information and example code you need to get your Java tests up and running on Sauce. 

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.

Prerequisites

Before getting started, you should review the Best Practices for Running Tests.

You need to have these components installed to set up testing on Sauce with Java. 

  • You must have JDK 1.6  or higher installed
  • You need to have the selenium-java client for your operating system installed. You can download it from http://www.seleniumhq.org/download/, under the section Selenium Client & WebDriver Language Bindings.
  • You should install the Java Helper Library, which will automatically update your Sauce Labs dashboard with information like whether tests have passed or failed, output the Sauce Session ID to stdout for parsing by the Sauce Jenkins and Bamboo plugins, and which provides a com.saucelabs.common.SauceOnDemandAuthentication class, which handles obtaining the Sauce OnDemand user name and access key from environment variables or the filesystem.

Quick Start

Configuring your existing Java-based Selenium tests to run on Sauce Labs is simple. The basic change is just to switch from using a local Selenium driver, to using a remote driver pointed at ondemand.saucelabs.com, specifying your Sauce Labs account credentials and desired browser configuration.
Local Testing Java Example
WebDriver driver = new FirefoxDriver(); 
Java Testing on Sauce Labs Example
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability("platform", "Windows 7");
caps.setCapability("version", "38.0");
WebDriver driver = new RemoteWebDriver(new URL("http://YOUR_USERNAME:YOUR_ACCESS_KEY@ondemand.saucelabs.com:80/wd/hub"),

Code Sample

This code example illustrates setting up a simple Java test to find the title of a page hosted by Sauce Labs. 

You can clone this script directly from our GitHub repo

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class SampleSauceTest {

  public static final String USERNAME = "YOUR_USERNAME";
  public static final String ACCESS_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub";

  public static void main(String[] args) throws Exception {

    DesiredCapabilities caps = DesiredCapabilities.chrome();
    caps.setCapability("platform", "Windows 10");
    caps.setCapability("version", "latest");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

    /**
     * Goes to Sauce Lab's guinea-pig page and prints title
     */

    driver.get("https://saucelabs.com/test/guinea-pig");
    System.out.println("title of page is: " + driver.getTitle());

    driver.quit();
  }
}

Running Local Tests

Developing websites/apps within your local network is secure and efficient. The drawback to this method is that local assets are not publicly-accessible on the Internet, so the browsers/devices in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect is a proxy server that creates a secure tunnel connection between the Sauce Labs virtual machine that runs your test and your local  network. You can also use Sauce Connect to test applications and websites that are located within your corporate firewall. Sauce Connect is not required to run tests on Sauce Labs, only in situations where the application or website you want to test is not publicly accessible. 

Running Tests in Parallel

Now that you’re running tests on Sauce, you may wonder how you can run your tests faster. One way to increase speed is by running tests in parallel across multiple virtual machines.

You can run your tests in parallel at two levels, and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and want to run on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example this would be more like 50 parallel tests. Doing this requires that your tests are written in a way that they do not collide with one another, as described in our Best Practice topics avoiding external test dependencies and avoiding dependencies between tests.

Match Thread Count to Concurrency Limit

You should match your thread count to your concurrency limit, which is shown in the My Account section of your user profile information on the Sauce Labs dashboard.  

Most Java users use one of two popular third party testing frameworks: TestNG or Junit. These links are for two example projects written in each. They are designed to run in parallel. You can clone them and add your own test cases if you want:
  1. https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Selenium
  2. https://github.com/saucelabs-sample-test-frameworks/Java-TestNG-Selenium

Running Tests in Parallel and Across Multiple Browsers

Tests can be run in parallel at two levels: you can run your tests in parallel,and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and run them serially on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example, this would be 50 parallel tests (10 tests, five browsers). This requires that your tests are written in a way that they do not collide with one another. For more on this see Selenium WebDriver - Running Your Tests in Parallel blog.

For more information, check out the example scripts in our GitHub repo

Reporting on Test Results

The Java Helper Library will automatically send test names and pass/fail results to your Sauce Labs dashboard if you are using TestNG or JUnit as your framework.

If using other frameworks, then use the SauceRest Java Library to update the pass or fail status of the test.

You should also check out the topic Best Practice: Use Build IDs, Tags, and Names to Identify Your Tests for more suggestions on how to improve test reporting and using build numbers for your continuous integration platform.