如何在Maven中从pom.xml调用testng.xml文件 [英] How to call testng.xml file from pom.xml in Maven

查看:117
本文介绍了如何在Maven中从pom.xml调用testng.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Selenium WebDriver, Eclipse, TestNG and Surefire plugin. I am not able to run testng.xml file from pom.xml. While I'm running pom.xml using mvn test it directly run the file which is in the src/test/java.

我的pom.xml代码是

my pom.xml code is

<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

-=>文件夹名称 |-=>子文件夹名称

-- =>folder names |-- =>sub folder names

我的testng.xml文件是...

My testng.xml file is...

  <?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方法.

i just wanted to call the SamplePage_Execution method from pom.xml through testng.xml file.

我的SamplePage_Execution方法是

My SamplePage_Execution method is

  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();
}}

推荐答案

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

My following code in pom.xml is working well:

<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>

使用以下命令运行测试:

Run the test by using following command:

mvn clean test -U -Pselenium-tests

OR,

mvn clean test

这篇关于如何在Maven中从pom.xml调用testng.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