javaautomated-teststestng

Parallel tests failing in TestNG


I'm working on automation project and I need to run tests in parallel using testNG. When I run only one instance everything is working fine but when ran like this:

@Test(invocationCount = 3, threadPoolSize = 3)

Only one test is going through all the steps, the rest is failing somewhere in the first steps due to not finding elements that are visible.

Here is my DriverFactory code:

public class Factory {

    private static ThreadLocal<WebDriver> ThreadDriver = new InheritableThreadLocal<>();

    public static synchronized WebDriver getDriver() {
        WebDriver driver = ThreadDriver.get();


        if (driver==null) {
            FirefoxBinary binary = new FirefoxBinary(new File(
                    "path-to-mozilla"));
            FirefoxOptions options = new FirefoxOptions();
            options.setBinary(binary);
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver(options);
            ThreadDriver.set(driver);
        }


        return driver;
    }

    public static synchronized void quitDriver() {
        WebDriver driver = ThreadDriver.get();

        if(driver!=null) {
            driver.quit();
            ThreadDriver.remove();
        }
    }
}

Then it gets initialised in @BeforeMethod:

public class UploadPhotoTest {
    WebDriver driver;
    static String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    static String extentReportFile = "./src/main/resources/reports/report_" + dateName + ".html";
    public static ExtentReports extent = new ExtentReports(extentReportFile, false);
    public static ExtentTest test;

    @BeforeMethod
    public void setUp() {
        driver = Factory.getDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

I tried running it through testng.xml with a copy of the test named differently like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Load Tests" parallel="methods" thread-count="2">
    <test name="Create video Test">
        <classes>
            <class name="UploadPhotoTest">

            </class>
        </classes>
    </test>
</suite>

But then one test is going through some steps and failing, throwing error about not finding element, and then second one starts working.


Solution

  • The problem lies in your test code.

    You are doing this in your config method

    @BeforeMethod
    public void setUp() {
        driver = Factory.getDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    

    This code is setting up a WebDriver instance at the class level, which defeats the purpose of the ThreadLocal variable.

    Please make the below changes

    1. Change driver = Factory.getDriver(); to WebDriver driver = Factory.getDriver() in your configuration method. This will ensure that you have a local variable to refer to the webdriver and so it makes it easy for you to set up the timeouts and maximising the window part.
    2. In all your @Test methods, access the thread local webdriver directly via Factory.getDriver() instead of referring to it via the class level WebDriver variable.

    This should ensure that your code works.

    The idea is that you read the WebDriver instance directly via the ThreadLocal ONLY in all your @Test methods, instead of storing a reference to it in your test class.