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