使用 WebdriverManager 在多个测试线程中注册驱动程序时出错

Error registering drivers in multiple test threads using WebdriverManager

我使用 TestNG xml 文件来管理测试队列和环境参数。 并想使用 WebdriverManager 库在系统中注册 drivers。但是当线程数超过 1 时,drivers 没有正确注册。请看下面。

例如,这里是测试配置和 driver 实例化的示例:

测试 1:

<suite name="project">
     <test name = "Wiki search tests" verbose="1" parallel="true">
         <parameter name="browserName" value="firefox" />
         <classes>
             <class name="project.tests.WikiSearchTest"/>
         </classes>
     </test>
</suite>

测试 2:

<suite name="project">
    <test name = "Wiki authorization tests" verbose="1" parallel="true">
        <parameter name="browserName" value="chrome" />
        <classes>
            <class name="project.tests.LoginTest"/>
        </classes>
    </test>
</suite>

如您所见,两者都有 "browserName" 参数,但其中的值不同。 driver在测试基地class注册示例:

private static final CountDownLatch latch = new CountDownLatch(1);

@Parameters({"browserName"})
@BeforeTest
public void setUp(@Optional String browserName) throws InterruptedException{
    switch (browserName) {
        case "firefox" :
            FirefoxDriverManager.getInstance().setup();
            latch.countDown();
            System.out.println(" in firefox");
            break;
        case "chrome" :
            ChromeDriverManager.getInstance().setup();
            latch.countDown();
            System.out.println(" in chrome");
            break;
    }
    latch.await();

Surefire 插件线程设置:

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire.version}</version>

        <configuration>
              <properties>
              <property>
                   <name>suitethreadpoolsize</name>
                   <value>2</value>
              </property>

当使用 suitethreadpoolsize 属性 的值 = 1 测试 运行 时,一切正常:

Running TestSuite
[2017-04-10 11:32:45:490] [main] INFO BrowserManager - Reading https://chromedriver.storage.googleapis.com/ to seek [chromedriver]
[2017-04-10 11:32:46:172] [main] INFO BrowserManager - Latest version of [chromedriver] is 2.29
[2017-04-10 11:32:46:190] [main] INFO BrowserManager - Exporting webdriver.chrome.driver as C:\Users\bnesteruk\.m2\repository\webdriver\chromedriver\win32.29\chromedriver.exe
 in chrome
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 11014


[2017-04-10 11:32:49:314] [main] INFO BrowserManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek [wires, geckodriver]
[2017-04-10 11:32:49:590] [main] INFO BrowserManager - Latest version of [wires, geckodriver] is 0.15.0
[2017-04-10 11:32:49:649] [main] INFO BrowserManager - Exporting webdriver.gecko.driver as C:\Users\bnesteruk\.m2\repository\webdriver\geckodriver\win64[=14=].15.0\geckodriver.exe
 in firefox

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.831 sec - in TestSuite

问题:

如果我在 surefire 插件中设置线程数大于 1,

 <property>
       <name>suitethreadpoolsize</name>
       <value>4</value>
 </property>

两个 .setup() 方法 运行 并行,似乎 BrowserManager 中 webdriver 的静态实例只是简单地替换为最后一个浏览器名称(在我的示例中为 geckodriver )

 [2017-04-10 11:38:00:726] [pool-1-thread-1] INFO BrowserManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek [wires, geckodriver]
[2017-04-10 11:38:00:726] [pool-1-thread-2] INFO BrowserManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek [wires, geckodriver]
[2017-04-10 11:38:01:301] [pool-1-thread-2] INFO BrowserManager - Latest version of [wires, geckodriver] is 0.15.0
[2017-04-10 11:38:01:301] [pool-1-thread-1] INFO BrowserManager - Latest version of [wires, geckodriver] is 0.15.0
[2017-04-10 11:38:01:361] [pool-1-thread-2] INFO BrowserManager - Exporting webdriver.gecko.driver as C:\Users\bnesteruk\.m2\repository\webdriver\geckodriver\win64[=16=].15.0\geckodriver.exe
[2017-04-10 11:38:01:361] [pool-1-thread-1] INFO BrowserManager - Exporting webdriver.gecko.driver as C:\Users\bnesteruk\.m2\repository\webdriver\geckodriver\win64[=16=].15.0\geckodriver.exe
 in chrome
 in firefox
[WARNING] [11:38:01.496] The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

我想,我使用 CountDownLatch 的方式不对?我怎么能处理这个?

您可以使用 Java CountDownLatch 在 WebDriverManager 工作时等待。像这样的东西(当然适合你的代码):

// Latch declaration (maybe as a class field)
CountDownLatch latch = new CountDownLatch(1);

// Somewhere in your code, you call to WebDriverManabger
ChromeDriverManager.getInstance().setup();
latch.countDown();

// In some other place in your code, you instantiate a WebDriver object
latch.await();
WebDriver chrome = new ChromeDriver();

问题已通过向驱动程序注册部分添加简单的同步(BrowserManager)部分解决,谢谢!

synchronized (BrowserManager.class) {
            switch (browserName) {
                case "firefox":
                    FirefoxDriverManager.getInstance().setup();
                    System.out.println("Registering    FIREFOX");
                    break;
                case "chrome":
                    ChromeDriverManager.getInstance().setup();
                    System.out.println("Registering    CHROME");
                    break;
            }
        }

输出:

Running TestSuite
Registering    CHROME
[2017-04-10 12:34:51:913] [pool-1-thread-2] INFO BrowserManager - Reading https://chromedriver.storage.googleapis.com/ to seek [chromedriver]
[2017-04-10 12:34:52:604] [pool-1-thread-2] INFO BrowserManager - Latest version of [chromedriver] is 2.29
[2017-04-10 12:34:52:624] [pool-1-thread-2] INFO BrowserManager - Exporting webdriver.chrome.driver as C:\Users\bnesteruk\.m2\repository\webdriver\chromedriver\win32.29\chromedriver.exe
Registering    FIREFOX
[2017-04-10 12:34:53:206] [pool-1-thread-1] INFO BrowserManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek [wires, geckodriver]
[2017-04-10 12:34:53:487] [pool-1-thread-1] INFO BrowserManager - Latest version of [wires, geckodriver] is 0.15.0
[2017-04-10 12:34:53:505] [pool-1-thread-1] INFO BrowserManager - Exporting webdriver.gecko.driver as C:\Users\bnesteruk\.m2\repository\webdriver\geckodriver\win64[=11=].15.0\geckodriver.exe