Data Driven Framework

Data Driven Framework is an automation testing framework which is used to drive test cases and suites from an external data feed.The data feed can be data sheets like xls, xlsx, and csv files. It enables testers to build both positive and negative test cases into a single test. It is the technique of separating the “data set” from the actual “test case” (code). Since the test case is separated from the data set, we can easily modify the test case of a particular functionality without making wholesale changes to your code. For example, if you want to modify the code for login functionality, then you can modify just that instead of having to also modify any other dependent portion in the same code.

Besides this, you can also easily control how much data needs to be tested. You can easily increase the number of test parameters by adding more username and password fields to the excel file (or other sources).




Data Driven Testing Example

This example will demonstrate how to read the data from excel files and perform data driven testing using selenium. Web Driver does not directly support data reading of excel files. Therefore, one needs to use a plugin such as Apache POI for reading/writing on any Microsoft office document.

  • Download Apache POI Jar files. Download the zip file or tar file as per requirement and place them along with the set of Selenium JARs and configure your build path.

Now let’s understand how to write the first test case. An excel file to read the data from the sheet. We have different combinations of username and password in the sheet.

The task here is to enter all the combinations of username and passwords into the login field in order to test the functionality. Let’s see how to do that.




Here, the target is to enter all these combinations of username and password into the OrangeHRM page as shown below.




Let’s write a code snippet to read the data files.

Step 1: Go to the Eclipse IDE and create a project. Add all the dependencies for TestNG, Selenium and Apache POI.

Step 2: Create a class file to write the functionality.



package DatadrivenTesting;

import org.testng.annotations.Test;

import org.testng.annotations.DataProvider;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.AfterTest;


public class datadriventesting {

WebDriver driver;

@Test(dataProvider = "testdata")

public void test (String username, String password) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "/Users/radha/Downloads/chromedriver");

driver = new ChromeDriver();

driver.get("https://opensource-demo.orangehrmlive.com/");

driver.manage().window().fullscreen();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Thread.sleep(2000);

driver.findElement(By.name("txtUsername")).sendKeys(username);

driver.findElement(By.name("txtPassword")).sendKeys(password);

driver.findElement(By.name("Submit")).click();

Thread.sleep(5000);

Assert.assertTrue(driver.getPageSource().contains("dashboard"));

System.out.println("Login successful");

}

@DataProvider(name="testdata")

public Object[][] testData(){

excelreader config = new excelreader("/Users/radha/Desktop/login_testdata.xlsx");

int rows = config.getRowCount(0);

Object[][]signin_credentials = new Object[rows][2];


for(int i=0;i<rows;i++)

{

signin_credentials[i][0] = config.getData(0, i, 0);

signin_credentials[i][1] = config.getData(0, i, 1);

}

return signin_credentials;

}

@AfterTest

public void afterTest() {

driver.close();

driver.quit();

}

}




In the above code, there is a “TestData() method” for which an object instance of another class named “ReadExcelFile” has been created . The path to the excel file has been mentioned . Defined a for loop to retrieve the text from the excel workbook. But to fetch the data from the excel file, one needs to write a class file for the same.


package DatadrivenTesting;

import java.io.File;

import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class excelreader {

XSSFWorkbook work_book;

XSSFSheet sheet;

public excelreader(String excelfilePath) {

try {

File s = new File(excelfilePath);

FileInputStream stream = new FileInputStream(s);

work_book = new XSSFWorkbook(stream);

}

catch(Exception e) {

System.out.println(e.getMessage());

}

}

public String getData(int sheetnumber, int row, int column){

sheet = work_book.getSheetAt(sheetnumber);

String data = sheet.getRow(row).getCell(column).getStringCellValue();

return data;

}

public int getRowCount(int sheetIndex){

int row = work_book.getSheetAt(sheetIndex).getLastRowNum();

row = row + 1;

return row;

}

}

In the code above, Apache POI libraries have been used to fetch the data from the excel file. Next, it will point to the data present in the excel file and then enter the relevant username and password to the sign in page.




In the above results screenshot we can see 2 positive test cases passed and the remaining 4 negative test cases failed.


Advantages of Data Driven Testing Framework

  • Can execute the same script with multiple sets of test data.

  • Re-usability of code

  • Improves test coverage

  • Faster Execution

  • Less maintenance

  • Permits better error handling

By incorporating data-driven testing using Selenium, testers can refine their test cases for more efficient execution. This shortens timelines, makes their lives easier and results in more thoroughly tested and better quality software.


Happy Testing!!!


41 views0 comments

Recent Posts

See All

Headless Browser in Python

What is a headless browser? A headless browser can access any website but unlike normal browsers (which you currently use) nothing will appear on the screen. Everything is done on the backend side inv