Search code examples
javamultithreadingseleniumwebdriver

The most optimized approach to handle common Webdriver functions library in multiple threads in parallel execution in Java


I have below class, which has reusable WebDriver functions.

I made all the methods as static since without creating an object ,from my Testcase classes I can access them. Also to keep in mind that " When synchronizing on a static method , the monitor belongs to the class."

public  class WebCommand {

    //Go to Url
    public  static void goToUrl(String url,WebDriver driver) {
        driver.get(url);
    }

}

So from my Testcase class, I call this method directly ,using a private static ThreadLocal driver = new ThreadLocal();

 WebCommand.goToUrl("https://blazedemo.com/login",driver);

Since I'm running on multiple threads ,parallelly, to thread safe the methods in WebCommand class what should be the best approach?

Even if I make method as non-static, as I know it won't guarantee the thread safety. Any help will be appreciated .

  public  void goToUrl(String url,WebDriver driver) {
            driver.get(url);
        }
//Then from TestCase class, call as WebCommand instance -new WebCommand ();

Solution

  • Since this method does not operate on shared data, only passed parameters, it is already thread safe, assuming that the driver instance is not shared between threads.