如何从命令行使用maven运行selenium测试?
我目前使用Eclipse作为我的Java IDE,我使用Maven。 我单击运行按钮,它可以运行我写的Selenium Java测试。
然后我继续在我的本地机器上安装Maven。
转到我的pom.xml文件所在的目录后。 我运行命令:mvn test
我收到以下结果:
[INFO] Scanning for projects... [INFO] [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBu ilder with a thread count of 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building SeleniumWebDriver 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumWebDriver --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, ie build is platfo rm dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ SeleniumWebDriver --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SeleniumWebDriver --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, ie build is platfo rm dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ SeleniumWebDriver --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ SeleniumWebDriver --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.139 s [INFO] Finished at: 2014-03-17T14:12:27-05:00 [INFO] Final Memory: 12M/99M [INFO] ------------------------------------------------------------------------
我不明白为什么Firefox webbrowser无法启动,并且运行了测试。 在Eclipse IDE中运行相同的测试时,Firefox webBrowser会启动。
它似乎编译得很好,但由于某些原因,它在编译后具有.class文件时不会测试或启动浏览器。
这是我的pom.xml文件的副本:
4.0.0 SeleniumWebDriver SeleniumWebDriver 0.0.1-SNAPSHOT org.seleniumhq.selenium selenium-java 2.40.0 org.seleniumhq.selenium selenium-firefox-driver 2.40.0 org.seleniumhq.selenium selenium-server 2.40.0 junit junit 4.11
这是一个较旧的主题,但仍然希望为那些坚持这一点的人提供输入。 您必须确保您创建的类文件名以“Test”字符串结尾。 例如AppTest,TempTest都是有效的类文件名,但AppCheck,TempTest1是无效的名称; maven不会检测这些文件以供执行。
我建议使用Maven Failsafe (用于集成测试)或Surefire(用于unit testing)。
org.apache.maven.plugins maven-failsafe-plugin 2.12 default integration-test verify
这很可能是由于您的目录结构。 Maven使用任意目录结构 。
-
src/main/java
是你的主要java代码 -
src/test/java
是你的测试。 默认情况下,Maven在执行mvn test
时会读取THIS目录。
你有两个选择:
-
将您的pom更新为:
src/java src/test ... -
遵守Maven约定并将测试源放在
src/test/java
直到我发布了自己的问题后才发现这个问题 ……后来又找到了答案! 我在下面提到了我的答案:
通过使用exec-maven-plugin并将以下内容添加到pom.xml,可以使Maven运行它编译的代码:
org.codehaus.mojo exec-maven-plugin 1.1.1 test java Selenium2Example arg0 arg1
正如你可以从代码片段中收集的那样,可以通过在pom.xml中列出参数来传递参数。 另外,请务必在mainClass
元素中使用正确的包名称。
然后,您可以运行mvn compile
然后运行mvn compile
mvn test
来编译和运行您的代码。
信用证必须转到http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/列出几种方法。