为什么在TestNG中使用threadPoolSize> 1时测试失败?

时间:2018-10-25 14:13:40

标签: java multithreading selenium selenium-webdriver testng

我必须使用TestNG中的以下配置运行测试。

@Test(invocationCount = 4, threadPoolSize = 2)

我的每次考试大约需要4分钟。因此,当我运行测试时,它将同时启动两个浏览器实例,并在两个实例上成功执行一半的测试。之后,测试将相互关联。

  

示例:一种浏览器操作正在另一浏览器上执行。

因此,我通过给出以下超时来运行测试。

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

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

这样,测试也将失败。因此,我在stackoverflow和其他博客上只引用了几篇文章。然后我发现失败的原因是static webDriver,所以我将其更改为dynamic,但仍然遇到相同的问题。

在这里,我有以下这些问题。

1。我的代码需要添加任何内容吗?

2。有没有办法让注意力集中在浏览器实例上?这意味着一个浏览器实例操作不应在其他实例上执行。

3。还有其他方法可以在多个线程上并行运行测试吗?

更新:

我正在使用页面对象模型+ TestNG框架,因此我需要调用其他类的方法来运行测试。

/*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;

    }

不是在driver initialization中定义@BeforeTest。我每次都从classB呼叫它。是否由于这种driver initialization发生此问题?

1 个答案:

答案 0 :(得分:0)

从给定的代码中,还不清楚为什么2分钟后发生关联。通常,对于并行运行,我们使用RemoteWebDriver和Selenium网格。我建议您尝试一下。

您可以从http://selenium-release.storage.googleapis.com/index.html下载selenium-server-standalone-*。jar。此演练假定您已经安装了Java。

在终端(或cmd)中运行命令以启动集线器:java -jar selenium-server-standalone-<version>.jar -role hub

然后是运行节点的命令:java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register

B类:

创建一个字段:private WebDriver driver; 修改getDriver方法如下:

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