Java 如何从命令行使用 maven 运行 selenium 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22462181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run a selenium test using maven from the command line?
提问by adbarads
I currently use Eclipse as my Java IDE and I use Maven. I click the run button and it is able to run a Selenium Java test I wrote.
我目前使用 Eclipse 作为我的 Java IDE,我使用 Maven。我单击运行按钮,它能够运行我编写的 Selenium Java 测试。
I then proceeded to install Maven on my local machine.
然后我继续在我的本地机器上安装 Maven。
After going to the directory where my pom.xml file is located. I run the command: mvn test
转到我的 pom.xml 文件所在的目录后。我运行命令:mvn test
I receive the following results:
我收到以下结果:
[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, i.e. 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, i.e. 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] ------------------------------------------------------------------------
I do not understand why the Firefox webbrowser does not start, and a test is ran. When running the same test in the Eclipse IDE the Firefox webBrowser does start.
我不明白为什么 Firefox 浏览器没有启动,并且运行了一个测试。在 Eclipse IDE 中运行相同的测试时,Firefox webBrowser 会启动。
It seems to compile fine, but it is for some reason not testing or kicking off the browser when it has the .class files after compilation.
它似乎编译得很好,但由于某种原因,当它在编译后具有 .class 文件时,它没有测试或启动浏览器。
here is a copy of my pom.xml file:
这是我的 pom.xml 文件的副本:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SeleniumWebDriver</groupId>
<artifactId>SeleniumWebDriver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
回答by ddavison
This is most likely due to your directory structure. Maven uses an arbitrary directory structure.
这很可能是由于您的目录结构造成的。Maven 使用任意目录结构。
src/main/java
is your main java codesrc/test/java
is your tests. Maven will read THIS directory when executingmvn test
by default.
src/main/java
是你的主要java代码src/test/java
是你的测试。mvn test
默认情况下,Maven 会在执行时读取此目录。
You have two options:
您有两个选择:
Update your pom to something like:
<build> <sourceDirectory>src/java</sourceDirectory> <testSourceDirectory>src/test</testSourceDirectory> ... </build>
Abide by Maven convention and put your test sources under
src/test/java
将您的 pom 更新为以下内容:
<build> <sourceDirectory>src/java</sourceDirectory> <testSourceDirectory>src/test</testSourceDirectory> ... </build>
遵守 Maven 约定并将您的测试源放在
src/test/java
回答by Sourabh Das
This is a older thread, but still want to provide input for those who are stuck up at this. You have to make sure that the class file names you are creating are ending with "Test" string. e.g. AppTest, TempTest are all valid class file names, but AppCheck, TempTest1 are invalid name; maven will not detect these files for execution.
这是一个较旧的线程,但仍然想为那些坚持这一点的人提供意见。您必须确保您创建的类文件名以“Test”字符串结尾。例如 AppTest、TempTest 都是有效的类文件名,而 AppCheck、TempTest1 是无效的名称;maven 不会检测这些文件来执行。
回答by FlyingCodeMonkey
I didn't see this until after I had posted my own question...and subsequently found an answer to it! I've included my answer below:
直到我发布了我自己的问题......并随后找到了答案之后,我才看到这个!我在下面提供了我的答案:
Maven can be made to run the code it compiles by using the exec-maven-plugin and adding the following to the pom.xml:
通过使用 exec-maven-plugin 并将以下内容添加到 pom.xml,可以使 Maven 运行它编译的代码:
<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>
As you can probably gather from the snippet, arguments can be passed in by listing them in the pom.xml. Also, be sure to use the proper package name in the mainClass
element.
正如您可能从代码片段中收集到的那样,可以通过在 pom.xml 中列出参数来传递参数。此外,请确保在mainClass
元素中使用正确的包名称。
You can then run mvn compile
followed by mvn test
to compile and run your code.
然后,您可以运行,mvn compile
然后mvn test
编译并运行您的代码。
Credit has to go to http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/for listing several ways to do this.
信用必须去http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/列出几种方法来做到这一点。
回答by djangofan
I would recommend using Maven Failsafe(for integration tests), or Surefire (for unit tests).
我建议使用Maven Failsafe(用于集成测试)或 Surefire(用于单元测试)。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>