samedi 5 mai 2018

Running Selenium Webdriver tests with TestNG in parallel does not sent correct data to browser

I have two TestNG classes with one test method in each. Each test class has its own @Dataprovider. Each test executes functionality on a web application using Selenium Webdriver. The driver is created for each TestNG class via a factory method and threadlocal so driver should be threadsafe. However when running the tests in parallel using testng.xml the same data is being used across both tests causing one to fail. Here is the code.

/ DriverFactory

@Listeners(ScreenshotListener.class)
public class DriverFactory {

    public final static String URL = "http://localhost/Quotation/Development/index.php";

    private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
    private static ThreadLocal<WebDriverThread> driverThread;

    @BeforeClass
    public static void instantiateDriverObject() {
        System.out.println("Before Class");
        driverThread = new ThreadLocal<WebDriverThread>() {
            @Override
            protected WebDriverThread initialValue() {
                WebDriverThread webDriverThread = new WebDriverThread();
                webDriverThreadPool.add(webDriverThread);
                return webDriverThread;}};
        }
    @BeforeTest
    public void setUp() throws Exception {

        System.out.println("Before Test");}

    public static WebDriver getDriver() throws Exception {
        return driverThread.get().getDriver();}

    protected String debug(String methodName) throws Exception {
        return methodName + " running on Thread " + Thread.currentThread().getId() +
                " with instance as " + this + " and driver " + driverThread.get().getDriver().toString();
    }

    @AfterSuite
    public static void closeDriverObjects() {
        System.out.println(webDriverThreadPool.size());
        for (WebDriverThread webDriverThread : webDriverThreadPool) {
            webDriverThread.quitDriver();}
        }
}

Aucun commentaire:

Enregistrer un commentaire