Selenium cross-browser tests on the cloud

The ability to automate Selenium tests on the cloud is quite interesting, with instant access to real devices. Sauce Labs, BrowserStack, and TestingBot are the leading web-based tools used for cross-browser compatibility checking. These tools contain unique test automation features, such as diagnosing failures through screenshots and video, executing parallel tests, running Appium mobile automation tests, executing tests on internal local servers, and so on.

SauceLabs

SauceLabs is the standard Selenium test automation web app to do cross-browser compatibility tests on the cloud. It lets you automate tests in your favorite programming languages, using test frameworks such as JUnit, TestNG, Rspec, and many more. SauceLabs cloud tests can also be executed from the Selenium Builder IDE interface. Check for the available SauceLabs devices, OS, and platforms at https://saucelabs.com/platforms.

Access the website from your web browser, log in, and obtain the Sauce username and Access Key. Make use of the credentials to drive tests over the SauceLabs cloud. SauceLabs creates a new instance of the virtual machine while launching the tests. Parallel automation tests are also possible using SauceLabs. The following is a Java program to run tests over the SauceLabs cloud:

package packagename;

import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.lang.reflect.*;

public class saucelabs {

  private WebDriver driver;

  @Parameters({"username", "key", "browser", "browserVersion"})
  @BeforeMethod
  public void setUp(@Optional("yourusername") String username,
                      @Optional("youraccesskey") String key,
                      @Optional("iphone") String browser,
                      @Optional("5.0") String browserVersion,
                      Method method) throws Exception {

  // Choose the browser, version, and platform to test
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setBrowserName(browser);
  capabilities.setCapability("version", browserVersion);
  capabilities.setCapability("platform", Platform.MAC);
  capabilities.setCapability("name", method.getName());
  // Create the connection to SauceLabs to run the tests
  this.driver = new RemoteWebDriver(
  new URL("http://" + username + ":" + key + "@ondemand.saucelabs.com:80/wd/hub"), capabilities);
  }

  @Test
  public void Selenium_Essentials() throws Exception {
    // Make the browser get the page and check its title
    driver.get("http://www.google.com");
    System.out.println("Page title is: " + driver.getTitle());
    Assert.assertEquals("Google", driver.getTitle());
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("Selenium Essentials");
    element.submit();
  }
  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();
  }
}

SauceLabs has a setup similar to BrowserStack on test execution and generates detailed logs. The breakpoints feature allows the user to manually take control over the virtual machine and pause tests, which helps the user to investigate and debug problems. By capturing JavaScript's console log, the JS errors and network requests are displayed for quick diagnosis while running tests against the Google Chrome browser.

SauceLabs

BrowserStack

BrowserStack is a cloud-testing web app to access virtual machines instantly. It allows users to perform multi-browser testing of their applications on different platforms. It provides a setup similar to SauceLabs for cloud-based automation using Selenium.

Access the site https://www.browserstack.com from your web browser, log in, and obtain the BrowserStack username and Access Key. Make use of the obtained credentials to drive tests over the BrowserStack cloud.

For example, the following generic Java program with TestNG provides a detailed overview of the process that runs on the BrowserStack cloud. Customize the browser name, version, platform, and so on, using capabilities. Let's see the Java program we just talked about:

package packagename;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class browserstack {

  public static final String USERNAME = "yourusername";
  public static final String ACCESS_KEY = "youraccesskey";
  public static final String URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@hub.browserstack.com/wd/hub";

  private WebDriver driver;

  @BeforeClass
  public void setUp() throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Firefox");
    caps.setCapability("browser_version", "23.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "XP");
    caps.setCapability("browserstack.debug", "true"); 
    //This enable Visual Logs

    driver = new RemoteWebDriver(new URL(URL), caps);
  }

  @Test
  public void testOnCloud() throws Exception {
    driver.get("http://www.google.com");
    System.out.println("Page title is: " + driver.getTitle());
    Assert.assertEquals("Google", driver.getTitle());
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("seleniumworks");
    element.submit();
  }

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

The app generates and stores test logs for the user to access anytime. The generated logs provide a detailed analysis with step-by-step explanations. To enhance the test speed, run parallel Selenium tests on the BrowserStack cloud, but the automation plan has to be upgraded to increase the number of parallel test runs.

TestingBot

TestingBot also provides a setup similar to BrowserStack and SauceLabs for cloud-based cross-browser test automation using Selenium. It records a video of the running tests to analyze problems and debug them. Additionally, it provides support to capture the screenshots on test failure. To run local Selenium tests, it provides an SSH tunnel tool that lets you run tests against local servers or other web servers. TestingBot uses Amazon's cloud infrastructure to run Selenium scripts in various browsers.

Access the site https://testingbot.com/, log in, and obtain the Client Key and Client Secret from your TestingBot account. Make use of the credentials to drive tests over the TestingBot cloud.

Let's see an example Java test program with TestNG, using the Eclipse IDE that runs on the TestingBot cloud:

package packagename;

import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class testingbot {
  private WebDriver driver;

  @BeforeClass
  public void setUp() throws Exception {
    DesiredCapabilitiescapabillities = DesiredCapabilities.firefox();
    capabillities.setCapability("version", "24");
    capabillities.setCapability("platform", Platform.WINDOWS);
    capabillities.setCapability("name", "testOnCloud");
    capabillities.setCapability("screenshot", true);

	capabillities.setCapability("screenrecorder", true);
    driver = new RemoteWebDriver(
    new URL
	("http://ClientKey:ClientSecret@hub.testingbot.com:4444/wd/hub"),
      capabillities);
  }

  @Test
  public void testOnCloud() throws Exception {
    driver.get
	  ("http://www.google.co.in/");
    driver.findElement(By.id("q")).clear();
    WebElement element = driver.findElement(By.id("q"));
    element.sendKeys("selenium");
    Assert.assertEquals("selenium - Google Search", driver.getTitle());
  }

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

Click on the Tests tab to check the log results. The logs are well organized with test steps, screenshots, videos, and a summary. Screenshots are captured at each and step to make the tests more precise, as follows:

capabillities.setCapability("screenshot", true); // screenshot
capabillities.setCapability("screenrecorder", true); // video capture

TestingBot provides a unique feature by scheduling and running tests directly from the site. The tests can be scheduled to repeat any number of times on a daily or weekly basis. It's even more accurate on scheduling the test start time. You will be apprised of test failures with an alert through e-mail, an API call, an SMS, or a Prowl notification. This feature enables error handling to rerun failed tests automatically as per the user settings.

TestingBot

Launch the Selenium IDE, record tests, and save the test case or test suite in default format (HTML). Access the https://testingbot.com/ URL from your web browser and click on the Test Lab tab. Now, try to upload the already-saved Selenium test case, select the OS platform and browser name and version. Finally, save the settings and execute tests. The test results are recorded and displayed under Tests.

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

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