How to pass Parameter to testng.xml from Command Line with Maven?
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}
.

Step 2: Right click on the project folder and click on properties

Step 3: Copy the Location

Step 4: Press Windows + "R", type "cmd" and click on Run.
Step 5: In the Command Prompt specify the project location

Step 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

Passing Parameters to testng.xml from Command Line
Step 1: Modify your testng.xml code
testng.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()
:
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.