2017-08-30 120 views
1

我有一個Maven web項目,我正嘗試在Eclipse下使用Selenium和Chromedriver運行一些簡單的Spock Web UI測試。如果我將以下內容添加到文件的運行配置虛擬機參數中,我可以單獨運行每個測試類並單擊它並選擇「Run As> Junit Test」:在Spock測試中啓動chromedriver時遇到麻煩

-Dwebdriver.chrome.driver =/Users/mht/ChromeDriver /2.3.1/chromedriver

很明顯,這是我的系統上的Chromedriver的位置。然後我嘗試在項目級設置一個Maven構建目標來運行測試。我點擊項目名稱並選擇「運行配置> Maven Build」,並創建一個「驗證」配置以匹配我的pom中定義的驗證目標。在「目標」框中輸入「verify」,然後在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」來運行。爲什麼不設置鍍鉻驅動器位置離子不在Maven目標水平上工作?

我不認爲這真的很重要,但我的聚甲醛的生成部分是

<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

Maven的參數不被傳遞給每默認情況下,神火和故障安全配置。這兩個新的JVM都沒有得到這些參數。有關argLine,請參閱文檔SurefireFailsafe。因此mvn verify -DargLine="-Dwebdriver.chrome.driver=/Users/mht/ChromeDriver/2.3.1/chromedriver"應該爲你工作。

但是,更好的方法是使用WebDriver Extensions Maven Plugin這可用於自動下載相應的驅動程序。然後,您可以通過一些簡單的腳本來通過geb配置來定位驅動程序,或者對已知的相對位置進行硬編碼。

順便說一句gmaven-plus插件已過時。