Home Guide How to run Selenium Tests using DataProvider and TestNG?

How to run Selenium Tests using DataProvider and TestNG?

By Neha Vaidya, Community Contributor -

Automation testing has become a fixture in the software development process. No one wants to devote hours of human effort and time when they can get accurate results with the right test scripts, frameworks, and tools.

This is why it is important to understand the different aspects of automated testing. This article deals with the question of how to write and run automated test cases (with Selenium) on a website using TestNG and DataProvider.

Note: TestNG comes up with DataProvider to automate the process of providing test-cases for execution.

DataProvider helps with data-driven test cases that carry the same methods but can be run multiple times with different data sets. It also helps in providing complex parameters to the test methods.

What is a Data Driven Framework?

A data-driven framework stores the test data in a table or spreadsheet format. It allows automation engineers to use a single test script for executing all 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 the creation of both positive and negative test cases into a single test.

In this test automation framework, input data can be stored in a single or multiple data sources like XLS, XML, CSV, and databases.

Before understanding what DataProvider is, let’s first look at parameterization and its two types.

TestNG Parameterization

Parameterization is an execution strategy which runs a test case automatically, multiple times with various input values. The test design comes with the provision to read data from a file or database rather than hard-coded values.

In TestNG, there are two ways by which to achieve parameterization :

  • With the help of Parameters annotation and TestNG XML file
    @Parameters({“name”, “searchKey”})
  • With the help of DataProvider annotation
    @DataProvider(name= “searchProvider”)

This article will focus on the second method.

DataProvider in TestNG

In order to provide the test data, first declare a method which returns the data set in the form of a 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 a DataProvider in another class but the method has to be static.

After adding this method, annotate it using @DataProvider to let TestNG know that it is a DataProvider method. Additionally, 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.

Parameterization using DataProvider

Entering thousands of web forms using the testing framework is tedious. To ease this process, one needs a different process for executing large datasets in single execution flow. This data-driven concept is achieved through @DataProvider annotation in TestNG framework.

It has only one attribute ‘name’. If one does not specify the name attribute then the DataProvider’s name will be the same as the corresponding method name. This is how DataProvider eases the task of testing multiple sets of data.

Let’s understand how it works with an example.

In this example, the tester wants to automate the process of entering username and password, and then logging into twitter.com. This test case should run 2 times with different sets of data (the data provided in the 2D array).

Let’s implement the same and see how it works.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderTest {
// this will take data from dataprovider which we created
@Test(dataProvider="testdata")
public void TestChrome(String uname,String password){
System.setProperty("webdriver.chrome.driver", "Path of the driver");
WebDriver driver = new ChromeDriver();
// Maximize browser
driver.manage().window().maximize();
// Load application
driver.get("https://twitter.com/login");
// clear email field
driver.findElement(By.name("session[username_or_email]")).clear();
// Enter username
driver.findElement(By.name("session[username_or_email]")).sendKeys(uname);
// Clear password field
driver.findElement(By.name("session[password]")).clear();
// Enter password
driver.findElement(By.name("session[password]")).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 [][] twitterdata=new Object[2][2];

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

On executing this program, the driver will launch Chrome, navigate to the Twitter home page and enter the specified combinations of username and password.

Run Selenium Tests on Real Devices for Free

Note: DataProvider can also be used to enter the combinations of data across multiple websites simultaneously.

Running Selenium tests using DataProvider and TestNG is an excellent way to speed up test cycles, establish more thorough automated testing of websites, and create phenomenal user experiences with minimal time, effort, and resources. It should feature prominently in testing pipelines, as it serves to make testers’ lives infinitely easier.

Needless to say, these tests are always best run on real browsers and devices. BrowserStack offers a cloud Selenium Grid of 2000+ real browsers and devices, which testers can access to run Selenium tests. Simply sign up, choose the browser-device-OS combination required, and start testing for free.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free