selenium-webdrivertestngtestng.xmlparallel-execution

Cannot run parallel execution in testng due to static WebDriver in the testng framework


I created a framework using testng and used driver as a static variable, but I'm unable to run parallel execution in testng, as I used static to create the driver. Now I want to change the driver to non static, but am confused on how to change the driver to non static and use it. Below is my framework flow.

public TestBase{
    public static WebDriver driver; // declared the webdriver as static in this class
    // below there are few methods(click, type etc) which can be used generally across different applications
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_CommonMethods extends TestBase{
    // have some common methods specific to lic application
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_AutoMethods extends LIC_CommonMethods{
    // have some methods which are specific to lic 
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_AutoExecution extends TestBase {
    @before Test
    public static void initiateBrowser(){
    // Browser initiation code will be present here
}

@DataProvider
public Iterator<Object[]>  getData(){
    // statement to get the data from another method
}

@Test
public static void Execution(){
    LIC_CommonMethods liccomm = new LIC_CommonMethods();
    LIC_AutoExecution licauto = new licauto();
    liccomm.method1();
    liccomm.method2();
    licauto.method1();
    licauto.method2();
}

@AfterTest
public static void closedriver(){
    driver.quit();
}

I tried adding constructors in TestBase, LIC_CommonMethods, and LIC_AutoMethods, but am not sure how to pass the driver value to these classes without affecting the driver.findElement statement used in the other classes such as TestBase, LIC_CommonMethods, and LIC_AutoMethods.


Solution

  • I followed the steps mentioned in the blog http://makeseleniumeasy.com/2020/05/27/threadlocal-static-webdriver-for-parallel-execution/ and solved the issue.