How to pass Parameters in TestNG


One of the important features of TestNG is the ability to pass different test data to a test case as arguments, which is called parametrization

There are mainly two ways through which we can provide parameter values to TestNG tests.

  1. Through testng.xml  XML configuration file
  2. Through DataProviders

In this tutorial, we will discuss using testng.xml for parametrization. If we need to pass some simple values such as String or Integer types to the test methods at runtime, there is something called @Parameter where parameter values through TestNG XML configuration files pass to test.


Let us explain how we can use parameters. To start with, add the below dependencies to the POM.xml in the case of the Maven project.




Step 1 – Create a JAVA test class, say,

Step 2 – Add test method parameterizedTest() to the test class. This method takes a string as an input parameter

Add the annotation @Parameters(“browser”) to this method. The parameter passes a value from testng.xml

Step 3 – Create a TestNG.xml and pass the value of the parameter in this configuration file.

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class TestNGParameterizationDemo {
	 WebDriver driver;
     public void parameterizedTest(String browser) {
        if (browser.equalsIgnoreCase("firefox")) {

        	 driver = WebDriverManager.firefoxdriver().create();
             System.out.println("Browser Started :" + browser);
        } else if (browser.equalsIgnoreCase("chrome")) {
        	 driver = WebDriverManager.chromedriver().create();
             System.out.println("Browser Started :" + browser);
     public void validCredentials()  {

         String newPageText = driver.findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
         System.out.println("newPageText :" + newPageText);

     public void invalidCredentials() {
         String actualErrorMessage = driver.findElement(By.xpath("//*[@id='spanMessage']")).getText();
         System.out.println("Actual ErrorMessage :" + actualErrorMessage);
         assertEquals(actualErrorMessage,"Invalid credentials");


     public  void closeBrowser() {

TestNG.xml looks like as shown below. Here, the parameter name is the browser name value for the browser is “Chrome”. So, this “Chrome” value is passed to Test as a parameter and as a result, a Google Chrome browser opens. Similarly, the same tests are run using Firefox, as it is mentioned in the testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">

<suite name="Suite ">
  <test name="Chrome Test">
   <parameter name="browser" value="chrome" />
      <class name="com.example.TestNGParameterizationDemo" />
  </test> <!-- Test -->
   <test name="Firefox Test">
   <parameter name="browser" value="firefox" />
      <class name="com.example.TestNGParameterizationDemo" />
  </test> <!-- Test -->
</suite> <!-- Suite -->

The output of the above program is


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s