2015-06-04 48 views
0

我想弄清楚如何运行Selenium WebDriver测试而不必使用Eclipse或IntelliJ或任何其他IDE。我使用纯文本编辑器来完成我所有的Java开发,并且不希望为了编译和运行测试而安装(并学习)IDE。如何使用Maven运行我的Selenium测试?

我试过下面的Selenium文档,但它实际上并没有告诉你如何从命令行运行测试。

我与Maven金额短暂经历以下几点:

$ mvn compile 
    <snip> 
    No sources to compile 

    $ mvn test 
    <snip> 
    No tests to run 

    $ mvn run 
    <snip> 
    Invalid task 'run' 

其他唯一的一个我所知道的是mvn jetty:run但似乎权利并不因为我不想运行一个新的Web服务器。

我怀疑我只是需要在我的pom.xml中设置正确的目标等,但我不知道他们应该是什么,并且出人意料地找不到任何联机。

任何人都可以帮忙吗?

+0

https://easytolearnautomationtesting.wordpress.com/maven-integration-with-automation-script/这将帮助您更好地了解如何通过maven执行脚本。 – ArrchanaMohan

回答

0

好吧,我终于意识到这实际上是一个Maven特有的问题,而不是Eclipse或Selenium。

Maven可以进行运行它通过编译使用exec-Maven的插件,并添加以下代码到pom.xml中:

<build> 
    <plugins> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     <version>1.1.1</version> 
     <executions> 
     <execution> 
     <phase>test</phase> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     <configuration> 
      <mainClass>Selenium2Example</mainClass> 
      <arguments> 
      <argument>arg0</argument> 
      <argument>arg1</argument> 
      </arguments> 
     </configuration> 
     </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 

正如你可能会从片段收集,理能通过将它们列在pom.xml中来传入。另外,请确保在mainClass元素中使用正确的包名。

然后,您可以运行mvn compile,然后按mvn test编译并运行您的代码。

Credit必须去http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/列出几种方法来做到这一点。

+0

实际上,人们通常使用的是Maven Surefire或Failsafe插件。这样你不需要像上面那样运行它:这与在命令行上运行它类似。默认情况下,Surefire已经将自己绑定到“测试”阶段。 – djangofan

1

简而言之:

mvn integration-testmvn verify就是你要找的东西。

说明

的目标,你调用,都是行家的生命周期阶段(见Maven Lifecycle Reference)。 mvn test适用于独立的单元测试,mvn integration-test在编译,测试和打包后运行。那也将是你调用Selenium测试的阶段。如果你需要启动和停止Jetty,Tomcat,JBoss等,你可以将这些启动/停止绑定到pre-integration-testpost-integration-test

我通常使用Failsafe运行集成测试,并在那里执行Selenium和其他集成测试的调用。

相关问题