How to pass Parameter to testng.xml from Command Line with Maven?
Last Updated :
23 Sep, 2024
In test automation, we run tests across multiple web browsers. The TestNG with Maven allows us to pass the parameters through the command line dynamically; this enables us to control various test environments without editing the code again and again.
Step to Pass Parameter to testng.xml from Command Line with Maven
A Maven project has been developed to launch a web browser and retrieve the title of a web page. This project includes a configuration file, pom.xml
, which specifies the Maven compiler and Surefire plugin. The steps outlined in this example project can be adapted for similar projects requiring Maven configuration for test automation.
Step 1: Modify pom.xml
to Pass File Name Dynamically
In your Maven project, configure the pom.xml
to accept dynamic file names for the TestNG XML suite. This allows you to specify different test suite files from the command line.
- Open your
pom.xml
file. - In the
<suiteXmlFiles>
section of the Surefire plugin configuration, change the file name to a variable ${filename}
.
pom.xmlStep 2: Right click on the project folder and click on properties
PropertiesStep 3: Copy the Location
LocationStep 4: Press Windows + "R", type "cmd" and click on Run.
Step 5: In the Command Prompt specify the project location
Command PromptStep 6: To trigger all the automation pom, where pom will trigger TestNG.xml
In the CMD run this parametrized command , you can replace "testng.xml" to the name of your test file name and after it's completion it'll launch the project.
mvn clean test -Dfilename=testng.xml
OutputPassing Parameters to testng.xml from Command Line
Step 1: Modify your testng.xml code
testng.xml
XML
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="gfg_suite" verbose="1" parallel="false" thread-count="2">
<test name="gfg_test">
<parameter name="browser" value="chrome" />
<classes>
<class name="SampleTestClass" />
</classes>
</test>
</suite>
Step 2: Run this command in the command line in your project directory
mvn test -Dbrowser=firefox
Step 3: Access the Parameter in Your Java Code
In your test code, retrieve the passed parameter using System.getProperty()
:
Java
public class YourTestClass {
@BeforeTest
@Parameters("browser")
public void setup(String browser) {
System.out.println("Browser: " + browser);
if (browser.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
}
@Test
public void fetchTitle() {
driver.get("https://example.com");
System.out.println("Page Title: " + driver.getTitle());
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
Conclusion
Maven and TestNg together can dynamically pass parameters to your testng.xml file from the command line terminal. It allows flexible and efficient testing across different types of environments. In this article it took an example to demonstrate the use of the browser parameters to launch different web browsers such as Chrome or Firefox based on the value passed through the command line.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Random Forest Algorithm in Machine Learning
A Random Forest is a collection of decision trees that work together to make predictions. In this article, we'll explain how the Random Forest algorithm works and how to use it.Understanding Intuition for Random Forest AlgorithmRandom Forest algorithm is a powerful tree learning technique in Machine
7 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read