使用WebdriverManager

时间:2017-04-07 13:45:46

标签: java selenium-webdriver testng webdrivermanager-java

我使用TestNG xml文件来管理测试队列和环境参数。 并且想使用WebdriverManager库来注册系统中的驱动程序。但是,如果线程数超过1,则驱动程序未正确注册。请参阅下文。

对于instanse,这里是测试配置和驱动程序实例化的一个例子:

测试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”参数,但其中包含不同的值。 驱动程序在测试基类中注册的示例:

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属性,当测试以value = 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\2.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\0.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\0.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\0.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?我怎么能这样呢?

2 个答案:

答案 0 :(得分:0)

您可以使用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();

答案 1 :(得分:0)

通过向驱动程序注册部分添加简单的同步(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\2.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\0.15.0\geckodriver.exe