【问题标题】:How do I run a selenium test using maven from the command line?如何从命令行使用 maven 运行 selenium 测试?
【发布时间】:2014-04-23 02:52:11
【问题描述】:

我目前使用 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, 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] ------------------------------------------------------------------------

我不明白为什么 Firefox 网络浏览器没有启动,并且运行了一个测试。在 Eclipse IDE 中运行相同的测试时,Firefox webBrowser 会启动。

似乎编译正常,但由于某种原因,当浏览器编译后有 .class 文件时,它没有测试或启动浏览器。

这是我的 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>

【问题讨论】:

标签: java eclipse maven selenium


【解决方案1】:

这是一个较旧的线程,但仍想为那些对此感到困惑的人提供输入。您必须确保您创建的类文件名以“Test”字符串结尾。 例如AppTest、TempTest都是有效的类文件名,但AppCheck、TempTest1是无效的名字; maven 不会检测这些文件执行。

【讨论】:

    【解决方案2】:

    我建议使用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>
    

    【讨论】:

      【解决方案3】:

      直到我发布了my own question...并随后找到了答案,我才看到这个!我在下面包含了我的答案:

      通过使用 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>
      

      正如您可能从 sn-p 中收集到的,可以通过在 pom.xml 中列出参数来传递参数。此外,请务必在 mainClass 元素中使用正确的包名称。

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

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

      【讨论】:

        【解决方案4】:

        这很可能是由于您的目录结构。 Maven 使用arbitrary directory structure

        • src/main/java 是您的主要 java 代码
        • src/test/java 是你的测试。 Maven默认会在执行mvn test时读取这个目录。

        你有两个选择:

        1. 将您的 pom 更新为:

          <build> <sourceDirectory>src/java</sourceDirectory> <testSourceDirectory>src/test</testSourceDirectory> ... </build>

        2. 遵守 Maven 约定,将您的测试源置于src/test/java

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-30
          • 2016-01-25
          • 2016-09-10
          • 2015-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多