Book Image

Selenium Framework Design in Data-Driven Testing

By : Carl Cocchiaro
Book Image

Selenium Framework Design in Data-Driven Testing

By: Carl Cocchiaro

Overview of this book

The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile device, will also work on all other browsers and mobile devices. Selenium supports all major development languages which allow it to be tied directly into the technology used to develop the applications. This guide will provide a step-by-step approach to designing and building a data-driven test framework using Selenium WebDriver, Java, and TestNG. The book starts off by introducing users to the Selenium Page Object Design Patterns and D.R.Y Approaches to Software Development. In doing so, it covers designing and building a Selenium WebDriver framework that supports both Browser and Mobile Devices. It will lead the user through a journey of architecting their own framework with a scalable driver class, Java utility classes, JSON Data Provider, Data-Driven Test Classes, and support for third party tools and plugins. Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage third party grids in the Cloud like SauceLabs. Other topics covered include designing abstract base and sub-classes, inheritance, dual-driver support, parallel testing, testing multi-branded applications, best practices for using locators, and data encapsulation. Finally, you will be presented with a sample fully-functional framework to get them up and running with the Selenium WebDriver for browser testing. By the end of the book, you will be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver.
Table of Contents (15 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface

Multithreading support for parallel and distributed testing


In order to leverage the TestNG parallel testing features, users must create a separate thread for each driver instance to control event processing requests. This is done in Java using the ThreadLocal<T> class. By declaring variables with this class, each thread has its own initialized copy of the variable, and can return specifics of that session. The following variables are declared in the singleton driver class, and have getter and setter methods to retrieve the session ID, browser, platform, and version:

private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
private ThreadLocal<AppiumDriver<MobileElement>> mobileDriver = new ThreadLocal<AppiumDriver<MobileElement>>();

private ThreadLocal<String> sessionId = new ThreadLocal<String>();
private ThreadLocal<String> sessionBrowser = new ThreadLocal<String>();
private ThreadLocal<String> sessionPlatform = new ThreadLocal<String>();
private ThreadLocal<String> sessionVersion = new ThreadLocal<String>();

Key points:

  • The set methods are called by the setDriver methods during instantiation of the driver.
  • The get methods are stored in the singleton driver class and can be called after the driver is created. Users can retrieve session parameters for each specific instance of the driver that is running.
  • To leverage the separate instances during parallel test runs, TestNG suite parameters must also be used. For example:
<suite name="Parallel_Test_Suite" preserve-order="true" parallel="classes" thread-count="10">

These are examples of the getter methods for the driver class:

/**
 * getSessionId method gets the browser or mobile id
 * of the active session
 *
 * @return String
 */
public String getSessionId() {
    return sessionId.get();
}

/**
 * getSessionBrowser method gets the browser or mobile type
 * of the active session
 *
 * @return String
 */
public String getSessionBrowser() {
    return sessionBrowser.get();
}

/**
 * getSessionVersion method gets the browser or mobile version
 * of the active session
 * 
 * @return String
 */
public String getSessionVersion() {
    return sessionVersion.get();
}

/**
 * getSessionPlatform method gets the browser or mobile platform
 * of the active session
 *
 * @return String
 */
public String getSessionPlatform() {
    return sessionPlatform.get();
}

How to set:

The session ID, browser, version, and platform can be set during driver creation in the setDriver methods as follows:

getEnv = "local";
getPlatform = platform;

if ( browser.equalsIgnoreCase("iphone") ||
     browser.equalsIgnoreCase("android") ) {

    sessionId.set(((IOSDriver<MobileElement>)
    mobileDriver.get()).getSessionId().toString());

    sessionId.set(((AndroidDriver<MobileElement>)
    mobileDriver.get()).getSessionId().toString());

    sessionBrowser.set(browser);
    sessionVersion.set(caps.getCapability("deviceName").toString());
    sessionPlatform.set(getPlatform);
}

else {
    sessionId.set(((RemoteWebDriver) webDriver.get())
             .getSessionId().toString());

    sessionBrowser.set(caps.getBrowserName());
    sessionVersion.set(caps.getVersion());
    sessionPlatform.set(getPlatform);
}