TestNG Parameterization Using XML | TestNG Tutorial
TestNG Parameterization Using XML:
Parameterized tests allow developers to run the same test over and over again using different values.
There are two ways to set these parameters:
- with testng.xml
- with Data Providers
Let’s see passing parameters with testng.xml:
With this technique, we could define the parameters in the testng.xml file and then reference those parameters in the source files.
Create a java test class, say, ParameterizedTest.java
Add test method parameterizedTest() to your test class. This method takes a string as input parameter
Add the annotation @Parameters(“browser”) to this method. The parameter would be passed a value from testng.xml, which we will see in the next step.
Check below video to see “TestNG Parameterization using XML”
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
package softwareTestingMaterial; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterizedTest { @Test @Parameters("browser") public void parameterizedTest(String browser){ if(browser.equals("firefox")){ System.out.println("Open Firefox Driver"); }else if(browser.equals("chrome")){ System.out.println("Open Chrome Driver"); } } }
testng.xml
<parameter name="browser" value="firefox"/>
Here, name attribute represents the parameter name and value represents the value of that parameter. We could use this parameter to the test method parameterizedTest by using @Parameters(“browser”) as mentioned in the above step.
Now let’s see the complete testng.xml file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <parameter name="browser" value="firefox"/> <classes> <class name="softwareTestingMaterial.ParameterizedTest" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: Open Firefox Driver =============================================== softwaretestingmaterial Total tests run: 1, Failures: 0, Skips: 0 ===============================================
TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the types supported:
- String
- int/Integer
- boolean/Boolean
- byte/Byte
- char/Character
- double/Double
- float/Float
- long/Long
- short/Short
You could find the complete TestNG tutorial here.
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.