Say good bye to intermediate level!
If you are genuinely follwoing these chapters and doing the handson without fail. It is time to move on to next level.
Parameterization in TestNG
TestNG is full of annotations and parameterization is nothing but passing the test data and similar stuff from testng.xml file instead of directly hardcoding them in the code. Let's look an simple example to have a better understanding. Here, I am going to write a class called "WhoIsThis" and I am going to pass the test data from testng.xml file.
NOTE: If you had followed my steps, you should already have a testng.xml file written for running multiple tests. Open the same file and paste the below code in the testng.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" verbose="2">
<test name="Run multiple tests">
<parameter name="Name" value="Agni"/>
<classes>
<!-- <class name="seleniumWithTestNG.OpenGoogle"/>
<class name="seleniumWithTestNG.SkipTest"/>
<class name="seleniumWithTestNG.TestWithPriority"/> -->
<class name="seleniumWithTestNG.WhoIsThis"/>
</classes>
</test>
</suite>
You can see <parameter name="Name" value="Agni"/> .
This line will pass the name Agni to the WhoIsThis class. Let's have a look at on how I have written the java code.
package seleniumWithTestNG;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WhoIsThis {
@Test
@Parameters("Name")
public void whoIsThis(String nameFromXml){
System.out.println("I am "+ nameFromXml);
}
}
I have used @Parameters annotation, which will receive the value from the testng.xml file during run time and will pass to the whoIsThis() method.
Run the program (Either right click the class-> Run as TestNG test or Right click the testng.xml->Run As -> TestNg Suite) and check the output. Below is my output.
Hope you have understood the concept or parametrisation. This is just a basic example, but the real world applications are limitless. Try doing more research on this technique, you will be amazed to see the results.