Chapter 2. Selenium WebDriver Cross-browser Tests

The term cross-browser testing can be applied to both multi-browser testing and compatibility testing. Testing the web application with multiple web browsers is defined as cross-browser testing. A lack of cross-browser testing results in layout and functionality issues. Manually testing an application needs a lot of human effort and time to finish a complex job, but automated tests are carried out to avoid such issues. In general, most of the cross-browser issues are generated while rendering web page elements, which results in a functional and UI-based mess. Selenium WebDriver provides excellent support for automating test cases with the most popular browsers, using their own drivers. Selenium cross-browser tests can also be automated on the cloud using web application tools, such as SauceLabs, BrowserStack, and TestingBot.

In this chapter, we will cover the following topics:

  • Selenium WebDriver compatibility tests
  • Selenium cross-browser tests on cloud
  • Selenium headless browser testing
  • Switching user agents
  • Tests on specific Firefox versions
  • Tests from custom Firefox profile
  • Tests from custom Chrome profile

Selenium WebDriver compatibility tests

Selenium WebDriver handles browser compatibility tests on almost every popular browser, including Chrome, Firefox, Internet Explorer, Safari, and Opera. In general, every browser's JavaScript engine differs from the others, and each browser interprets the HTML tags differently. The WebDriver API drives the web browser as the real user would drive it. By default, FirefoxDriver comes with the selenium-server-standalone.jar library added, but for Chrome, IE, and Opera, there are libraries that need to be added or instantiated externally.

Let's see how we can instantiate each of the following browsers through its own driver:

  • Mozilla Firefox: The selenium-server-standalone library is bundled with FirefoxDriver to initialize and run tests in a Firefox browser. FirefoxDriver is added to the Firefox profile as a file extension on starting a new instance of FirefoxDriver. Please check the Firefox versions and its suitable drivers at http://selenium.googlecode.com/git/java/CHANGELOG.

    The following is the code snippet to kick start Mozilla Firefox:

    WebDriver driver = new FirefoxDriver();
  • Google Chrome: Unlike FirefoxDriver, the ChromeDriver is an external library file that makes use of WebDriver's wire protocol to run Selenium tests in a Google Chrome web browser. The following is the code snippet to kick start Google Chrome:
    System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe"); 
    WebDriver driver = new ChromeDriver();

    Note

    To download ChromeDriver, refer to http://chromedriver.storage.googleapis.com/index.html.

  • Internet Explorer: IEDriverServer is an executable file that uses the WebDriver wire protocol to control the IE browser in Windows. Currently, IEDriverServer supports the IE versions 6, 7, 8, 9, and 10. The following code snippet helps you to instantiate IEDriverServer:
    System.setProperty("webdriver.ie.driver","C:\IEDriverServer.exe");
    DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
    dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    WebDriver driver = new InternetExplorerDriver(dc);

    Note

    To download IEDriverServer, refer to http://selenium-release.storage.googleapis.com/index.html.

  • Apple Safari: Similar to FirefoxDriver, SafariDriver is internally bound with the latest Selenium servers, and starts the Apple Safari browser without any external library. SafariDriver supports Safari browser version 5.1.x and runs only on Mac. For more details, refer to http://elementalselenium.com/tips/69-safari.

    The following code snippet helps you to instantiate SafariDriver:

    WebDriver driver = new SafariDriver();
  • Opera: OperaPrestoDriver (formerly called OperaDriver) is available only for Presto-based Opera browsers. Currently, it does not support Opera versions 12.x and above. However, the recent releases (Opera 15.x and above) of Blink-based Opera browsers are handled using OperaChromiumDriver. For more details, refer to https://github.com/operasoftware/operachromiumdriver.

    The following code snippet helps you to instantiate OperaChromiumDriver:

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("opera.binary", "C://Program Files (x86)//Opera//opera.exe");
    capabilities.setCapability("opera.log.level", "CONFIG");
    WebDriver driver = new OperaDriver(capabilities);

    Note

    To download OperaChromiumDriver, refer to https://github.com/operasoftware/operachromiumdriver/releases.

