Make Selenium Easy

Problems in Parallel Execution With Static WebDriver

Static variables in Javadoc is defined as:-

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

Many Selenium WebDriver professionals use static class variables frequently and mostly make WebDriver a static reference variable. The advantages they get from static WebDriver is that they do not need to declare and initialize local WebDriver instances in multiple classes to keep same reference and can access WebDriver value from a single place. It also helps them to ignore NullPointerException for driver instance which is problem for many beginners in Selenium WebDriver. Whatever browser they launch, to use same reference without any problem they make WebDriver static. This post will use a static WebDriver reference variable and show how it creates a problem in running scripts in parallel but this applies for all static reference you use in your project.

Let’s see example codes with static WebDriver:-

I have created a class named “WebDriverFactoryStatic” where I have a static WebDriver reference and a static method “setDriver” which initialize a chrome browser i.e. initialize static WebDriver reference. Now you can use this static WebDriver reference using its class name i.e. WebDriverFactoryStatic. You could have WebDriver reference as private and put a getter method.

Class WebDriverFactoryStatic :-

There are two simple TestNG classes with two @Test methods and a @BeforeClass and a @AfterClass configuration methods in each class. In @BeforeClass I am initializing browser so that both tests of a class runs on same browser. Clearing cookies as a last step in @Test methods. In @AfterClass method I am closing a browser. See the code below:-

Test Class TestOneWithStaticWebDriver :-

Test Class TestTwoWithStaticWebDriver :-

Let’s run above TestNG classes linearly using TestNG xml as below:-

Output:

We can see tests were run one by one successfully using one thread in linear execution. Execution started from TestOneWithStaticWebDriver class and a browser was initialized in BeforeClass method. Both tests of this class was executed one after another. After executing test methods initialized browser was closed in AfterClass method. Similar sequence of execution was done for TestTwoWithStaticWebDriver class.

Time is money. Let’s save time and run tests in parallel. Let’s run TestNG classes in parallel with thread count as 2. Two different threads will be executing two TestNG classes.

TestNG XML with parallel setup:-

Output:-

We were expecting that one thread will execute all @Test methods of first TestNG class and second thread will execute all @Test methods of another TestNG class. But you can see in above output it did not happen in expected way. You see test method and configuration method failures If you rerun you may see different output with a open browser left. This happened because of static WebDriver. Static WebDriver reference was shared by both threads.

First thread initialized a browser and set a value to static WebDriver reference. Second thread initialized another browser and set a new value to the same static WebDriver reference and this will impact value set by first thread as it is a static. If you are confused with static concept in Java please refer Javadoc here.

Two threads call “setDriver” method of WebDriverFactoryStatic class simultaneously but both uses same static reference of WebDriver as it is static. Thread one sets value of driver as X which was reset by Thread second as Y. Now both threads will have driver reference as Y only and X is abandoned now. Both threads wanted to close same browser that is the reason there is one configuration method failure as once closed another thread will not find session. Browser was closed already so last test did not get a browser for execution.

Once second thread sets final value for static WebDriver, both threads started working on same browser itself. Another browser is left unattended. If you observe output carefully, you will find tests for printing title of loaded URLs is also not from same class. A thread should have loaded Google and Facebook and another thread should have loaded Flipkart and Myntra but it was messed because of static reference. Both threads worked on same instance.

Okay, you don’t believe on my words how execution went wrong. Let’s print WebDriver reference in tests and observe output:-

TestOneWithStaticWebDriver:

TestTwoWithStaticWebDriver :-

Output:-

Now you see the printed driver reference. First initialization of driver reference was overwritten by second initialization of driver reference and both threads started working on same browser instance.

We will see solution of above problem in next post.

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

1 thought on “Problems in Parallel Execution With Static WebDriver

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.

Problems in Parallel Execution With Static WebDriver

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