Maven 构建成功,但没有在 Selenium 中执行测试

问题描述 投票:0回答:7

编辑:我已将项目上传到 github,供任何想要查看的人https://github.com/hfunsh/test-automation.git

我使用 Intellij 在 Selenium 中编写了一些测试,脚本在 IDE 中成功运行,但是当我尝试从控制台运行它们时,我收到“BUILD SUCCESSFUL”消息,但没有测试执行。

我可以从控制台运行其他 Maven Junit 项目,所以我知道这很可能是我的 pom 文件的配置问题,但我无法弄清楚。

这是 Java 类名称之一的示例(包含单词 Test)

/Users/ser/IdeaProjects/SeleniunTrainingII/src/test/java/com/selenium/tests/junit/tests/TestLoginAmazon.java

第二次编辑:我现在已经按照建议运行“mvn clean verify”

    INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Selenium tests project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ selenium-app ---
[INFO] 
[INFO] --- maven-failsafe-plugin:2.18.1:integration-test (default) @ selenium-app ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.2.1/maven-plugin-api-2.2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-descriptor/2.2.1/maven-plugin-descriptor-2.2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.2.1/maven-artifact-2.2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.2.1/maven-project-2.2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings/2.2.1/maven-settings-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-descriptor/2.2.1/maven-plugin-descriptor-2.2.1.jar (39 KB at 32.7 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.2.1/maven-plugin-api-2.2.1.jar (13 KB at 10.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.2.1/maven-profile-2.2.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.2.1/maven-artifact-manager-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings/2.2.1/maven-settings-2.2.1.jar (48 KB at 40.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-registry/2.2.1/maven-plugin-registry-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.2.1/maven-artifact-2.2.1.jar (79 KB at 64.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.2.1/maven-model-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.2.1/maven-project-2.2.1.jar (153 KB at 123.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-core/2.2.1/maven-core-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.2.1/maven-profile-2.2.1.jar (35 KB at 27.6 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-parameter-documenter/2.2.1/maven-plugin-parameter-documenter-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.2.1/maven-artifact-manager-2.2.1.jar (66 KB at 52.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.2.1/maven-repository-metadata-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-registry/2.2.1/maven-plugin-registry-2.2.1.jar (30 KB at 23.1 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-error-diagnostics/2.2.1/maven-error-diagnostics-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.2.1/maven-model-2.2.1.jar (86 KB at 66.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-monitor/2.2.1/maven-monitor-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.2.1/maven-repository-metadata-2.2.1.jar (26 KB at 19.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.2.1/maven-toolchain-2.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-parameter-documenter/2.2.1/maven-plugin-parameter-documenter-2.2.1.jar (22 KB at 16.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-error-diagnostics/2.2.1/maven-error-diagnostics-2.2.1.jar (13 KB at 9.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-monitor/2.2.1/maven-monitor-2.2.1.jar (11 KB at 7.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.2.1/maven-toolchain-2.2.1.jar (37 KB at 26.9 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-core/2.2.1/maven-core-2.2.1.jar (174 KB at 126.2 KB/sec)
[INFO] No tests to run.
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! The file encoding for reports output files should be provided by the POM property ${project.reporting.outputEncoding}.
[INFO] 
[INFO] --- maven-failsafe-plugin:2.18.1:verify (default) @ selenium-app ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.536 s
[INFO] Finished at: 2016-01-14T10:10:47-05:00
[INFO] Final Memory: 11M/108M

编辑:我现在已经将 Maven Surefire 和 Failuresafe 插件添加到我的 pom 中,但它仍然无法工作。

这是我更新后的文件的样子

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.selenium.test</groupId>
  <artifactId>selenium-app</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Selenium tests project</name>
  <description>Web Autotests simple project.</description>
  <url>http://maven.apache.org</url>

  <properties>
    <junit.version>4.11</junit.version>
    <testng.version>6.8.17</testng.version>
    <selenium.version>2.45.0</selenium.version>
    <compiler.version>1.7</compiler.version>
  </properties>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${compiler.version}</source>
          <target>${compiler.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.18.1</version>
        <executions>
          <execution>
            <configuration>
              <forkCount>3</forkCount>
              <reuseForks>true</reuseForks>
              <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
              <testFailureIgnore>true</testFailureIgnore>
              <includes>
                <includesFile>**/*Test.java</includesFile>
              </includes>
            </configuration>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>${testng.version}</version>
    </dependency>
    <dependency>
      <groupId>xml-apis</groupId>
      <artifactId>xml-apis</artifactId>
      <version>1.4.01</version>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>${selenium.version}</version>
    </dependency>
  </dependencies>
</project>

这是我的控制台的输出以及从控制台运行“mvn clean test”后的 POM 文件。

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] --------------------------------------------------------------
[INFO] Building Selenium tests project 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ selenium-app 
[INFO] Deleting   /Users/user/IdeaProjects/SeleniumTraining/target
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------    
[INFO] Total time: 0.350 s
[INFO] Finished at: 2016-01-13T17:07:22-05:00
[INFO] Final Memory: 6M/77M
[INFO] ------------------------

还有我的pom:

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.selenium.test</groupId> 
    <artifactId>selenium-app</artifactId> 
    <packaging>pom</packaging> 
    <version>1.0-SNAPSHOT</version>  
    <name>Selenium tests project</name> 
    <description>Web Autotests simple project.</description> 
    <url>http://maven.apache.org</url>  
    <properties> 
        <junit.version>4.12</junit.version> 
        <testng.version>6.8.17</testng.version> 
        <selenium.version>2.45.0</selenium.version> 
        <compiler.version>1.7</compiler.version>
    </properties>  
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-compiler-plugin</artifactId> 
                <version>3.1</version> 
                <configuration> 
                    <source>1.8</source> 
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>  
    <dependencies> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>4.12</version>
        </dependency> 
        <dependency> 
            <groupId>org.testng</groupId> 
            <artifactId>testng</artifactId> 
            <version>${testng.version}</version>
        </dependency> 
        <dependency> 
            <groupId>xml-apis</groupId> 
            <artifactId>xml-apis</artifactId> 
            <version>1.4.01</version>
        </dependency> 
        <dependency> 
            <groupId>org.seleniumhq.selenium</groupId> 
            <artifactId>selenium-java</artifactId> 
            <version>2.48.0</version>
        </dependency>
    </dependencies>
</project> 
java maven selenium selenium-webdriver
7个回答
2
投票

您需要将 maven-surefire-pluginmaven-failsafe-plugin 添加到您的 POM,然后运行 mvn clean test 命令,测试应该开始执行

示例摘录:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <executions>
                    <execution>
                        <configuration>
                            <forkCount>3</forkCount>
                            <reuseForks>true</reuseForks>
                            <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                            <testFailureIgnore>true</testFailureIgnore>
                            <includes>
                                <includesFile>**/*Test.java</includesFile>
                            </includes>
                        </configuration>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

编辑:您在 TestLogin.java 中的包名称是错误的。 编辑:此外,您在 POM.xml 中的打包设置为 pom..您需要将其设置为 jar


2
投票

我遇到了同样的问题,但已解决。 我改变的步骤:

Step1:我们的测试名称应以“Test”结尾。

Step2:我们应该检查项目的属性,其中应包含 M2_REPO

在eclipse中选择Project右键->Properties->Java BuildPath->Libraries

检查 M2_REPO 的可用性,如果不存在,则创建一个具有该名称的变量,并给出本地 m2 目录路径。

然后单击“应用”和“确定”。

在本地计算机中检查 m2 文件夹并将路径粘贴到此处。

Step3:右键项目->Maven->UpdateProject。

Step4:右键项目->RunAs->Maven Test。


1
投票

请确保您的测试目录包含在 pom.xml 文件中

<build>
    <testSourceDirectory>your_test_source_directory</testSourceDirectory>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>                      
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles> 
            <includes>
            <include>**.java</include>
            </includes>
         </configuration>
     </plugin>
</build>

1
投票

我是 Maven 和自动化的新手,我也遇到了同样的问题。 “测试”没有被执行。但是,要解决此问题,您只需在包名称前添加“com.packt”前缀即可。


1
投票

使用命令提示符,我有同样的问题,即只有 Maven BUILD SUCCESSFUL 但没有在 Selenium 中执行测试。

因为我在(src/test/java)下的所有测试类名称都不是以“Test”结尾。

例如。有问题的测试类名称 - AppiumTest02.java、RESTAPITest02.java 等

我将所有测试类名称更改为以“Test”结尾。

正确的测试类名称 - AppiumTest.java、RESTAPITest.java 等

确保我们的测试类名称应以“Test”结尾。

更改测试名称后就解决了。请尝试同样的方法来解决这个问题。


0
投票

将 maven-surefire-plugin 添加到您的 pom 中以触发 testng 运行。您可以在此文档链接中获取有关 Maven Surefire 插件的更多信息。

pom 中的示例插件条目:

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    [...]
</plugins>

0
投票

我的问题已解决,我刚刚将 POM XML 文件中的 maven-surefire-plugin 版本从版本 3.0.0-M5 替换为版本 3.0.0-M1。

© www.soinside.com 2019 - 2024. All rights reserved.