简体   繁体   中英

Why are the tests failing while using threadPoolSize>1 in TestNG?

I have to run my test with below configurations in TestNG.

@Test(invocationCount = 4, threadPoolSize = 2)

Each of my test will take approximately 4 minutes . So when i run the test, it is launching two browser instances at same time and executing the half of the test successfully on both instances. After that, the tests are correlating with each other values.

Example: One browser action is performing on other browser.

So i run the tests by giving timeouts as below.

@Test(invocationCount = 4, threadPoolSize = 2,timeOut=240000)

and

@Test(invocationCount = 4, threadPoolSize = 2,invocationTimeOut=240000)

In this way also tests are getting failed. So i referred few articles on stackoverflow and other blogs. Then i found a reason for failure is static webDriver so i changed it to dynamic still i faced the same problem.

Here I have these below questions.

1. Is there anything need to be added for my code?

2. Is there a way to keep the focus on browser instances? That means one browser instance actions should not perform on other instance.

3. Is there any other way to run my tests parallelly on multiple threads?

UPDATE:

I am using page object model + TestNG frame work.So I need to call methods from other classes to run my test.

/*This test is in classA */
/*'loginToApplication' method is in classB*/
@Test(invocationCount = 4, threadPoolSize = 2)
        public void verifyAccountDetails() {
            accountPage = (AccountPage) this.loginToApplication(propLoad.getProperty("username"), propLoad.getProperty("password"));
  }

/*This method is in classB*/
public Object loginToApplication(String userName, String password) {
        DetailsPage details = this.navigateToAccounts();
        details.enterUserName(userName);
        details.enterPassword(password);
        details.clickLogin();
}

/*This method also in classB*/
public AccountsPage navigateToAccounts() {
        DetailsPage details = new DetailsPage(this.getDriver());
        return details;
    }

/*This method also in classB*/
public WebDriver getDriver()
    {
    WebDriver driver =null;
    System.setProperty("webdriver.chrome.driver", System.getProperty("location");
    ChromeOptions options = new ChromeOptions();
    options.setCapability("acceptInsecureCerts", true);
    options.addArguments("disable-infobars");
    driver = new ChromeDriver(options);                           
    driver.get(propLoad.getProperty("URL"));
    return driver;

    }

Instead of defining driver initialization in @BeforeTest . I am calling it from classB for everytime. Is this issue happening because of this type of driver initialization ?

From the given code it's not entirely clear why the correlation happens after 2 minutes. Usually, for parallel runs we use a RemoteWebDriver and Selenium grid. I suggest you giving it a try.

You can download the selenium-server-standalone-*.jar from http://selenium-release.storage.googleapis.com/index.html This walk-through assumes you already have Java installed.

in terminal(or cmd) run command to start hub: java -jar selenium-server-standalone-<version>.jar -role hub

then a command to run node: java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register

In class B:

create a field: private WebDriver driver; modify getDriver method as follows:

private WebDriver getDriver() {
    if(this.driver == null) {
      DesiredCapabilities capability = DesiredCapabilities.chrome();
      driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    }
    return driver;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM