How to pass Parameters in TestNG

 HOME

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.

@Parameters("value")

Lets explain how we can use parameters

Step 1 – Create a java test class, say, TestNGParameterizationDemo.java

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
 
public class TestNGParameterizationDemo {
            WebDriver driver;

            @Test
            @Parameters("browser")
            public void parameterizedTest(String browser) {
                        if (browser.equalsIgnoreCase("firefox")) {
 
                                    // set path to geckodriver.exe and create gecko instance
                                    System.setProperty("webdriver.gecko.driver",
                                                            "C:\\Users\\Vibha\\Desktop\\SeleniumKT\\geckodriver-v0.27.0-win64\\geckodriver.exe");
                                    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
                                    System.setProperty("webdriver.chrome.driver",
                                                "C:\\Users\\Vibha\\Desktop\\SeleniumKT\\chromedriver_win32\\chromedriver.exe");
                                    driver = new ChromeDriver();
                                    System.out.println("Browser Started:" + browser);
                        }
            }
}

Output
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 "http://testng.org/testng-1.0.dtd">
<suite name = "TestSuite">
  <test name="ChromeTest">
<parameter name="browser"value="Chrome" />
<classes>
<class name="com.selenium.testng.TestNGDemo.TestNGParameterizationDemo">
</class>
</classes>
</test>
</suite>

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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