Parameterization using @DataProvider Annotation in Selenium

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.

@DataProvider Annotation Banner

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):

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

Console Output

@DataProvider annotation

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


Join Inviul fb group

Leave a Reply