Sauce Labs Selenium Tutorial with Examples!

In this Sauce Labs Selenium Tutorial, we will implement our first JAVA Selenium Sauce Labs test by using RemoteWebDriver. RemoteWebDriver connects to a server, sends requests to that server and that server drives local browsers according to its requests. It needs to be configured, then it can run a test on external machines. It composed of a client and a server. The client is our webdriver test and the server is a Java servlet. The server will always run on the machine with the browser you want to test.

We configure RemoteWebdriver with DesiredCapabilities (it configures browser type, version, platform, etc.) and external server URL. Then, we can run our tests on the server which we configured for RemoteWebDriver.

We can set up Selenium Server on our local computer or we can use cloud grid system providers such as Sauce Labs, Browserstack or TestingBot, etc. These kinds of companies provide extensive grids on their servers. You can find all OS and all browser versions. In this post, I will explain how to run our Java Selenium Tests on Sauce Labs. In order to test on those provider’s servers, we need to go to their sites and register for a free plan.

Selenium Cloud Testing on Sauce Labs

Sauce Labs is a grid in the cloud. We can run our tests on their cloud with different browsers and operating systems. You can also do manual testing. In order to do these, first, we should go saucelabs.com and register for free as shown below.

Sauce Labs Selenium Tutorial

Then, when you log in to Sauce Labs, you will see the below screen.

Sauce Labs Selenium

Then select “Automated testing (15 mins) and then select JAVA.

Then, we should click the “Start Testing” button and follow the three steps which will be shown on the next page.

The first step is to get “sauce connect proxy” and do its setup. For this, we need to click the TUNNELS and download a proxy according to our operating system. In this article, I will do it for macOS but for the others also steps should be similar.

sauce labs selenium builder

After downloading the “sauce connect proxy” zip package, we should continue with the following steps.

First, select your data center as shown below:

Then, go to the extracted “sauce connect proxy” directory and run the below command. You can get the command from the Sauce Labs website. Just copy, and paste as shown below.

sauce labs selenium webdriver 

Now, we connected the Saucelabs environment with sauce connect proxy (sc). From now on, I will call is as “sc”. :-)

SauceLabs LIVE

First, let’s see how we can do testing live in a manual way. Especially, this kind of testing is really helpful when you are testing nasty problems for a specific browser type and version.

After filling URL,” Sauce Connect Proxy”, and the browser version, you can start the session by clicking the start session button and a new session will be started on a new window as you can see below and you can test any website on the browser which you selected. :-)

SauceLabs AUTOMATED

Before starting coding for automated test execution in Sauce Labs, I suggest setting SAUCE_USERNAME and SAUCE_ACCESS_KEY environmental variables. This differs on Windows and Mac but on Mac, you can open your .bashrc or .profile file and add the below lines.

export SAUCE_USERNAME='your-user-name'
export SAUCE_ACCESS_KEY='your-access-key'

You can find these values on the Sauce labs account -> user settings page.

Then you need to source your .profile or .bash_profile or .zsh_profile file. 

source .profile  

Then you can test the environment variables with echo commands.

echo $SAUCE_USERNAME
echo $SAUCE_ACCESS_KEY

After these steps, we can open IntelliJ and implement our code. If it is already opened, please re-start your IntelliJ to see newly defined and sourced environment variables.

Here, I want to share a sample example as follows.

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class SauceLabsExample {
    WebDriver driver;
    //Setting environmental variables
    String sauceUserName = System.getenv("SAUCE_USERNAME");
    String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY");
    String sauceURL = "https://ondemand.us-west-1.saucelabs.com:443/wd/hub";

    @Test
    public void shouldOpenSwTestAcademyOnSauceLabs() throws MalformedURLException {
        //Here we set the MutableCapabilities for "sauce:options", which is required for newer versions of Selenium.
        MutableCapabilities sauceOpts = new MutableCapabilities();
        sauceOpts.setCapability("username", sauceUserName);
        sauceOpts.setCapability("accessKey", sauceAccessKey);

        //In order to use w3c you must set the seleniumVersion
        sauceOpts.setCapability("seleniumVersion", "4.0.0-beta-2");
        sauceOpts.setCapability("name", "4-best-practices");

        //Tags are an excellent way to control and filter your test automation
        List<String> tags = Arrays.asList("sauceDemo", "demoTest", "module4", "javaTest");
        sauceOpts.setCapability("tags", tags);

        //Timeout capability
        sauceOpts.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.
        sauceOpts.setCapability("commandTimeout", 600);

        //How long can the browser wait for a new command
        sauceOpts.setCapability("idleTimeout", 1000);

        //Setting a build name
        sauceOpts.setCapability("build", "SW Test Academy Sauce Example - Java-Junit5");

        //Required to set w3c protocol
        ChromeOptions chromeOpts = new ChromeOptions();
        chromeOpts.setExperimentalOption("w3c", true);

        //Set a second MutableCapabilities object to pass Sauce Options and Chrome Options
        MutableCapabilities capabilities = new MutableCapabilities();
        capabilities.setCapability("sauce:options", sauceOpts);
        capabilities.setCapability("goog:chromeOptions", chromeOpts);
        capabilities.setCapability("browserName", "chrome");
        capabilities.setCapability("platformVersion", "Windows 10");
        capabilities.setCapability("browserVersion", "latest");


        driver = new RemoteWebDriver(new URL(sauceURL), capabilities);
        /** Don't forget to enter in your application's URL in place of 'https://www.saucedemo.com'. */
        driver.navigate().to("https://www.swtestacademy.com");
        assertTrue(driver.getTitle().contains("Software Test Academy"));
    }

    //Below we are performing 2 critical actions. Quitting the driver and passing the test result to Sauce Labs user interface.
    @AfterEach
    public void cleanUpAfterTestMethod() {
        ((JavascriptExecutor) driver).executeScript("sauce:job-result=" + ("passed"));
        driver.quit();
    }
}

After running this test, I saw that it passed. By the way, you can see SauceURL on account -> User settings -> Driver Creation section. You need to remove the username and access key that is written there as a URL. In the code, you can see the sauceURL as a reference.

And after tests run, we can see the results on the Sauce Labs dashboard.

And if you go to one of the test details by clicking one of them, you can see the test result details, and test execution video.

I hope you enjoyed this article. Please write a comment if you see any problems or missing points.

Sauce Labs Selenium Tutorial Github Project

https://github.com/swtestacademy/selenium-examples/blob/main/src/test/java/saucelabs/SauceLabsExample.java

Thanks for reading,
Onur

6 thoughts on “Sauce Labs Selenium Tutorial with Examples!”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.