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
How to Run Specific TestNG Suite with Maven from Command Line? When using Maven as your build automation tool and TestNG for Java-based testing, you may need to execute particular test suites instead of running every test. Running test subsets according to functionality, environment, or any other criteria can be helpful in this regard. You must set up your Mave
2 min read
How to pass java code a parameter from Maven for testing? Maven is one of the most commonly used tools in Java projects for build automation, allowing the developer to manage the dependency of their project and many other operations, like compiling codes or running tests. Sometimes, you may want to pass some parameters from Maven to the Java code, so you c
3 min read
How to Pass Parameter from TestNG.xml File? Parameters in TestNG allow you to configure test methods flexibly and make them more maintainable and reusable. The parameters feature allows you to pass data directly from the testng.xml file to test methods. This increases reusability at the suite level without the need to hard code values into th
3 min read
How to call testng.xml file from pom.xml in Maven? Maven is a tool that is widely used for automation whenever developers work on Java projects. apart from the Java language, the Maven software supports other projects that are written in C#, Ruby, etc. The Maven project is controlled and hosted by the famous Apache software foundation.Table of Conte
4 min read
How to run TestNG from command line? Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility a
4 min read