Execute selenium webdriver testng.xml using Maven

We can execute our tests using maven Surefire plug-in. This plug-in is used during the test phase of software build lifecycle to execute tests. To configure Surefile Plug-in, we need to add the snippet as below in pom.xml file. And also we need to add TestNG dependency to the pom.xml file.

To get started with TestNG, we need to include the following dependency in our project configuration file Pom.xml

 <dependency>
   	<groupId>org.testng</groupId>
      	<artifactId>testng</artifactId>
      	<version>6.9.x</version>
      	<scope>test</scope>
 </dependency>

To run selenium tests, we need to include following dependency to pom file :

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
	<artifactId>selenium-java</artifactId>
	<version>2.xx.x</version>
</dependency>

To execute tests over multiple machines / virtual machines, we need to include Selenium server into your maven project, you need to add the following dependency to your pom.xml.

   <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.0.1</version>
    </dependency> 

After adding dependencies, navigate to project directory and run maven command from command-line which downloads all dependencies and adds them to the project.
mvn clean install

We have to include, maven-compiler-plugin and maven-surefire-plugin to the configuration file Pom.xml

Maven-compiler-plugin: Compiler-plugin is used to compile the sources of our project

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>3.5.1</version>
			<configuration>
				<source>${jdk.level}</source>
				<target>${jdk.level}</target>
			</configuration>
	</plugin>

Maven-surefire-plugin: Surefire-plugin is responsible for running tests that are placed in test source directory /src/test/java.

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>2.19.1</version>
			<configuration>
				<suiteXmlFiles>
				<!-- TestNG suite XML files -->
					<suiteXmlFile>testng.xml</suiteXmlFile>
				</suiteXmlFiles>
			</configuration>
	</plugin>

By default, Surefire-plugin runs all tests that matches with filename pattern such as *Test.java in test source directory src/test/java . To use a different naming scheme, we can configure Surefire Plugin which includes parameter and specify the tests that we want to include.

NOTE : - If you want to parametrize and choose which TestNG xml suites to run from command line, Please check article on Run TestNG xml suites from command line.

Let us now create a simple example using selenium, testNG and execute with the help of Maven.

Step 1: First create a maven project and name it as 'FirstDemo'.
Step 2: Create a class 'GoogleHomePageTest.java'
Step 3: Add Tests in 'GoogleHomePageTest.java' class.
Step 4: Add TestNg and Selenium Dependencies to maven pom.xml file.
Step 5: Now add maven Surefire Plug-in to pom.xml
Step 6: Execute tests using 'mvn test' from command prompt.

The below is the Project structure after creating as above steps.
maven Testng Java project

The below is the example program to execute testng.xml file using maven.

package com.google.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GoogleHomePageTest {
	
	private WebDriver driver; 
	String appURL = "http://google.com";

	@BeforeClass
	public void testSetUp() {
		
		driver = new FirefoxDriver();
	}
	
	@Test
	public void verifyGooglePageTittle() {
		driver.navigate().to(appURL);
		String getTitle = driver.getTitle();
		Assert.assertEquals(getTitle, "Google");
	}
	
	@AfterClass
	public void tearDown() {
		driver.quit();
	}
	
}

We have a test 'verifyGooglePageTitle()' in the above class. Below is the testng.xml file which we will include in pom.xml file.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Example test run">
  <test name="Simple Test">
    <classes>
      <class name="com.google.tests.GoogleHomePageTest"/>
    </classes>
  </test>
</suite>

After adding all the dependencies, we need to add the classes that we want to execute. And below is the pom.xml file looks like.

<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>FirstDemo</groupId>
	<artifactId>FirstDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<jre.level>1.7</jre.level>
		<jdk.level>1.7</jdk.level>
	</properties>

	<build>
		<plugins>
			<!-- Compiler plug-in -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${jdk.level}</source>
					<target>${jdk.level}</target>
				</configuration>
			</plugin>
			<!-- Below plug-in is used to execute tests -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18.1</version>
				<configuration>
					<suiteXmlFiles>
						<!-- TestNG suite XML files -->
						<suiteXmlFile>testng.xml</suiteXmlFile>
					</suiteXmlFiles>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- Include the following dependencies -->
	<dependencies>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>2.45.0</version>
		</dependency>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.8.8</version>
		</dependency>
	</dependencies>

</project>

After executing the above program, the report will be generated in your project folder under target\surefire-reports. You can checkout default testng html reports.

Hope the above example works for you. Please let us know if you face any problem.

Build Tools: 

Comments

Hai team,
Can you provide details on how to rerun failed test cases in Maven ( failed test cases available in testng-fsiled.xml)

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project FirstTestNGProjectCreation: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process

Hey Team,
I just want to thank you for creating this website if I am stuck anywhere I just visit this site and found all the solutions that I need for. I learn selenium on my own. But I don't feel like that I am alone that much you helped me. This is the first comment over I post on someone blog in my entire life. Thanks keep doing good work.

mvn clean test -DsuiteXmlFile=testng.xml
Have Parameters in BeforeSuite and Test Methods.And Passed them in testng.xml before <test> tags.

at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)

Hello,

When I trying the run a particular testng.xml with Maven surefire plugin, it executes all the tests under src/test/java even though they are not a part of the testng.xml file.

In your example, there is only file under src/test/java, so it seems to be working fine.

Could you please add one more .java file and try to run the same code and let me know if it works as expected or you too facing the same issue.

Thanks,
Deven J.

Would it not be better to include testng.xml files under test resources package?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.