Make Selenium Easy

Are Your Selenium Scripts Ready For Parallel Execution?

If I ask you how can you run your Selenium scripts in parallel (Assuming you are using Selenium – Java with TestNG) , probably you will answer just add a “parallel” attribute with a desired value (methods, classes, tests or instances) and set a thread count in TestNG XML and you are all set to run your scripts in parallel.

Correct answer. Now ask a question yourself that “Are your selenium scripts ready for parallel run?

Let’s see some basics concepts about Parallel execution in TestNG.

  • We may have multiple @Test annotated methods (tests) in a TestNG class.
  • Similarly we can have multiple TestNG classes with multiple @Test annotated methods in it.
  • We can have multiple “test” tags consisting multiple TestNG classes in a TestNG suite.
  • We can have multiple suites i.e. testng.xml files as well.
  • We can have multiple methods in an instance (Factory method)

We can run out tests in parallel using TestNG in multiple ways :-

  1. Run @Test methods in parallel
  2. Run TestNG classes in parallel
  3. Run <test> tags of TestNG.xml in parallel
  4. Run instances in parallel
  5. Run testng.xml files in parallel

I would like to give extract from Official TestNG document how parallel execution is performed in TestNG here. They have perfectly summarized clearly.

  • parallel=”methods”: TestNG will run all your test methods (@Test annotated methods) across suite in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
  • parallel=”tests”: TestNG will run all the test methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
  • parallel=”classes”: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
  • parallel=”instances”: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

If we have 10 @Test methods and we want to run these methods in parallel and we provide thread count as 2, then these 2 threads will divide test methods between them based on availability. If T1 picks a method say M1 for execution and T2 picks a method say M2 for execution, upcoming method will be picked by thread which finishes execution first.

Let’s implement a simple parallel run mechanism using Threads concept in Java.

You are writing automated scripts for an e-Commerce application and you perform different tests like placing an order, cancelling an order etc. As a part of prerequisite, you are creating a user before starting actual tests. You have created a utility to register a user. Your different tests will be using same resource or method to register a user.


Now creating two threads which are sharing resource as below:-

Let’s run the and observe output:-

Let’s understand how did execution go step by step:-

  1. An instance of RegisteredUser were created.
  2. Same instance of RegisteredUser were passed to two different threads.
  3. First thread ( Thread ID 10 above) started execution first and started execution of createUser() method. It checked default value of class variable “registeredUserName” which was currently “No Value”. Now creates a user and set value of “registeredUserName”.
  4. Second Thread (Thread ID 11 above) also started execution just after Thread 1 , and started execution of createUser() method. It also checked default value of class variable “registeredUserName” which was NO more as “No Value”. In stead of it had value set by Thread 1. Thread 2 also created a user and set value of “registeredUserName”.
  5. Both threads waited for 5 seconds simultaneously and fetched registeredUserName value. Expectation is that both threads will print registered user name created by them but they printed the user name registered by last thread.


Class variables are shared by threads created from same object.

In above example, we have two threads but they shared class variable “registeredUserName ” and both worked on same reference which leads to data inconsistently.

So are you making sure that your tests are thread safe and data generated by a test running by a thread should not be used by another test running by another thread?

Let’s see similar example in a Selenium script which we generally do.

We have a class named “” which initializes a chrome browser and give you a setter method and a getter method to set and get driver instance respectively.

A class named TestCases which contains two normal TestNG tests. This class has a class variable of type “WebDriverFactory” which is used to set and get driver instance in tests. For each test BeforeMethod will be called to initialize driver and AfterMethod will close browser.

Let’s run methods in parallel using testng xml:-

You should observe while running that it is not running as we expected. It will launch two browsers but both tests may be executed in same browser and another will be left unattended. You can play around by placing sleep before starting and may observe different output. You know the reason well now.

Console Output:-

We can solve above problem by converting class variable in to local as below:-

Run about tests in parallel and you can see expected output:-

But above solution is not optimal as you can not setup prerequisite and post requisite. Also states can not be shared between tests.

We will solve this problem using ThreadLocal concept which is recommended as well. We will see that in next post.

You can clone the above example from my repo.

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

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

6 thoughts on “Are Your Selenium Scripts Ready For Parallel Execution?

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.

Are Your Selenium Scripts Ready For Parallel Execution?

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