Automation framework has prime importance with respect to automation testing. We have discussed Data-driven framework (in recent live session) which exclusively incorporates different sets of test data for testing of the scenario. Parameterization is the term given by test engineers and framework architect to inject test data from external sources like Excel sheet, XML, JSON, YML, etc. After considering all these technicalities, TestNG has an inbuilt feature to accommodate a large amount of test data. Hence, parameterization through TestNG can be done with the help of @DataProvider annotation.
Our agenda for this tutorial is to discuss the parameterization in Selenium using TestNG in-built feature. It has various significances, some of its significances are discussed below.
Significances of using @DataProvider annotation for parametrization
- It converts any method into data supplier method
- A broad range of test data are parameterized through @DataProvider annotation
- Test data from other sources like excel sheet, JSON, etc are injected through @DataProvider
- It returns the 2-dimensional array of object
- @DataProvider sets value for the @Factory annotation
Properties of @DataProvider annotation
- Its default value is its method name. If you forgot to use (name=”dataProvider”) then you can proceed by using the method name inside @DataProvider annotation
- When we have a large number of values in @DataProvider then it takes all values as default. But we can restrict it by defining its indices as @DataProvider(name=”dataprovider”, indices={1, 5, 7, 9})
- TestNG is loosely coupled test framework, we can connect Test class and @DataProvder class together by using property dataProviderClass as @Test(dataProvider =”dataprovider”, dataProviderClass= DataProviderClass.class)
@DataProvider solves 99% problems of the data-driven framework. You can efficiently design your framework with the help of @DataProvider annotation.
Here are some of the basic tutorials on TestNG, which I would like to recommend you (before we jump to the implementation of @DataProvider annotation):
- TestNG introduction and easy installation guide
- An introduction to TestNG annotations
- How to ignore tests in Selenium using TestNG?
- A technique to skip tests in Selenium at runtime
How to implement @DataProvider annotation for Parameterization
We have choices to implement @DataProvider either in the same class where we are using the test or in another class. If we are using Test class and @DataProvider class differently then we need to write a definition for the different class.
When @DataProvider and @Test are in the same class
In this case, we are keeping both the annotations in the same class file. Here is the sample programme:
Sample Program
package Test; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestClass { @Test(dataProvider="dataProvider") public void readData(String u, String p) { System.out.println(u+" - "+p); } @DataProvider(name="dataProvider") public Object[][] dataMethod(){ Object[][] data = new Object[2][2]; data[0][0] = "First Name"; data[0][1] = "Last Name"; data[1][0] = "Avinash"; data[1][1] = "Mishra"; return data; } }
Console Output
When @DataProvider and @Test are in a different class
In this case both the annotations are in a different class. So here is the sample program for each one of them.
Note: You make sure that the method of @DataProvider annotation is static otherwise TestNG will throw SkipException.
Sample Program
TestClass.java
package Test; import org.testng.annotations.Test; public class TestClass { @Test(dataProvider="dataProvider", dataProviderClass=DataProviderClass.class) public void readData(String u, String p) { System.out.println(u+" - "+p); } }
DataProvderClass.java
package Test; import org.testng.annotations.DataProvider; public class DataProviderClass { @DataProvider (name="dataProvider") public static Object[][] dp() { return new Object[][] { {"First Name", "Last Name"}, {"Avinash", "Mishra"} }; } }