In this article i will talk about how to run testng.xml from maven’s POM.xml file. This is very useful feature of maven when we need to run multiple testng.xml files.
The very first thing is, install maven in your eclipse IDE. Refer below article to know about how to install maven in eclipse ide.
How to download and Install Maven in Eclipse IDE?
Now, create a maven project. Refer below article to understand how to create new project in eclipse IDE.
How to create Maven Project for Selenium in Eclipse IDE?
Now, write one sample test case in one of the folder created by maven, preferably test folder. I have created below sample code –
package kdf1; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class Sample { @Test public void test(){ System.out.println("Test Print"); System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("http://demowebshop.tricentis.com/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } }
Now, create testng.xml file for executing above test case. My testng.xml files looks like –
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Default suite"> <test verbose="2" name="Default test"> <classes> <class name="kdf1.Sample"/> </classes> </test> <!-- Default test --> </suite> <!-- Default suite -->
Now, create a POM.xml file, add required dependencies. My POM.xml 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>com</groupId> <artifactId>kdf1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>test</scope> </dependency> </dependencies> </project>
Now, My job is to execute above given testng.xml file from above created POM.xml file via maven commands.
To achieve this, we need to use Suite XML files. Add below lines of code in POM.xml under <build> tags.
Make sure that you give the full testng.xml path.
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins>
After adding above piece of code in to my POM.xml (under <build> tags), my POM.xml 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>com</groupId> <artifactId>kdf1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>C:\Users\Prakash\workspace\kdf1\testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
Thats it, you are done.
You are good to execute your testng.xml file from maven’s POM.xml. The main advantage of using this is we can execute multiple testng.xml files.
You can execute the project now by right clicking on project, Run As –> Maven Test (another way to execute test case is from command prompt).
This was all about how to run testng.xml from pom.xml.
To know more about how to execute maven commands from command prompt, refer below link –
Maven Build Life Cycle – How to execute maven project
Hope this helps !!!!! Do provide your valuable comments !!!