Write First Selenium-Cloud Test with Sauce Labs

Monil Joshi
6 min readFeb 13, 2022

--

In previous article, we saw what is need of cloud platforms to test Web and Mobile test. You can check that article here

Need of Cloud Services for Web & Mobile application testing.

In this article we will see how can we start with Sauce Labs.

To work with Sauce Labs, first thing you must have a account with Sauce Labs. You can create free trail account which will be valid till 28 days.

  1. Go to https://saucelabs.com/
  2. Click on Try it for free.
  3. Click on Sign up with Email
  4. Provide valid Email address, Username and Password
  5. It shows initial Data center is from Australia. Don’t worry you can change it later
  6. Click on Sign Up

Once you Sign up it will ask you few questions to understand you and your requirements. You just have to select among suggestion options. Once you are done with this set up, you will get a email verification. You have to verify your email using that link.

Next thing you will have to do it You need to find your account details to provide it in your test case. It helps your test to connect to your Sauce Labs account.

  1. Click on Account > User Settings
User Settings

2. You will land on My Account page

3. From this page you have to pick UserName, AccessKey, and Driver Creation URL. These are the entry points for you script. Driver Creation URL is a URL, combination of UserName, AccessKey and data center.

https://username:accessKey@ondemand.dataCenter.saucelabs.com:80/wd/hub

4. Note all these things as we will need it while writing script.

5. Benefit of using cloud platform is you do not have write capabilities of your own you select platform, select device type and device and it give you script in desired language

6. Search for Platform Configurator in Sauce labs.

7. You can see several options for Windows and Mac OS for Web executions along with all the real browsers Chrome, Edge, Firefox, Safari with most of the major version of the browser including beta versions.

8. There are lot of options available for Mobile testing with iOS simulator and emulator

9. Also you can select any resolution for Web or Mobile test.

Sauce Labs capability language support
Different Selenium Versions for Web Executions
Different Operating System for Web & Mobile Testing
Cross Browser Support
Browser Version Support
Resolution Support
Appium Version support for Select Device

In this we will see how to write Web Test case on Sauce Labs on Edge browser.

Create a class. Now you will be using 3 things that you have noted earlier. UserName, AccessKey, and Driver Creation URL.

Create 3 variables and provide proper value to it.

public static final String USERNAME = "user_name";
public static final String ACCESS_KEY = "access_key";

Also you have to create URL to connect your sauce account and this is nothing but combination of UserName, AccessKey and data center. We can use this URL in our capabilities as it contains to enter in to our Sauce labs account Our URL is

https://username:accessKey@ondemand.dataCenter.saucelabs.com:443/wd/hub

Create one more variable for the same

public static final String Exe_URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.us-west-1.saucelabs.com:80/wd/hub";

Use the capabilities provided by Sauce labs platform configurator as I shown above. I am using it for Edge browser

EdgeOptions browserOptions = new EdgeOptions();
browserOptions.setCapability("platformName", "Windows 10");
browserOptions.setCapability("browserVersion", "91.0");

Next step is creating Driver instance. Generally we connect to our local browser driver like Firefox, Chrome So we write

WebDriver driver = new ChromeDriver();
OR
WebDriver driver = new FirefoxDriver();

But in this case these machines are present on cloud and we are connecting them remotely. We are using Parent class of these local drivers, i.e RemoteWebDriver

You can read this article on how all the classes and interfaces interconnected.

What is WebDriver interface & hierarchy of related interfaces and Classes

If you check the constructor of RemoteWebDriver.

public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
this(new HttpCommandExecutor(remoteAddress), capabilities);
}

We have to provide URL with remote address and capabilities. We have already form our URL and write capabilities. Provide those variable to RemoteWebDriver. Check the parameter name provided above

driver = new RemoteWebDriver(new URL(Exe_URL), browserOptions);

Then write any test you want to test.

@Test
public void testGoogle(){

driver.get("https://www.google.com/");
driver.findElement(By.name("q"));
Assert.assertEquals(driver.getTitle(),"Google");

}

Write Tear down method to quit the open browser instance.

@AfterTest
public void tearDown(ITestResult result) throws Exception{
driver.quit();
}

If you observe execution on Sauce it will have phases like Queued, Running etc.

WOW..!!

You have just executed your test on Sauce Labs. Here is complete code.

public class Web_Execution_on_Sauce{public static final String USERNAME = "user_name";public static final String ACCESS_KEY = "access_key";public static final String Exe_URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.us-west-1.saucelabs.com:80/wd/hub";WebDriver driver;    @BeforeTest
public void sauceCapabilities() throws MalformedURLException {
EdgeOptions browserOptions = new EdgeOptions();
browserOptions.setCapability("platformName", "Windows 10");
browserOptions.setCapability("browserVersion", "91.0");
driver = new RemoteWebDriver(new URL(Exe_URL), browserOptions);
}
@Test
public void testGoogle(){
driver.get("https://www.google.com/");
driver.findElement(By.name("q"));
Assert.assertEquals(driver.getTitle(),"Google");

}
@AfterTest
public void tearDown(){
driver.quit();
}

BUT WAIT.

Let’s check the execution result on. You will get result something like this.

Job Completed

It gets marked with “Completed” Status. You will not understand exactly what happened to your test whether it gets passed or failed. Unless and until you see a video or you have check from where you executing this code. This is not at all feasible.

What is the solution?

Sauce Labs mark execution as completed once it is done. We have to make sure we are providing Test Status whether it pass or fail once it is completed.

How can we do that?

We will be using Interface ITestResult present in TestNG. We will be calling this in our tear down. You have to pass reference ITestResult interface as parameter to method which contains result of the test executed. Here we are using JavascriptExecutor class to pass result to Sauce labs using executeScript method .Here is the code for the same.

public void tearDown(ITestResult result) throws Exception{
String status = result.isSuccess() ? "passed" : "failed";
((JavascriptExecutor) driver).executeScript("sauce:job-result=" + status);
driver.quit();
}

When I was executing this I was getting below error

org.testng.TestNGException: 
Can inject only one of <ITestContext, XmlTest> into a @AfterTest annotated tearDown.

Error clearly says we can inject only one argument to AfterTest annotation. I really did not understand this at first place.When I try to dig down this issue. I found very helpful table in their docs. We can not pass reference of interface. Here is the table in their document

PC: TestNG_Doc

Here they have clearly mentioned that you can not pass ITestResult to AfterTest. So I changed BeforeTest and AfterTest annotations to BeforeMethod and AfterMethod and Guess what. I can see my test status on Sauce Pass.

public class Web_Execution_on_Sauce{public static final String USERNAME = "user_name";public static final String ACCESS_KEY = "access_key";public static final String Exe_URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.us-west-1.saucelabs.com:80/wd/hub";WebDriver driver;@BeforeMethod
public void sauceCapabilities() throws MalformedURLException {
EdgeOptions browserOptions = new EdgeOptions();
browserOptions.setCapability("platformName", "Windows 10");
browserOptions.setCapability("browserVersion", "91.0");
driver = new RemoteWebDriver(new URL(Exe_URL), browserOptions);
}
@Test
public void testGoogle(){
driver.get("https://www.google.com/");
driver.findElement(By.name("q"));
Assert.assertEquals(driver.getTitle(),"Google");

}
@AfterMethod
public void tearDown(ITestResult result) throws Exception{
String status = result.isSuccess() ? "passed" : "failed";
((JavascriptExecutor) driver).executeScript("sauce:job-result=" + status);
driver.quit();
}
}

Here is the status of Test

You can try and let me know if you are facing any challenges.

Happy Reading..!!

--

--

Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj