Make Selenium Easy

TestNG Tutorials 51: DataProvider in TestNG – Two Dimensional Array DataProvider Method

Hello Folks,

In last post, we have seen Basics of DataProvider in TestNG and its working logic with one dimensional array. In this post I will explain the working logic of a DataProvider method which returns a two dimensional array.

I see people are always confused with the usage of a DataProvider method which returns a two dimensional array. I want make it simple. Here you go.

Suppose you are testing a student registration application. To register a student, you need to pass First_Name, Last_Name and Email_lD of student. You need to understand here that You required three data( First_Name, Last_Name and Email_lD) of student. Now you want to register 5 students on your application. We have two inputs here:

  1. No of fields to register a (single) student. (3 fields)
  2. No of student to be registered. ( 5 Student)

 

Let’s draw a table with data:

First_NameLast_nameEmail_ID
MukeshOtwaniMotwani@gmail.com
AmodMahajanamahajan@hotmail.com
AnimeshPrashantaprashant@gmail.com
AnkurSingh
asingh@gmail.com
AmritanshKumarakumar@gmail.com

In above table:

Row is a Number of students to be registered which is 5 and each row has three columns which are details of each student. Below is the flow it goes through:

  1. You pick first row of above table. Now use first column (First_Name) of first row and use it followed by second column (Last_name) and third column (Email_ID). Done with first row.
  2. Now you will pick second row and iterate through its columns and same for each columns of each rows.

The same logic we apply in a DataProvider method in TestNG. We need to pass above table in form of an Array to method from a DataProvider method.

We will create a 2D (Two Dimensional Matrix) array containing rows and columns which looks similar to above table. I hope you would have basics understanding of Arrays.

Let’s see an example:

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
public class DataProviderBasics2 {
   
	
	
	// You must need to mention data provider method name in Test method
	@Test(dataProvider="DataContainer")
    public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
        System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID);
        
      
    }
     
	
	// A data provider method with return type as 2D array
    @DataProvider(name="DataContainer")
    public Object[] myDataProvider() {
    	
    	Object data[][]=  new Object[5][3];
    	// First student details
    	data[0][0]= "Mukesh"; 
    	data[0][1]= "Otwani";
    	data[0][2]= "Motwani@gmail.com";
    	
    	// Second student details
    	data[1][0]= "Amod";
    	data[1][1]= "Mahajan";
    	data[1][2]= "amahajan@hotmail.com";
    	
    	// Third student details
    	data[2][0]= "Animesh";
    	data[2][1]= "Prashant";
    	data[2][2]= "aprashant@gmail.com";
    	
    	// Fourth student details
    	data[3][0]= "Ankur";
    	data[3][1]= "Singh";
    	data[3][2]= "asingh@gmail.com";
    	
    	// Fifth student details
    	data[4][0]= "Amritansh";
    	data[4][1]= "Kumar";
    	data[4][2]= "akumar@gmail.com";
    	
        return data;
        
        
    }
}

Output:

[RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: methodWithSingleAttribute("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: methodWithSingleAttribute("Amod", "Mahajan", "amahajan@hotmail.com")
PASSED: methodWithSingleAttribute("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: methodWithSingleAttribute("Ankur", "Singh", "asingh@gmail.com")
PASSED: methodWithSingleAttribute("Amritansh", "Kumar", "akumar@gmail.com")

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

Understand the output now. Method “studentRegistration” executed 5 times with different set of data we passed in given order. I hope you can understand now that how does 2D DataProvider pass data to a method in TestNG.

We can make out above DataProvider method more optimal. Instead of assigning values using indexes, we can use inline array as below:

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
public class DataProviderBasics3 {
   
	
	
	// You must need to mention data provider method name in Test method
	@Test(dataProvider="DataContainer")
    public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
        System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID);
        
      
    }
     
	
	// A data provider method with return type as 2D array
    @DataProvider(name="DataContainer")
    public Object[] myDataProvider() {      
        return new Object[][] 
        {
        	{"Mukesh", "Otwani", "Motwani@gmail.com"},
        	{"Amod","Mahajan","Amahajan@hotmail.com"},
        	{"Animesh","Prashant","aprashant@gmail.com"},
        	{"Ankur","Singh","asingh@gmail.com"},
        	{"Amritansh", "Kumar","akumar@gmail.com"}        	
        	
        };
        
        
    }
}
[RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: methodWithSingleAttribute("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: methodWithSingleAttribute("Amod", "Mahajan", "amahajan@hotmail.com")
PASSED: methodWithSingleAttribute("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: methodWithSingleAttribute("Ankur", "Singh", "asingh@gmail.com")
PASSED: methodWithSingleAttribute("Amritansh", "Kumar", "akumar@gmail.com")

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.