Make Selenium Easy

ThreadLocal Static WebDriver For Parallel Execution

In previous post, we learnt How a static WebDriver reference created problems in parallel execution. We will see solution of those problems in this post.

Please note I am exclusively using a static WebDriver reference but similar solution will be applicable for any static reference.

Solution I have already discussed in my previous post. We need to use ThreadLocal so that each thread will have their own reference to class variables including static class variables. Read more about ThreadLocal at my blog here.

Let’s declare a static WebDriver as ThreadLocal. I have created simple methods to set, get and remove driver.

Class WebDriverFactoryStaticThreadLocal :-

Little changes to TestNG test classes to use driver instance using getter method and calling close method from WebDriverFactoryStaticThreadLocal class.

Class TestOneWithStaticWebDriver

Class TestTwoWithStaticWebDriver :-

Let’s execute TestNG classes in parallel using TestNG xml as below:-

Output:-

Observe output and find how driver reference were kept separately for both threads and how ThreadLocal helped in that.

Let’s dig further little more.

Above we ran TestNG class in parallel. What will happen if we run @Test methods in parallel.

Let’s run by changing parallel attribute as below:-

Output:-

Again something unexpected happen and one test could not find a browser to execute on. Reason behind this is that @BeforeClass and @AfterClass will not be executed for each @Test annotated methods if we run them in parallel. We were expecting that since we are running @Test methods in parallel, @BeforeClass and @AfterClass for each test methods will be called but it does not work in that way. You may ask what will happen if we set thread count as 4 which is equal to test method. Try that as well. You will see two threads will have browser to execute test but another two threads will have null driver reference.

Output:-

Since tests execution requires browsers, so we need to change BeforeClass and AfterClass to BeforeMethod and AfterMethod. Change it and run.

Output:-

You can download code form this repo.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

2 thoughts on “ThreadLocal Static WebDriver For Parallel Execution

  1. Hi Amod can you explain regarding failure when it’s @BeforeClass @AfterClass Scenario with parallel = methods. Step by Step .

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

ThreadLocal Static WebDriver For Parallel Execution

by Amod Mahajan time to read: 9 min
2
%d bloggers like this: