BAST23
BAST23

Reputation: 75

How to run two test methods in two different browser in parallel using TestNG?

I have one test case contains two methods. When trying the two test methods in two browser instance, only one browser instance can open the website but the rest of the steps can't execute. Another browser instance can't even open the website (blank page).

I've tried the suggested solution on Stackoverflow. Those solutions do not work in my case.

public class RunSimpleTest{

private String baseUrl = "https://mywebsite";

public  WebDriver driver;

GlobalFunctions objGlobalFunc;

@BeforeMethod(alwaysRun = true)

public void setup() {

    try{

        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver.exe");

        // Disable Chrome Developer Mode Extension
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-extensions");
        options.addArguments("--start-maximized");

        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        objGlobalFunc = new GlobalFunctions(driver);

        driver.get(baseUrl);

        objGlobalFunc = new GlobalFunctions(driver);


        objGlobalFunc.selectEnglishLanguage();

    }
    catch (Exception e){
        e.printStackTrace();
    }

}

@Test
public void BTRun1() {
    objGlobalFunc.setUserNameValue("ABC");
    objGlobalFunc.clickOKBtnOnMEXLoginForm();
}
@Test
public void BTRun2() {
    objGlobalFunc.setUserNameValue("ABC");
    objGlobalFunc.clickOKBtnOnMEXLoginForm();
}
}   

BTRun1 is opened in a chrome browser. And, the user can login.

BTRun2 is opened in another chrome browser. And, the user can login.

Upvotes: 1

Views: 460

Answers (1)

AutomatedOwl
AutomatedOwl

Reputation: 1089

The core problem of your code is the usage of global WebDriver object.

When running in parallel, TestNG is creating just one instance of RunSimpleTest, therefore one instance of WebDriver object. That's causing the two test override each other when communicating with the WebDriver object.

One solution would be using ThreadLocalDriver and ThreadLocalGlobalFunctions:

protected ThreadLocalDriver threadLocalDriver;
protected ThreadLocalGlobalFunctions threadLocalGlobalFunctions;

public void setup() {

    try{

        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver.exe");

        // Disable Chrome Developer Mode Extension
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-extensions");
        options.addArguments("--start-maximized");

        threadLocalDriver = new ThreadLocalDriver(options);

        threadLocalDriver.getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        objGlobalFunc = new ThreadLocalGlobalFunctions(threadLocalDriver.getDriver());

        threadLocalDriver.getDriver().get(baseUrl);

        objGlobalFunc.getGlobalFunc().selectEnglishLanguage();

    }
    catch (Exception e){
        e.printStackTrace();
    }

}


@Test
public void BTRun1() {
    objGlobalFunc.getGlobalFunc().setUserNameValue("ABC");
    objGlobalFunc.getGlobalFunc().clickOKBtnOnMEXLoginForm();
}

@Test
public void BTRun2() {
    objGlobalFunc.getGlobalFunc().setUserNameValue("ABC");
    objGlobalFunc.getGlobalFunc().clickOKBtnOnMEXLoginForm();
}

To learn more about using ThreadLocal with WebDriver, check: http://seleniumautomationhelper.blogspot.com/2014/02/initializing-webdriver-object-as-thread.html

Upvotes: 1

Related Questions