Parameterization Using TestNG.xml:
- Sometimes it may be time for us to pass the values of test methods during run time.
- Parameterization using the testng.xml file will allow users to pass parameters to test methods as an argument using XML file.
- To test the parameters, use the @Parameters annotation in the code and parameters tag in the XML file to pass parameter values.
- Runtime can pass some simple values like strings for test methods, you can use this approach to send parameter values through TestNG xml configuration files.
- For this, you need to use parameter annotations in the code and parameter tags in the XML file to test ways to test parameter values.
There are two types of parameterization in TestNG:
- Using Parameters annotation and TestNG XML file.
- Using @DataProvider annotation.
Example: Parameterization Using TestNG.xml file:
- This example we pass parameters using @Parameters annotation.
- Pass values this parameter using TestNG.xml file.
- This example we pass parameters using @Parameters annotation.NG.xml file into @Parameters annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package package_Name; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class TestNG_Parameterization { @Parameters({ "browser" }) @Test public void testCaseOne(String browser) { System.out.println("browser passed as :- " + browser); } @Parameters({ "user", "pass" }) @Test public void testCaseTwo(String user, String pass) { System.out.println("Parameter for UserName:- " + user); System.out.println("Parameter for Password:- " + pass); } } |
Output:
1 2 3 4 5 | browser passed as :- Firefox Parameter for UserName:- username Parameter for Password:- password |
Example: Pass parameter values using TestNG.xml file:
- Provide parameter values into TestNG.xml file to use in TestNG program.
- Using this syntax we provide values : <parameter name=”user” value=”username”/>.
- These parameter’s we use in any class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Parameterization Test Suite"> <test name="Testing Parameterization"> <parameter name="browser" value="Firefox"/> <parameter name="user" value="username"/> <parameter name="pass" value="password"/> <classes> <class name="package_Name.TestNG_Parameterization" /> </classes> </test> </suite> |
Note:
- The XML file above has three tests and explains the different methods of passing the parameters of each test methods.
- The parameter is declared by the parameter tag and name attribute, defines the name of the attribute and it must match the @Parameters annotation attribute in the code.
- And the value defines the value of the attribute parameter.
- The parameter tag can also be used at the level of the suite along with the test level.