Parameters in TestNG
In the previous post, we have learnt about assertions and reporting in TestNG. In this post, we will learn about parameters in TestNG.
As we know, parameterization plays very important role in automation testing. It is required when we don’t want to hard code the value in our code and it is not a good practice as well. Then parameterization comes into picture, so we will not have to hard code any value and it will be easy to handle the new data also.
Suppose, any scenario needs to be run with different set of data. This can be achieved by parameterization. Selenium itself is not sufficient to handle parameterization , that’s why it takes help from TestNg Framework. TestNG provides one more good feature called Parameters.
TestNG lets you pass parameters directly to your test methods with your testng.xml.
Let’s understand with an example:
- Adding parameters into testng.xml file. Added 2 parameters named as ‘browser’ and ‘app’.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="ExampleTestSuite" > <test name="Parameters functionality"> <parameter name="browser" value="Firefox"/> <parameter name="app" value="Gmail"/> <classes> <class name="com.testsuite.example.ParametersExample"/> </classes> </test> </suite>
2. Pass both the parameters to the method “Test” by using @Parameters annotation. The same no. of arguments has to be passed in the method.
package com.testsuite.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParametersExample { private WebDriver driver; @Test @Parameters({"browser","app"}) public void Test(String browser,String Application){ //With browser parameter if(browser.equalsIgnoreCase("Firefox")){ driver = new FirefoxDriver(); }else if(browser.equalsIgnoreCase("chrome")){ //Code for opening chrome } if(Application.equalsIgnoreCase("Gmail")){ driver.get("http://www.gmail.com"); }else if (Application.equalsIgnoreCase("Gmail")){ driver.get("http://www.gmail.com"); } } }
Output: This method will open a Firefox browser with Gmail appliacation.
Define Parameters at different scope:
Parameters can be defined at different scopes based on the need. It can be defined at suite
level, test
level, classes
level or at the class
level. If you define a parameter at suite level then all the classes defined under the suite can access it without the need of re-defining. You can also override a parameter at a lower scope. For example, in the below configuration file, we define Suite2 at suite level and then override it at class
level.
<?xml version="1.0" encoding="UTF-8"?> <suite name="ExampleTestSuite" > <parameter name="suiteParam" value="Truck"></parameter> <parameter name="suiteCommon" value="Vehicle"></parameter> <test name="Parameters functionality"> <parameter name="browser" value="Firefox"/> <parameter name="app" value="Gmail"/> <classes> <parameter name="ClassesParam" value="All Classes"></parameter> <class name="com.testing.Class1"> <parameter name="ClassParam" value="ScooterClass"></parameter> </class> <class name="com.testing.Class2"></class> </classes> </test> </suite>
Class1:
package com.testing; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Class1 { @Test @Parameters({"browser"}) public void Test3(String browser){ System.out.println("Test3 opens "+ browser); } @Test @Parameters({"ClassesParam"}) public void Test4(String allClasses){ System.out.println("Test4 "+ allClasses); } @BeforeSuite @Parameters({"suiteParam"}) public void Test1(String suiteParam1){ System.out.println("Test1 Suite Parameter" +suiteParam1); } @BeforeTest @Parameters({"suiteCommon"}) public void Test2(String suiteCommon){ System.out.println("Test2 - "+suiteCommon); } }
Class2:
package com.testing; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Class2 { @Test @Parameters({"browser","app"}) public void Test(String browser,String app){ System.out.println(browser); System.out.println(app); } }