Using cloud-based grids for cross-browser testing

To set up a Selenium Grid for cross-browser testing, you need to set up physical or virtual machines with different browsers and operating systems. This requires an investment in the required hardware, software, and support to run the test lab. You also need to put in effort to keep this infrastructure updated with the latest versions and patches. Not everybody can afford these costs and the effort.

Instead of investing and setting up a cross-browser test lab, you can easily outsource a virtual test lab to a third-party cloud provider for cross-browser testing. The Sauce Labs and BrowserStack are leading cloud-based cross-browser testing cloud providers. Both of these have support for over 400 different browser and operating system configurations, including mobile and tablet devices, and support running Selenium WebDriver tests in their cloud.

Here, we will set up and run a test in the Sauce Labs cloud. The steps are similar if you want to run tests with BrowserStack.

Let's set up and run a test with Sauce Labs. You need a free Sauce Labs account, to begin with. Register for a free account on Sauce Labs at https://saucelabs.com/, and get the username and access key. Sauce Labs provides all the needed hardware and software infrastructure to run your tests in the cloud. You can get the access key from the Sauce Labs dashboard after you log in from the My Account page:

Let's create a new test to execute on the Sauce Labs cloud. We need to add the Sauce username and access key to the test, and change the Grid address to the Sauce Labs Grid address instead of the local Selenium Grid, as shown in the following code example:

public class BmiCalculatorTest {

WebDriver driver;

@BeforeMethod
public void setUp() throws Exception {

String SAUCE_USER = "upgundecha";
String SAUCE_KEY = "5768f2a9-33be-4ebd-9a5f-3826d7c38ec9";

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platform", "OS X 10.9");
caps.setCapability("browserName", "Safari");
caps.setCapability("name", "BMI Calculator Test");
driver = new RemoteWebDriver(
new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub'",
SAUCE_USER, SAUCE_KEY)), caps);
driver.get("http://bit.ly/1zdNrFZ");

}

@Test
public void testBmiCalc() {
WebElement height = driver.findElement(By.name("heightCMS"));
height.sendKeys("181");

WebElement weight = driver.findElement(By.name("weightKg"));
weight.sendKeys("80");

WebElement calculateButton = driver.findElement(By.id("Calculate"));
calculateButton.click();

WebElement bmi = driver.findElement(By.name("bmi"));
assertEquals(bmi.getAttribute("value"), "24.4");

WebElement bmi_category = driver.findElement(By.name("bmi_category"));
assertEquals(bmi_category.getAttribute("value"), "Normal");
}

@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}

When you execute the test, it will connect to Sauce Lab's hub and request the desired operating system and browser configuration. The sauce Labs cloud-management software automatically assigns a virtual machine for our test to run on a given configuration. We can monitor this run on a dashboard, as shown in the following screenshot:

We can further drill down into the session and see exactly what happened during the run. It provides details of the Selenium commands, screenshots, logs, and a video of the execution on multiple tabs, as shown in the following screenshot:

Selenium details window

You can also test applications that are securely hosted on internal servers, by using the Sauce Connect utility. sauce connect creates a secure tunnel between your machine and the Sauce cloud.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.158.184