Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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:

[table id=7 /]

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:

[java]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]= “[email protected]”;

// Second student details data[1][0]= “Amod”; data[1][1]= “Mahajan”;

data[1][2]= “[email protected]”;

// Third student details data[2][0]= “Animesh”; data[2][1]= “Prashant”;

data[2][2]= “[email protected]”;

// Fourth student details data[3][0]= “Ankur”; data[3][1]= “Singh”;

data[3][2]= “[email protected]”;

// Fifth student details data[4][0]= “Amritansh”; data[4][1]= “Kumar”;

data[4][2]= “[email protected]”;

return data;

} }

[/java]

Output:

[xml][RemoteTestNG] detected TestNG version 6.14.2 Registered student with details: Mukesh Otwani [email protected] Registered student with details: Amod Mahajan [email protected] Registered student with details: Animesh Prashant [email protected] Registered student with details: Ankur Singh [email protected] Registered student with details: Amritansh Kumar [email protected] PASSED: methodWithSingleAttribute(“Mukesh”, “Otwani”, “[email protected]”) PASSED: methodWithSingleAttribute(“Amod”, “Mahajan”, “[email protected]”) PASSED: methodWithSingleAttribute(“Animesh”, “Prashant”, “[email protected]”) PASSED: methodWithSingleAttribute(“Ankur”, “Singh”, “[email protected]”)

PASSED: methodWithSingleAttribute(“Amritansh”, “Kumar”, “[email protected]”)

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

===============================================

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

===============================================

[/xml]

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:

[java]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”, “[email protected]”}, {“Amod”,”Mahajan”,”[email protected]”}, {“Animesh”,”Prashant”,”[email protected]”}, {“Ankur”,”Singh”,”[email protected]”},

{“Amritansh”, “Kumar”,”[email protected]”}

};

} }

[/java]

[xml][RemoteTestNG] detected TestNG version 6.14.2 Registered student with details: Mukesh Otwani [email protected] Registered student with details: Amod Mahajan [email protected] Registered student with details: Animesh Prashant [email protected] Registered student with details: Ankur Singh [email protected] Registered student with details: Amritansh Kumar [email protected] PASSED: methodWithSingleAttribute(“Mukesh”, “Otwani”, “[email protected]”) PASSED: methodWithSingleAttribute(“Amod”, “Mahajan”, “[email protected]”) PASSED: methodWithSingleAttribute(“Animesh”, “Prashant”, “[email protected]”) PASSED: methodWithSingleAttribute(“Ankur”, “Singh”, “[email protected]”)

PASSED: methodWithSingleAttribute(“Amritansh”, “Kumar”, “[email protected]”)

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

===============================================

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

===============================================

[/xml]

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

Uncategorized

Post navigation

Previous Post: Handling ElementNotVisibleException And Element Is Not Clickable Exception In Selenium
Next Post: API Testing Tutorial Part 6 – Idempotent Methods in HTTP Methods

Related Posts

Postman Tutorial Part 10 – Sending PATCH Request in Postman Uncategorized
TestNG Tutorials 63: Dependency in TestNG – Usage of Regular Expressions with DependsOnGroup Uncategorized
How To Push Code From Eclipse To GitHub Repo Using Access Token? Uncategorized
Frequently Asked Java Program 09: Printing Words In Circular Order From Given Index Uncategorized
REST Assured Tutorial 51 – How To Retrieve and Assert Content-Type of Response in Rest Assured Uncategorized
Postman Tutorial Part 39 – Header Presets in Postman Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark