@Parameters in TestNG: How to Pass Value at Runtime from testng.xml

Print View Mobile View

In Annotations post, we briefly understood that @Parameters is used to pass different values to a method at runtime, allowing us to run a test case multiple times with different values. These values are stored in a file called testng.xml – an XML configuration file. Let’s take a look at how this works now.

Below you’ll find an example where Google homepage is opened and a query is executed. We are going to pass two parameters “purl” and “keyword” via testng.xml file.

File: ParametersDemo.java

package TestNGDemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;

public class ParametersDemo {
	public WebDriver driver;

	@Test
	@Parameters({"purl","keyword"})

	public void testParameterWithXML(String purl,String searchKey){

		// Set path of Chrome Driver
		System.setProperty("webdriver.chrome.driver", "C:\\Files\\chromedriver.exe");
		// Create a new instance of Chrome driver
		driver = new ChromeDriver();

		// Open Google
		driver.navigate().to(purl);

		// Find Search field
		WebElement kw = driver.findElement(By.name("q"));
		// Type in keyword for search
		kw.sendKeys(searchKey);

		// Prints in console
		System.out.println("We are going to search for a link to "+searchKey);		

		// Clicks on search button
		driver.findElement(By.name("btnK")).click();
		
		// Close Google Chrome
		driver.quit();
	}
}

The parameters would be passed from testng.xml. This file is located inside your project directory. If it doesn’t exist, you can create the file by going to: File menu > New > File. Name the file as “testng.xml” and click on Finish. You should now be able to see the file listed under “Package Explorer” in Eclipse.

Note: Parameters specified in the XML file are mapped in the exact order they are found to the Java parameters, so TestNG will issue an error if the count of values or the type don’t match.

Add the below code to testng.xml file:

File: testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" thread-count="3">
    <parameter name="purl" value="http://google.com" />
    <test name="Test">
        <parameter name="keyword" value="Download Selenium" />
        <classes>
            <class name="TestNGDemo.ParametersDemo"></class>
        </classes>
    </test>
</suite>

In the above file, we have two attributes:

  • name: defines name of the parameter
  • value: defines the value of the parameter.

Now to run the script, right-click on testng.xml file -> Run as -> Testng Suite

@Parameters annotation support these data types: String, Integer, Byte, Character, Double, Float, Long, Short, and Boolean. You can use any of these and TestNG will automatically convert the specified value to the type of the parameter.

Parameters can be used with any method that has a @Test, @Before, @After or @Factory annotation. Its values can be set at both suite and test level. If there are two parameters with the same name, then the parameter value defined at the test level will have precedence over suite level.