13

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.

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>

Please help me.

My project structure is

 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

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>

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

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();
}}
Andrejs
  • 10,803
  • 4
  • 43
  • 48
Manigandan
  • 5,004
  • 1
  • 28
  • 47
  • My following code doesn't work: testng.xml The code below worked fine: **/*ST.java Note: My selenium java code name is: Selenium2ExampleST.java – Ripon Al Wasim Dec 04 '12 at 11:14

6 Answers6

17

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
Community
  • 1
  • 1
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
3

You can run maven test invoking testng.xml from command line directly via

mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
Xiao
  • 12,235
  • 2
  • 29
  • 36
2

You should change suiteXmlFile from a class to a File instead for example testng.xml file which is located in src/test/resources. Furthermore update the surefire-plugin (current version 2.12). One other thing is that selenium tests are usually integration tests and not unit tests.

Update You have to add the testng dependency instead the junit dependency. So remove the junit dependency and add the testng dependency accordingly to the documentation of the maven-surefire-plugin.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • i changed both the things but i didn't get the solution. will u please explain it more elaborately. – Manigandan May 10 '12 at 12:51
  • What does that mean? Did you got an error message? Have you update maven-surefire-plugin to 2.12 ? Where is you testng.xml file located ? – khmarbaise May 10 '12 at 12:56
  • No, i didn't get any error msg.Actually, i'm having a testng class in the "src/test/java" directory. as i mentioned earlier while i'm running pom.xml file it directly executes the testng class file and it runs fine. but i want to run that testng class file by using the testng.xml file and the testng.xml file should called through pom.xml.. And i updated the maven-surfire-plugin to 2.12 also. – Manigandan May 10 '12 at 13:13
  • Than you have to put a correct testng.xml file into src/test/resources and given an appropriate configuration for that like this: src/test/resources/testng.xml – khmarbaise May 10 '12 at 14:42
  • i didn't have any path like that..My project structure is project name --src/main/java --src/test/java | --my testng class --maven dependencies --jre system library --src --target pom.xml testng.xml. where to find the src/test/resource directory in the project explore? – Manigandan May 11 '12 at 04:05
  • Manigandan, you need to move your testng.xml in your src/test/resources folder, since that is where it should ideally lie in a maven project. Then you need to specify the path that khmarbaise has suggested. – niharika_neo May 11 '12 at 05:12
  • thanks for your concern niharika. I moved my testng.xml file in to the src/test/resource folder and i specify the path as khmarbaise mentioned. but, i didn't get the solution. my pom.xml file doesn't recognize the testng.xml file.please clarify me. – Manigandan May 11 '12 at 09:55
  • You seemed to have not read what i wrote: src/test/resources/testng.xml.. – khmarbaise May 11 '12 at 11:50
  • hi khmarbaise sorry for the late reply..i changed my path directory as u mentioned above.but till now i didn't get the solution.Please make me clear.. – Manigandan May 14 '12 at 04:02
  • Updated the answer accordingly. – khmarbaise May 14 '12 at 07:33
  • khmarbaise : i did as you mentioned in the answer..but, my bad time i didn't get the answer not yet. I added all my files above..please take a look and tell where i made wrong.. – Manigandan May 14 '12 at 12:23
0

I think there is basic Mistakes here.. pom.xml = package definition which is maintained by Maven

and testing.xml(any xml user may define) is used for TestNG test definition. Either you use the xml configuration of you can make own configuration from custom run settings for running Test NG And, your provided configuration in xml file (test name, listener class name, suite name or class name) should me match to your test class. Like you have decleared class name = TestScripts.SamplePage but your test class = sample , so this xml will never Run your test class. Small example : <class name="com.automation.test.TestA" /> it is in my testing xml and my test class will be TestA.

Shantonu
  • 1,280
  • 13
  • 12
0

you can give direct testng.xml instead of src/test/resources/testng.xml

My POM.xml is working fine. Replace it with your POM.xml . Update your maven and run again. Best of luck :)

<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>
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
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>