TestNG allows you to group your tests and also allows to pass Test Parameters using parameter tag in the TestNG xml file.
You can then execute one or more specific groups such as Sanity,Regression or Functional Tests based on the requirement and passing the parameter values to the testng.xml using maven-surefire-plugin via pom.xml the following configuration is helpful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> <configuration> <suitexmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suitexmlFiles> <systemPropertyVariables> <propertyName>urlValue</propertyName> </systemPropertyVariables> <groups>sanity,regression</groups> </configuration> </plugin> </plugins> |
Passing the specific group name from the Command Line is achieved by updating the configuration of maven-sure-fire plugin as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> <configuration> <suitexmlFiles> <suiteXmlFile>Grouping.xml</suiteXmlFile> </suitexmlFiles> <systemPropertyVariables> <propertyName>${url}</propertyName> </systemPropertyVariables> <groups>${groups}</groups> </configuration> </plugin> |
Maven Command Line:
Steps to achieve is mentioned as follows:
Like our Facebook Page to stay in touch with us. Happy Testing!!!!
https://www.facebook.com/totalqa
1. Create a Testng @Test program as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.totalqa.tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class GroupingTests{ @Parameters({"url"}) @Test(groups={"sanity","regression"}) public void verifyTitle(String url) { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get(url); String actual = driver.getTitle(); String expected = "Total-QA - Future of Software Testing"; Assert.assertEquals(actual, expected); } @Test(groups={"sanity"}) public void test2() { } } |
2. Create an Grouping.xml file to invoke the Testng @Test Program.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" verbose="5"> <test name="Test"> <classes> <class name="org.totalqa.tests.GroupingTests" /> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
3. Update the pom.xml as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<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>org.iit.mmp</groupId> <artifactId>seleniummaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>seleniummaven</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <verbose>true</verbose> <fork>true</fork> <executable>C:\Program Files\Java\jdk1.8.0_65\bin\javac.exe</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> <configuration> <suitexmlFiles> <suiteXmlFile>Grouping.xml</suiteXmlFile> </suitexmlFiles> <systemPropertyVariables> <propertyName>${url}</propertyName> </systemPropertyVariables> <groups>${groups}</groups> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> </dependency> </dependencies> </project> |
4. Download the apache maven software apache-maven-3.6.0-bin.tar.gz from this url https://maven.apache.org/download.cgi.
5. Set the classpath as follows.
6. Execute the following command from the command line.
Conclusion:
Passing the command line arguments is possible by using apache maven via pom.xml to the TestNG.xml
Hi Sir, can you update your article? Some parts of the pom and grouping XML files are not visible and it is really difficult to figure it out.