如何从Maven中的pom.xml调用testng.xml文件

时间:2012-05-10 11:35:09

标签: testng maven-2 pom.xml maven-plugin

我正在使用Selenium WebDriver,Eclipse,TestNG和Surefire插件。我无法从pom.xml运行testng.xml文件。当我使用 mvn test 运行pom.xml时,它会直接运行 src / test / java 中的文件。

我的pom.xml代码是

<groupId>angel</groupId>
<artifactId>Angel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Angel</name>  
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   </properties>

<dependencies>
          <!-- Java API to access the Client Driver Protocols -->
 <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.21.0</version>
 </dependency>
  <!-- JExcel API is a java library which provides the ability to read, write, and                 modify Microsoft Excel spreadsheets.-->
  <dependency>
     <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
    </dependency>
   <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
</dependency>
 <!-- Java API for manipulate the Microsoft Excel Sheets.  -->  
 <dependency>  
<groupId>org.apache.poi</groupId>  
<artifactId>poi-contrib</artifactId> 
 <version>3.5-beta5</version>   
 </dependency>
 <!-- Java Mail API used to send Mails. -->   <dependency>             <groupId>javax.mail</groupId> 
 <artifactId>mail</artifactId>   <version>1.4.3</version>
</dependency>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.3.1</version>
  <scope>test</scope>
</dependency>
 </dependencies>  
  <build>  
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugin</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
          <configuration>
           <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
                 </configuration> 
    </plugin>
 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
             <source>1.6</source>
            <target>1.6</target>
         </configuration>        </plugin> 

</plugins> 
   </project>

请帮帮我。

我的项目结构是

 project
--src/main/java
--src/main/resources
--src/test/java
    |
     --my testng class file with @Test method.
--src/test/resources
     |
      --testng.xml
--maven dependencies
--jre system library
--src
   |
    --main
    --test
--target
pom.xml

- =&gt;文件夹名称 | - =&gt;子文件夹名称

我的testng.xml文件是......

  <?xml version="1.0" encoding="UTF-8"?>
  <suite name="Suite1" verbose="1"  >

 <test name="samplePage Test" >
   <classes>
   <class name="TestScripts.SamplePage" >
        <methods>
            <include name = "SamplePageTest_Execution"/>
        </methods>
    </class>
</classes>
 </test>
   <test name="newSamplePage Test" >
    <classes>
   <class name="TestScripts.NewSamplePage" >
        <methods>
            <include name = "NewSamplePage_Execution02"/>
        </methods>
        </class>
    </classes>
   </test> 
 </suite>

我只想从pom.xml通过testng.xml文件调用SamplePage_Execution方法。

我的SamplePage_Execution方法是

  public class sample{
  @Test
  public static void SamplePageTest_Execution() throws Exception{

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
boolean testStatus = true;
       driver = new FirefoxDriver();
      driver.get("http://www.google.com"); 

   WebElement searchField = driver.findElement(By.name("q")); 

    searchField.sendKeys(new String [] {"selenium2.0"});

    searchField.submit();

    driver.close();
}}

6 个答案:

答案 0 :(得分:13)

我在pom.xml中的以下代码运行良好:

<profiles>
   <profile>
      <id>selenium-tests</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>2.12.4</version>
               <configuration>
                  <suiteXmlFiles>
                     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                  </suiteXmlFiles>
               </configuration>
            </plugin>     
         </plugins>
      </build>
   </profile>
</profiles>

使用以下命令运行测试:

mvn clean test -U -Pselenium-tests

OR,

mvn clean test

答案 1 :(得分:2)

您应该将suiteXmlFile从类更改为File,例如将testng.xml文件更改为src / test / resources。此外,更新surefire-plugin(当前版本2.12)。另一件事是硒测试通常是集成测试而不是单元测试。

<强>更新 您必须添加testng依赖项而不是junit依赖项。因此,删除junit依赖项并相应地将testng依赖项添加到maven-surefire-plugin的文档中。

答案 2 :(得分:2)

您可以直接通过

从命令行运行maven test调用testng.xml
mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml

答案 3 :(得分:0)

我认为这里存在基本错误。 pom.xml =由Maven维护的包定义

和testing.xml(任何xml用户可以定义)用于TestNG测试定义。您可以使用xml配置,从自定义运行设置中进行自己的配置以运行Test NG 并且,您在xml文件中提供的配置(测试名称,监听器类名称,套件名称或类名称)应该与您的测试类匹配。就像你已经删除了类名= TestScripts.SamplePage但你的测试类= sample,所以这个xml永远不会运行你的测试类。 小例子:<class name="com.automation.test.TestA" />它在我的测试xml中,我的测试类将是TestA。

答案 4 :(得分:0)

你可以直接给出testng.xml而不是src / test / resources / testng.xml

我的POM.xml工作正常。将其替换为您的POM.xml。更新您的maven并再次运行。祝你好运:))

<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>Automation</groupId>
  <artifactId>Automation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <forkCount>0</forkCount>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.46.0</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.6</version>
    </dependency>
  </dependencies>
</project>

答案 5 :(得分:0)

<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>com.adiltutorials.facebook</groupId>
  <artifactId>WebdriverTests</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <forkCount>0</forkCount>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.52.0</version>
    </dependency>
    <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
</dependency>  
</dependencies>

</project>
相关问题