SauceLabs : Running an Example Sample Test

Introduction

In this post, we will see how to run a sample test on SauceLabs. You need to have a SauceLabs account to set up and run the program. The sample program requires the SauceLabs username and access key.

To get the access key:

https://www.testingdocs.com/saucelabs-credentials-to-run-cloud-tests/

 

SauceLabs Website

Capabilities

Let’s write a sample test that searches the Bing home page for a keyword and asserts the title of the results page.

We will run the program on Mozilla Firefox browser version 46 by setting the DesiredCapabilities and on Windows 10 operating system.

DesiredCapabilities caps = DesiredCapabilities.firefox(); caps.setCapability("platform", "Windows 10"); caps.setCapability("version", "46.0");

 

 

Sample Test:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import java.net.URL;
public class SampleBingTest { public static final String USERNAME = "SAUCE_USERNAME_GOES_HERE"; public static final String ACCESS_KEY = "SAUCELAB_ACCESS_KEY_GOES_HERE"; 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.firefox(); caps.setCapability("platform", "Windows 10"); caps.setCapability("version", "46.0"); WebDriver driver = new RemoteWebDriver(new URL(URL), caps); driver.get("https://www.bing.com"); driver.findElement(By.id("sb_form_q")).sendKeys("HelloWorld"); driver.findElement(By.id("sb_form_go")).click(); WebDriverWait wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.titleContains("Hello")); Assert.assertEquals("HelloWorld - Bing", driver.getTitle()); driver.quit(); }
}

 

Running Tests in SauceLab

If you are planning to run your tests on Saucelabs, below are some Basics to follow Before Running Your Test

You need to have set up a Sauce Labs account
You should have an existing Test that you want to run on Sauce.
The website you want to test must be accessible over the Internet for Browsers to be able to reach it.
If the website or application you want to test is behind a firewall, you need to set up a secure Sauce Connect Proxy tunnel to connect to it.