TestNG

TestNG (Next Generation) is one of the most widely used unit-testing frameworks implemented for Java. It runs Selenium-based browser compatibility tests with the most popular browsers. The Eclipse IDE users must ensure that the TestNG plugin is integrated with the IDE manually. However, the TestNG plugin is bundled with IntelliJ IDEA as default. The testng.xml file is a TestNG build file to control test execution; the XML file can run through Maven tests using POM.xml with the help of the following code snippet:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.2</version>
    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile> 
      </suiteXmlFiles>
    </configuration>
  </plugin>

To create a testng.xml file, right-click on the project folder in the Eclipse IDE, navigate to TestNG | Convert to TestNG, and click on Convert to TestNG, as shown in the following screenshot:

TestNG

The testng.xml file manages the entire test. It acts as a mini data source by passing the parameters directly into the test methods. The location of the testng.xml file is shown in the following screenshot:

TestNG

As an example, create a Selenium project (for example, Selenium Essentials) along with the testng.xml file, as shown in the previous screenshot. Modify the testng.xml file with the following tags:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="3" parallel="tests" thread-count="5">

  <test name="Test on Firefox">
    <parameter name="browser" value="Firefox" />
      <classes>
        <class name="package.classname" />
      </classes>
  </test>

  <test name="Test on Chrome">
    <parameter name="browser" value="Chrome" />
    <classes>
      <class name="package.classname" />
    </classes>
  </test>

  <test name="Test on InternetExplorer">
    <parameter name="browser" value="InternetExplorer" />
    <classes>
      <class name="package.classname" />
    </classes>
  </test>

  <test name="Test on Safari">
    <parameter name="browser" value="Safari" />
    <classes>
      <class name="package.classname" />
    </classes>
  </test>

  <test name="Test on Opera">
    <parameter name="browser" value="Opera" />
    <classes>
      <class name="package.classname" />
    </classes>
  </test>
</suite>
<!-- Suite -->

Download all the external drivers except FirefoxDriver and SafariDriver, extract the zipped folders, and locate the external drivers as mentioned in the preceding snippets for each browser.

Note

Refer to Chapter 1, The Selenium IDE, to learn how to set the path on different platforms.

The following Java snippet will explain how you can get parameters directly from the testng.xml file and how you can run cross-browser tests as a whole:

@BeforeTest
@Parameters({"browser"})
public void setUp(String browser) throws MalformedURLException {
  if (browser.equalsIgnoreCase("Firefox")) {
    System.out.println("Running Firefox");
    driver = new FirefoxDriver();
  } else if (browser.equalsIgnoreCase("chrome")) {
    System.out.println("Running Chrome");
    System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
    driver = new ChromeDriver();
  } else if (browser.equalsIgnoreCase("InternetExplorer")) {
    System.out.println("Running Internet Explorer");
    System.setProperty("webdriver.ie.driver", "C:\IEDriverServer.exe");
    DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
    dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
   //If IE fail to work, please remove this line and remove enable protected mode for all the 4 zones from Internet options
    driver = new InternetExplorerDriver(dc);
  } else if (browser.equalsIgnoreCase("safari")) {
    System.out.println("Running Safari");
    driver = new SafariDriver();
  } else if (browser.equalsIgnoreCase("opera")) {
    System.out.println("Running Opera");
   // driver = new OperaDriver();       --Use this if the location is set properly--
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("opera.binary", "C://Program Files(x86)//Opera//opera.exe");
    capabilities.setCapability("opera.log.level", "CONFIG");
    driver = new OperaDriver(capabilities);
  }
}

SafariDriver is not yet stable. A few of the major issues in SafariDriver are as follows:

  • SafariDriver won't work properly in Windows
  • SafariDriver does not support modal dialog box interaction
  • You cannot navigate forwards or backwards in the browser history through SafariDriver
..................Content has been hidden....................

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