无法在Spock测试中启动chromedriver

时间:2017-08-30 00:54:19

标签: maven testing selenium-chromedriver spock geb

我有一个Maven Web项目,我试图在Eclipse下使用Selenium和Chromedriver运行几个简单的Spock Web UI测试。我可以单独运行每个测试类,然后单击它并选择" Run As> Junit Test"如果我将以下内容添加到文件的运行配置VM参数:

-Dwebdriver.chrome.driver = /用户/ MHT / ChromeDriver / 2.3.1 / chromedriver

显然,这是Chromedriver在我的系统上的位置。然后我尝试在项目级别设置Maven构建目标来运行测试。我点击项目名称并选择"运行配置> Maven Build"并创建一个"验证"配置以匹配我的pom中定义的验证目标。在"目标框中,我输入"验证"在JRE选项卡上,我还在VM参数框中输入chromedriver的上述位置。当我运行此目标时,或者在命令行上输入" mvn verify"时,我收到以下错误:

geb.driver.DriverCreationException:无法从回调创建驱动程序' script15040527017471153797989 $ _run_closure1 @ 1046d517'     在com.google.common.base.Preconditions.checkState(Preconditions.java:754) ...

这告诉我测试无法找到chromedriver。如果我将chromedriver复制到项目的基本目录中,那么测试将使用验证目标或键入" mvn verify"在命令行上。为什么不设置chromedriver位置不能在maven目标级别工作?

我认为这不重要,但我的pom的构建部分是

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec.java</include>
                </includes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <goals>
                        <goal>run-war-only</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                    <configuration>
                        <port>9081</port>
                        <fork>true</fork>
                    </configuration>
                </execution>
                <execution>
                    <id>tomcat-shutdown</id>
                    <goals>
                        <goal>shutdown</goal>
                    </goals>
                    <phase>post-integration-test</phase>
                </execution>
            </executions>
        </plugin> 
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

1 个答案:

答案 0 :(得分:1)

默认情况下,maven参数不会传递给surefire和failsafe配置。两个分叉的新JVM都没有得到这些参数。请参阅有关argLine的文档SurefireFailsafe

所以mvn verify -DargLine="-Dwebdriver.chrome.driver=/Users/mht/ChromeDriver/2.3.1/chromedriver"应该适合你。

但是,更好的方法是使用可用于自动下载相应驱动程序的WebDriver Extensions Maven Plugin。然后,您可以通过geb配置执行一些简单的脚本来查找驱动程序,或者对已知的相对位置进行硬编码。

BTW gmaven-plus插件已过时。