Using Cloud tools for cross-browser testing running tests in the Cloud

We set up a local Grid in previous recipes to run the tests for cross-browser testing. This requires setting up physical or virtual machines with different browsers and operating systems. There are costs and efforts needed get 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, and so on. Not everybody can afford these costs and 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. 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.

In this section, 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.

Getting ready

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 user name 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 Sauce Labs dashboard after the login:

Getting ready

How to do it...

Let's modify the test we created earlier to run with grid, and add steps to run this test on Sauce Labs cloud. We need to add the Sauce username and access key to the test and change the Grid address to Sauce's Gird address, passing the username and access key, as shown in the following code example:

package com.secookbook.examples.chapter13;

import static org.junit.Assert.*;
import static org.testng.Assert.assertEquals;

import java.net.URL;
import java.text.MessageFormat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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;

public class SauceTest {

  WebDriver driver;

  @Before
  public void setUp() throws Exception {
    String SAUCE_USER = "<your username>";
    String SAUCE_KEY = "<your key>";

    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");

  }

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

  @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 bmiCategory = driver.findElement(By.name("bmi_category"));
    assertEquals(bmiCategory.getAttribute("value"), "Normal");
  }

}

Tip

You can get a list of platforms supported on Sauce Labs at https://saucelabs.com/platforms.

While running the test, it will connect to Sauce Lab's hub and request the desired operating system and bowser configuration. Sauce assigns a virtual machine for our test to run on a given configuration. We can monitor this run on the Sauce dashboard, as shown in the following screenshot:

How to do it...

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

How to do it...

Tip

You can also test applications 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.226.177.85