Parameterization in TestNG using testng.xml

Using testng.xml file we can also pass values to the argument of any test case or method. @Parameters annotation is used to pass parameters to a test case or method. Let us consider an example where we have written a test case say invokeBrowser which accepts an argument browserType. Based on the value passed respective Browser is invoked.

In testng.xml file a tagname called parameter is used which has an attribute called name and another its value.

Below is the example for testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization">
	<test name="Firefox testing">
		<parameter name="BrowserType" value="firefox"></parameter>
		<classes>
			<class name="testng.Parameterization"/>
		</classes>
	</test>
</suite>

Let us see the code for same:

package testNg;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
 
public class Parameterization {
	WebDriver Driver;
 
	@Parameters ("BrowserType")
	@Test
	public void invokeBrowser(String sBrowserType){
 
		if(sBrowserType.equalsIgnoreCase("chrome")){
 
			System.setProperty("webdriver.chrome.driver",
			"C:\\workspace\\libs\\chromedriver.exe");
			Driver = new ChromeDriver();
		} else if(sBrowserType.equalsIgnoreCase("firefox")){
			Driver = new FirefoxDriver();
		} else if (sBrowserType.equalsIgnoreCase("ie")){
 
			System.setProperty("webdriver.ie.driver",
			"C:\\workspace\\libs\\IEDriverServer.exe");
			Driver = new InternetExplorerDriver();
		}
 
		Driver.manage().deleteAllCookies();
		Driver.manage().window().maximize();
 
		Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
		Driver.get("http://www.qatechhub.com/v4");
 
	}
 
}

Here, an attribute called sBrowserType accepts value from an attribute BrowserType passed as firefox from testng.xml file.

If a method has multiple arguments, values from parameter attribute are accepted in sequential order.

PS: For any questions queries or comment feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: