Testing With Selenium WebDriver (68 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (58 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Know How To Use DataProvider in TestNG – TestNG Parameterization

Published on Jun 06,2019 2.1K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
10 / 14 Blog from Selenium Webdriver

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Selenium is used to make things easier and faster. But what’s the point of Automation Testing if the test-cases have to be provided manually? In order to speed up the testing process, the test cases must be automated. Well, this is where DataProviders fits into the picture. DataProvider in TestNG is used to automate the process of providing test-cases and that’s what I will be talking about in this article.

I will be covering the following topics in this article:

What is Data Driven Framework?

Data-driven is a test automation framework which stores test data in a table or spreadsheet format. It allows automation engineers to have a single test script for executing all the test data present in the table. In this framework, input values are read from data files and are stored into a variable in test scripts. Data Driven testing enables building both positive and negative test cases into a single test only.

Data Driven Framework - Data Provider in TestNG - EdurekaIn the Data-Driven Test Automation framework, input data can be stored in single or multiple data sources like XLS, XML, CSV, and databases.

Types of Parameterization in TestNG

In order to understand parameterization in a better way, let’s understand the parameterization options in TestNG. There are two ways by which we can achieve parameterization in TestNG and they are as follows:

  • With the help of Parameters annotation and TestNG XML file

@Parameters({“name”, “searchKey”})

  •  With the help of DataProvider annotation

@DataProvider(name= “searchProvider”)

Now let’s dive deeper and understand what is DataProvider in TestNG.

What is DataProvider in TestNG?

If you have to provide the test data, then you need to declare a method that returns the data set in the form of two-dimensional object array Object[][]. The first array represents a data set whereas the second array contains the parameter values. The DataProvider method can be in the same test class or one of its superclasses. It is also possible to provide DataProvider in another class but then the method has to be static.

Once you have added this method, you need to annotate it using @DataProvider to let TestNG know that it is a DataProvider method. You can also provide a name using the name attribute of the DataProvider annotation. If one hasn’t provided the name, by default the name of the method will be used for reference.

Now that you know what is DataProvider in TestNG, let’s move further and understand how actually it works through Parameterization.

Parameterization using DataProvider

To fill thousand’s of web forms using the testing framework you need a different methodology which will give you a very large dataset in the single execution flow. This data-driven concept is achieved by @DataProvider annotation in TestNG. It has only one attribute ‘name’. If you do not specify the name attribute then the DataProvider’s name will be the same as the corresponding method name. That’s how DataProvider eases the task of testing multiple sets of data. Now let’s dive into the practical implementation of DataProvider in TestNG with the help of examples.

DataProvider Examples

Example I: In this example, I will pass the data from getData() method to DataProvider. I  will also send data into 3 rows and 2 columns ie. I will pass three different usernames and passwords. Let’s take a look at the code below.

package co.edureka.pages;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderExample{

//This test method declares that its data should be supplied by the DataProvider
// "getdata" is the function name which is passing the data
// Number of columns should match the number of input parameters
@Test(dataProvider="getData")
public void setData(String username, String password)
{
System.out.println("your username is::"+username);
System.out.println("your password is::"+password);
}

@DataProvider
public Object[][] getData()
{
//Rows - Number of times your test has to be repeated.
//Columns - Number of parameters in test data.
Object[][] data = new Object[3][2];

// 1st row
data[0][0] ="user1";
data[0][1] = "abcdef";

// 2nd row
data[1][0] ="user2";
data[1][1] = "xyz";

// 3rd row
data[2][0] ="user3";
data[2][1] = "123456";

return data;
}
}

When you execute the above code, each data set that you pass will be considered as a test method. As I have passed three sets of data to the DataProvider, it will display the result as shown below:

Output of DataProvider Example1 - DataProvider in TestNG - Edureka

Now, let’s understand one more example where I will be providing the data into the Facebook website using DataProvider.

Example II: In this example, I want to type username and password and login into facebook.com. This test case should run 2 times with a different set of data(data we have provided in the 2D array)

Let’s implement the same and check how it works

package co.edureka.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestDDT {
// this will take data from dataprovider which we created
@Test(dataProvider="testdata")
public void TestFireFox(String uname,String password){
System.setProperty("webdriver.gecko.driver", "C:geckodriver-v0.23.0-win64geckodriver.exe");
WebDriver driver = new FirefoxDriver();
// Maximize browser
driver.manage().window().maximize();
// Load application
driver.get("http://www.facebook.com");
// clear email field
driver.findElement(By.id("email")).clear();
// Enter usename
driver.findElement(By.id("email")).sendKeys(uname);
// Clear password field
driver.findElement(By.id("pass")).clear();
// Enter password
driver.findElement(By.id("pass")).sendKeys(password);
}

@DataProvider(name="testdata")
public Object[][] TestDataFeed(){

// Create object array with 2 rows and 2 column- first parameter is row and second is //column
Object [][] facebookdata=new Object[2][2];

// Enter data to row 0 column 0
facebookdata[0][0]="username1@gmail.com";
// Enter data to row 0 column 1
facebookdata[0][1]="Password1";
// Enter data to row 1 column 0
facebookdata[1][0]="username2@gmail.com";
// Enter data to row 1 column 0
facebookdata[1][1]="Password2";
// return arrayobject to testscript
return facebookdata;
}
}

When you execute the above code, Chrome Driver will launch Google Chrome, navigate through facebook.com and enter two sets of data i.e. login credentials. Basically, this will check whether the DataProvider was successfully able to provide the data into facebook.com or not. If you have 100s and thousands of datasets then you can use Excel sheet to store the data and then provide the path of the excel file in your code. That will help you to process all the records present in the excel file.

This marks the end of this article on DataProvider in TestNG. I hope you guys enjoyed this article and understood how to feed the data and run the test cases. Now, if you want to get more insights into Selenium, you can check out the article on Selenium Tutorial.

If you found this “DataProvider in TestNG” relevant, check out the Selenium Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.