How to pass Parameters in TestNG


One of the important features of TestNG is ability to pass different test data to a test case as arguments that 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 about using testng.xml for parameterization. If we need to pass some simple values such as String or Integer types to the test methods at runtime, there is a something called @Parameter where parameter values through TestNG XML configuration files pass to test.


Lets explain how we can use parameters

Step 1 – Create a java test class, say,

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

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

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestNGParameterizationDemo {
            WebDriver driver;

            public void parameterizedTest(String browser) {
                        if (browser.equalsIgnoreCase("firefox")) {
                                    // set path to geckodriver.exe and create gecko instance
                                    driver = new FirefoxDriver();
                                    System.out.println("Browser Started:" + browser);
                        // Check if parameter passed as 'chrome'
                        else if (browser.equalsIgnoreCase("chrome")) {
                                    // set path to chromedriver.exe and create chrome instance
                                    driver = new ChromeDriver();
                                    System.out.println("Browser Started:" + browser);

Browser Started:Chrome

TestNG.xml looks like as shown below. Here, parameter name is browser name value for browser is “Chrome”. So, this “Chrome” value is pass to Test as parameter and as a result a Google Chrome browser opens.

<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name = "TestSuite">
  <test name="ChromeTest">
<parameter name="browser"value="Chrome" />
<class name="com.selenium.testng.TestNGDemo.TestNGParameterizationDemo">

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s