Data Driven test suite using TestNG framework

What is Data Driven Testing? 
Consider an example of account open to the bank. Now there are various possible scenarios for account open itself. But notice that the flow remains same, you will pass through the same screens but only you will enter different sets of data. 
Now, writing a test case for each set of data for same type of functionality it not a good idea, so what we can do here is, we can write one generic test case and pass the all sets of data to this test case. Here the role of data driven testing comes in to picture.
Data driven testing is where the test input and the expected output results are stored in a separate data file (normally in a tabular format) so that a single driver script can execute all the test cases with multiple sets of data.
The driver script contains navigation through the program, reading of the data files and logging of the test status information.
Test data (data file) can be stored in one or more central data sources like excel file, xml file or can be text file.

Below snap explains this – 

Advantages:
• This framework reduces the number of overall test scripts needed to implement all the test cases.
• Less amount of code is required to generate all the test cases.
• Offers greater flexibility when it comes to maintenance and fixing of bugs.
• The test data can be created before test implementation is ready or even before the system to be tested is ready.
Let us see now. How data driven testing can be achieved using TestNG framework.
Below steps explains how to create data driven testing framework.
              
              1.      Create a script with a set of constant test data
              2.      Replace constant test data with some variables.
              3.      Create multiple sets of test data in data storage like excel or xml file.
              4.      Assign the values to variable.
Consider a very simple example of emi calculator. We will write one generic test case & will pass different sets of data to the test case.
My Input Excel file, containing test data, looks like –

Below piece of code explains how can we pass multiple sets of test data to a single test case.
We need to use @DataProvider annotation of testing. This will help to provide the data to test case.
Note that, @DataProvider should always return an array of object.
Give some meaningful name to @DataProvider annotation.
Use the same name in @Test annotation.
Note: we need to add Apache POI jar files in project build path
Step1: Write test case with variables in it.
Step2: Use apache POI to read excel file, and return an array of object (because @DataProvider annotation should always return an array of Object).
Step3: Give some meaningful name to @DataProvider annotation.  Ex.    @DataProvider(name = “data”)
Step4: Use the same name in @Test annotation.   Ex.       @Test(dataProvider= “data”)
Refer Below code –

package dataDrivenTestingExample;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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 DataDrivenTest {


@Test(dataProvider = "data")
public void DataDrivenTest1(String LoanAmount, String InterestRate, String Tenure){

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://emicalculator.site/");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.findElement(By.xpath("//*[@id='frm-emi']/p[1]/input")).sendKeys(LoanAmount);
driver.findElement(By.xpath("//*[@id='frm-emi']/p[2]/input")).sendKeys(InterestRate);
driver.findElement(By.xpath("//*[@id='frm-emi']/div/div/input")).sendKeys(Tenure);
driver.findElement(By.xpath("//*[@id='frm-emi']/p[3]/input")).click();

driver.close();

}


@DataProvider(name = "data")
public Object[][] testDataSupplier() throws Exception{
//file path where excel file placed, containing test data.
String filePath = "C:UsersPrakashworkspaceLearnTestNGTestDataSheet.xlsx";

//read excel file using file input stream, using Apache POI
FileInputStream fis = new FileInputStream(new File (filePath));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("TestData");

//calculate total number of rows and columns so that we can iterate over it.
int totalNumberOfRows = sheet.getLastRowNum()+1;
int totalNumberOfCols = sheet.getRow(0).getLastCellNum();

//create an object array. which will store the test data from excel file
Object[][] testdata1 = new Object[totalNumberOfRows][totalNumberOfCols];


for (int i = 0; i <totalNumberOfRows; i++ ){
for (int j = 0; j < totalNumberOfCols; j++){

testdata1[i][j] = sheet.getRow(i).getCell(j).toString();
}
}
return testdata1;
}

}
Hope this helps !!!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories