Parallel testing and Thread Safe driver for testNG

In the recent times cicd is being the buzz word. Keeping this in mind the automation testing has been evolved to support this very need. Since after the build automation test suite need to be executed, the tests run time being the critical for whole build time. Most often because the tests run longer only prioritized tests will run in build generation cycle.

In this context its useful to know about parallel testing to reduce the turn around time. Let’s first look at parallel testing in testNg and selenium.

TestNg gives option to run parallel tests in threads. We can define how many threads we want to kick off simultaneously.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" thread-count="2">
 <test name="Test" thread-count="3">
 <classes>
 <class name="com.tests.testcases"/>
 </classes>
 </test> <!-- Test -->
</suite> <!-- Suite -->

<suite name=”Suite” parallel=”methods” thread-count=”2″>

At Suite level the attribute parallel=”methods” added to run tests simultaneously.

The highlighted thread-count attribute determines how many threads testNG framework wants to kick off, these are parallel process running with different thread ids.

<test name=”Test” thread-count=”3″>

At test level also we have thread-count attribute and it is set to 3 in above example.

This means, three tests can run in parallel provided if we have enough threads.

Although you give thread-count is 2 at suite level and 3 at test level only two tests will run simultaneously, the third test will run only when first thread is freed up.

So batches of 2 tests will run until all tests completes.

The most common issue here being creating driver object which is thread-safe

And the other issue is being managing logging, as several threads trying to write to the same report. At the end we want single report for all tests.

Let us look at above mentioned two concepts. In java programming we can create driver which is thread safe with given command.

protected ThreadLocal<WebDriver> wbdriver = new ThreadLocal<WebDriver>();

ThreadLocal class in java is helpful in creating objects which are thread safe. It has get and set methods.  Following is its implementation

//chrome driver (options describe the desired capabilities for chrome driver.)
//you can just use string url instead of getting from properties file
String url = "https://google.com";
wbdriver.set(new ChromeDriver(options));
wbdriver.get().manage().window().maximize(); 
wbdriver.get().get(url);

in the above example, wbdriver holds the driver created, it is protected by ThreadLocal class for thread safety.

This way if you create driver, each test will run by its own thread. The only challenge being, the results will be hard to interpret when lot of concurrent driver processes are doing job simultaneously.

Lets discuss about synchronizing the logs with ExtentReports in next posts.

Extent report logging while parallel testing

 

 

 

2 thoughts on “Parallel testing and Thread Safe driver for testNG

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s