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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

5 thoughts on “ThreadLocal Static WebDriver For Parallel Execution

  1. Hi Amod,

    I am using docker selenium grid and running our script parallelly by test tag but 1 test case is getting failed. Can you please help me?
    Here is the testng file-

    Here is the driver launch code-

    protected static ThreadLocal driver = new ThreadLocal();

    public static void intializeRemoteBrowser(String browserType) throws MalformedURLException {
    DesiredCapabilities cap = new DesiredCapabilities();
    if (browserType.equalsIgnoreCase(“chrome”)) {
    cap.setBrowserName(BrowserType.CHROME);
    WebDriverManager.chromedriver().setup();
    System.out.println(“########### TEST CASE EXECUTION STARTED ON ==>” + browserType+” : “+ Thread.currentThread().getId());
    }

    else if (browserType.equalsIgnoreCase(“firefox”)) {
    cap.setBrowserName(BrowserType.FIREFOX);
    WebDriverManager.firefoxdriver().setup();
    System.out.println(“########### TEST CASE EXECUTION STARTED ON ==>” + browserType+” : “+ Thread.currentThread().getId());
    }

    public static WebDriver getDriver() {
    return driver.get();
    }

    public static void closeBrowser() {
    driver.get().close();
    driver.remove();
    System.out.println(“###############TEST CASES EXECUTION ENDED###############”+” : “+ Thread.currentThread().getId());
    }

  2. Hi Amod, can I use the same code in case of multiple browsers as well? My existing code has else if statements for different browsers.

  3. 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.