CosmoCode (Formerly TeachMeSelenium)

Selenium Grid | Running tests on SauceLabs’ cloud machines

In the Previous Tutorial, we learned how to run a test in the Selenium grid architecture. You must have realised that to use Selenium Grid, you need to maintain your own infrastructure for the nodes.

There are some providers who provide a Selenium Grid as a service in the cloud – Sauce Labs, BrowserStack and Testing Bot etc. In this tutorial, we’ll use the Selenium grid cloud infrastructure provided by Sauce Labs.

What is Sauce Labs

Sauce Labs is one of the leading Cloud-based testing providers that offer Selenium/manual tests to be run on its cloud. It has a large inventory of different platform-browser combinations. It means we can run the same test on different Operating Systems and against different browsers (or browser versions) with minimum change in the test. It even offers mobile testing on real devices and simulators.

Create an account on SauceLabs

Updating DesiredCapabilities to target SauceLabs cloud infrastructure

final String USERNAME = "teachmeselenium_saucelabs"; //Enter your own username
final String ACCESS_KEY = "6b785beb-cc60-40c8-8256-dfea7a77c94a"; //Enter your own accesskey
final String URL_STRING = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub";
URL url = new URL(URL_STRING);
USERNAME = "teachmeselenium_saucelabs" #Enter your own username
ACCESS_KEY = "6b785beb-cc60-40c8-8256-dfea7a77c94a" #Enter your own accesskey
URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"

You can write rest of the RemoteWebDriver code as you wrote for the traditional Selenium grid setup:

DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability("name", "My first SauceLabs test.");
caps.setPlatform(Platform.LINUX);
caps.setVersion("60");
WebDriver driver = new RemoteWebDriver(url, caps);
desired_caps = DesiredCapabilities.CHROME
desired_caps = DesiredCapabilities.CHROME.copy()
desired_caps['name'] = "My first Sauce test"
desired_caps['platform'] = "LINUX"
desired_caps['version'] = "60"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=URL)

Now we can write regular Selenium WebDriver code to navigate to a website and perform actions.

Complete Code

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;
public class TeachMeSelenium_SauceLabs {
 public static void main(String[] args) throws MalformedURLException {
 //Variables
 final String USERNAME = "teachmeselenium_saucelabs";
 final String ACCESS_KEY = "6b785beb-cc60-40c8-8256-dfea7a77c94a";
 final String URL_STRING = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub";
 //Construct URL
 URL url = new URL(URL_STRING);
 //Create DesiredCapabilities object with desired configurations
 DesiredCapabilities caps = DesiredCapabilities.chrome();
 caps.setCapability("version", "30.0");
 caps.setCapability("platform", "Windows 7");
caps.setCapability("name", "My first SauceLabs test.");
 //Create RemoteWebDriver object and pass URL alond with DesiredCapabilities object
 WebDriver driver = new RemoteWebDriver(url, caps);
 //Navigating to Automation Practice Homepage
 driver.get("https://cosmocode.io/automation-practice");
     //Get page title
 System.out.println("Here is the page title : " + driver.getTitle());
 driver.quit();
 }
}
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_caps = DesiredCapabilities.CHROME
desired_caps = DesiredCapabilities.CHROME.copy()
desired_caps['name'] = "My first Sauce test"
desired_caps['platform'] = "LINUX"
desired_caps['version'] = "60"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=URL)
driver.get("https://cosmocode.io/automation-practice/")
driver.maximize_window()
driver.find_element_by_id("firstname").send_keys("teachmeselenium")
driver.quit()

Verify the test running on Sauce Labs portal

As you run the test you won’t see the test running against a browser on the local machine. It is running on a machine hosted on Sauce Labs.

Oh my goodness! This page is so detailed. It even has a screenshot of the first page.

It is even more detailed. It has the screenshot of each step. Let’s try clicking on different steps and we would get its screenshot along with other execution details.

Hey, I took time in opening SauceLabs’ account page and execution was completed. I could not see the test in action. How can I believe if the execution really happened?

It happened, indeed. Please click on ‘Screencast’ tab and it has recorded Video of test execution. Play the video and be proud of SauceLabs. 

Why the test status is showing as Finished. It would have been more helpful had it display it as PASS/FAIL?

You can update the details a test (Job, in SauceLabs’s terms), including its status by using REST API. SauceLabs has provided a Java library that can be used in this case. I would appreciate if you can do some google and implement it yourself. Please share your solution in comments.

In the Next Tutorial, we’ll run the tests against another IAAS provider.

Exit mobile version