In this tutorial, we will learn the concept of data-driven testing. We will learn how to design and build a TestNG DataProvider class. The technologies used in this framework are Java and TestNG.
DataProvider Class in TestNG
DataProvider feature is one of the important features provided by TestNG. It is the second way of passing parameters to test methods. It allows the users to write data-driven tests in which we can run multiple times the same test method with different sets of test data.
The complex parameters can be complex objects, object read from a property file, or a database, etc that are created using Java technologies.
What is DataProvider?
DataProvider is a method in a class that returns a two dimensional an array of object (Object [ ][ ]) to the test method. The test method will be called m times in the m*n type of object array.
m ➨ The first array m represents the number of rows that has to be repeated your test m number of times.
This method is annotated with @DataProvider. We can have data providers with different names which can be defined either on a test class or on other classes.
@DataProvider Annotation in TestNG
TestNG provides an annotation called a @DataProvider to use the DataProvider feature in our tests. This annotation is declared with a method in our test class which can then be called on test methods.
@DataProvider annotation can take an attribute “name” called dataProvider in the Test annotation. It has the following general syntax/form to use.
@DataProvider(name = “myTestData“)
This annotation has only one string attribute called name. If the name of the data provider is not provided, the data provider’s name will automatically be set to the method’s name.
“myTestData” is the name of the function which is passing the test data. It can also be any other name. Look at the below screenshot for a complete Dataprovider annotation code.
TestNG uses an attribute called dataProvider in the called @Test to annotate the test methods. The following example is given below for using the annotation.
TestNG DataProvider Example with Multiple Parameters
Let’s see an example program and learn how to use the DataProvider feature in our tests. You follow all the steps below.
1. Open Eclipse and create a Java project named DataProviderProject.
2. Right-click on a java project and go to New option > create a package with name “testDataProvider”.
3. Now create a new java class with the name “DataProviderEx1”. Look out the below program source code.
Program source code 1:
Explanation of Source code:
1. The test class contains a setData() method which takes two arguments name and rollNo as input and prints it on the console when it will be executed.
2. A DataProvider method called dataProvidermethod has been declared in the same class by using DataProvider annotation of TestNG with attribute “name”.
3. The DataProvider returns a two dimensional (2D) an array of objects with three sets of data such as data one, data two, and data three.
4. The “getData” is the value of name attribute which passes the data to the dataProvider attribute which has the same value getData.
5. The DataProvider provides the values of the parameters to a setData() method which is annotated with @Test annotation, attribute dataProvider and value getData.
6. Now run the test class as TestNG Suite. You will see the following test result in the console.
As you can see the above test result, the respective setData() method has been executed three times in the class. The execution of the setData() method is dependent upon the number of sets of data passed by the dataProviderMethod.
Since the setData method is executed three times. Therefore, three different sets of data have been returned by the DataProvider.
1. It is mandatory for a DataProvider method to return the data in the form of a double array of object class (Object[ ][ ]). The first array represents a set of data whereas the second array contains values of parameters.
2. The “name” attribute of DataProvider is optional. If you don’t declare it, the name of method will use.
Let’s see another example program related to this point.
Program source code 2:
Let’s automate a scenario using data provider feature in the test.
Scenario to Automate:
In this scenario, we will take a very simple example of LogIn application pixabay where the username and password are required to clear the authentication.
First, make two accounts from two different email ids and passwords in the pixabay website. You can also take another website for your convenience. Now automate the following scenario below.
1. Launch the Firefox browser web browser.
2. Open the login page pixabay.
3. Login with two different sets of usernames and passwords using the DataProvider feature.
4. Logout webpage and close the browser.
Now follow all steps in the below source code.
Program source code 3:
TestNG Result Window:
Once test execution is finished, the results will look like this in the TestNg Result window.
As we are providing the test data two times. Therefore, the above test will be executed two times completely.
How to Call DataProvider from another Class?
To put the DataProvider method in another class, we will have to make data provider as to static. In addition to this, In the Test method, we will need to add one more attribute dataProviderClass in @Test annotation.
Scenario to Automate:
1. Launch Firefox browser.
2. Open the Yandex search (“https://www.yandex.com”).
3. Send data into search text box.
4. Close browser.
Now follow the below source code to automate the above scenario.
Program source code 4:
Now, create DataProviderClass.
Now execute DataProviderTest and see the below test result on the console.
TestNG Result Window:
Advantages of TestNG Data Provider
The advantages of data provider in TestNG are as follows:
1. TestNG Data provider helps to pass parameter values directly to the test method.
2. It allows users to write data-driven test where we can run multiple times the same test method with different sets of test data.
Hope that this tutorial has covered almost all important concept related to DataProvider annotation in TestNG with example scenarios and program source code. I hope that you will have understood this data provider in TestNG and enjoyed this topic.
Thanks for reading!!!
⏪ PrevNext ⏩