Data Driven Framework

In Data Driven framework the test data is kept separate from the actual scripts. So, if any change occurs in test data or if you have to test the script with multiple sets of test data, then no change is required in the script. This data can be kept in some external file like Excel Sheet or CSV file, or some database.

For example:
If we have to test a scenario like a login into facebook with some ten credentials that can be done using data-driven approach, keeping all these credential in some external file like an excel sheet.

Let us now design data-driven framework solution for above example. For this, we will be using Excel sheet, TestNG, Selenium WebDriver and APACHE POI.

Before, proceeding with this article, it is advisable to cover TestNG and Working with Excel Sheet.

Scenario to test with Data-Driven Framework:

  1. Open any browser say, Chrome.
  2. Navigate to http://www.facebook.com
  3. Read data from an excel sheet which has ten different pairs of credentials.
  4. Login to Facebook using these ten pairs of credentials.
Classes created:
  • Facebook login – which has a method to invoke browser, login into Facebook and close the browser.
  • Excel Driver – This class has methods to work with an excel sheet. Here, I am using the same class which we designed in the last article i.e. Working with Excel using POI apache API.

Code:

Excel Sheet which has Test Data:

data-driven-testing

Facebook Project:

In the below code there are four methods written

  1. setup – This method is with @BeforeMethod annotation of TestNG which means it will be executed before every test case. Here, we are simply invoking the chrome browser, setting up few configurations and then navigating to Facebook.
  2. facebook_login – This is the actual scenario to be executed, here we are passing test data to this method using dataProvider.
  3. getData – written with an annotation @DataProvider this method provides test data reading from an excel sheet. To read data from excel sheet we are using ExcelDriver class created in the last article.
package testNG;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class FacebookProject {
		
		WebDriver Driver;
		
		@BeforeMethod 
		public void setup(){
			
			String url = "http://www.facebook.com";
			System.setProperty("webdriver.chrome.driver", "C:\\Users\\sdhingra\\workspace\\libs\\chromedriver.exe");
			Driver = new ChromeDriver();
			Driver.manage().window().maximize();
			Driver.manage().deleteAllCookies();
			Driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
			Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			Driver.get(url);
			
		}
		
		
		@Test (dataProvider="getData", enabled= true)
		public void facebook_login(String username, String password){
			
			Driver.findElement(By.id("email")).sendKeys(username);
			Driver.findElement(By.id("pass")).sendKeys(password);
			Driver.findElement(By.id("u_0_l")).click();
			
		}
		
		
		@DataProvider
		public Object[][] getData(){
			
			String sFileName = "C:\\Users\\sdhingra\\Desktop\\TestData.xlsx";
			String sSheetName = "TestData";
			ExcelDriver excel = new ExcelDriver();
			excel.openExcelSheet(sFileName);
			int iRowCount = excel.getRowCountoFSheet(sSheetName);
						
			Object[][] data = new Object[iRowCount][2];
			
			for(int iRow=1; iRow<=iRowCount; iRow++){
				for(int iCell =1; iCell<=2; iCell++){
					data[iRow-1][iCell-1] = excel.getCellData(sSheetName, iRow, iCell);
				}
			}
			
			return data;
		}
		
		@AfterMethod
		public void tearDown(){
			Driver.quit();
		}
}

Working with Excel:

package commonLibs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDriver {
	
	private InputStream oFileReader;
	private OutputStream oFileWriter;
	private Workbook oExcelWorkbook;
	private String sExcelFileName;
	
	public void createExcelWorkbook(String sFilename){
		
		try {
			
			sFilename = sFilename.trim();
			
			if(sFilename.isEmpty()){
				throw new Exception("Invalid file name..");
			}
			
			if(new File(sFilename).exists()){
				throw new Exception("File already exists");
			}
			
			if(sFilename.endsWith("xlsx")) {
				oExcelWorkbook = new XSSFWorkbook();
			} else if(sFilename.endsWith("xls")){
				oExcelWorkbook = new HSSFWorkbook();
			} else {
				throw new Exception("Invalid File Extension...");
			}
			
			oFileWriter = new FileOutputStream(sFilename);
			oExcelWorkbook.write(oFileWriter);
			oFileWriter.close();
			oExcelWorkbook.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	//----------------------------------------------------------------------------
	
	public void openExcelSheet(String sFileName){
		try {
			
			//Trim check
			sFileName = sFileName.trim();
			
			//Empty Check
			if(sFileName.isEmpty()){
				throw new Exception("File Name not specified");
			}
			
			//To verify if the file exist or not
			if(! (new File(sFileName)).exists()){
				throw new Exception("File doesnot exist.. ");
			}
			
			//Reading a file 
			oFileReader = new FileInputStream(sFileName);
			sExcelFileName = sFileName;
			
			//Reading a file and converting it in excel format
			oExcelWorkbook = WorkbookFactory.create(oFileReader);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//----------------------------------------------------------------------------
	
	public void save(){
		try {
			
			oFileWriter = new FileOutputStream(sExcelFileName);
			oExcelWorkbook.write(oFileWriter);
			
			oFileWriter.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//----------------------------------------------------------------------------
	
	
	public void saveAs(String sFileNewName){
		try {
			sFileNewName = sFileNewName.trim();
			if(sFileNewName.isEmpty()){
				throw new Exception("File name does not exists");
			}
			
			if((new File(sFileNewName)).exists()){
				throw new Exception("File already exists");
			}
			
			oFileWriter = new FileOutputStream(sFileNewName);
			oExcelWorkbook.write(oFileWriter);
			
			oFileWriter.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//----------------------------------------------------------------------------
	
	public void close(){
		try {
			
			oExcelWorkbook.close();
			oFileReader.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//----------------------------------------------------------------------------
	
	public void createSheet(String sSheetName){
		try {
			
			sSheetName = sSheetName.trim();
			
			if(sSheetName.isEmpty()){
				throw new Exception("Sheet name not specified..");
			}
			
			Sheet oSheet;
			
			oSheet = oExcelWorkbook.getSheet(sSheetName);
			
			if(oSheet != null){
				throw new Exception("Sheet Already exist...");
			}
			
			oExcelWorkbook.createSheet(sSheetName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//----------------------------------------------------------------------------
	
	public int getRowCountoFSheet(String sSheetName){
		try {
			
			sSheetName = sSheetName.trim();
			
			if(sSheetName.isEmpty()){
				throw new Exception("Sheet name not specified..");
			}
			
			Sheet oSheet;
			
			oSheet = oExcelWorkbook.getSheet(sSheetName);
			
			if(oSheet == null){
				throw new Exception("Sheet doesnot exist...");
			}
			
			return oSheet.getLastRowNum();
			
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	//----------------------------------------------------------------------------
	
	public int getCellCount(String sSheetName, int iRow){
		try {
			
			sSheetName = sSheetName.trim();
			
			if(sSheetName.isEmpty()){
				throw new Exception("Sheet name not specified..");
			}
			
			Sheet oSheet;
			
			oSheet = oExcelWorkbook.getSheet(sSheetName);
			
			if(oSheet == null){
				throw new Exception("Sheet doesnot exist...");
			}
			
			if(iRow < 1){
				throw new Exception("Row Index start from 1");
			}
			
			Row oRow;
			
			oRow = oSheet.getRow(iRow);
			if(oRow == null ){
				return 0;
			} else {
				return oRow.getLastCellNum();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	//----------------------------------------------------------------------------
	
	public String getCellData(String sSheetName, int iRow, int iCell){
		
		try {
			
			sSheetName = sSheetName.trim();
			
			if(sSheetName.isEmpty()){
				throw new Exception("Sheet name not specified..");
			}
			
			Sheet oSheet;
			
			oSheet = oExcelWorkbook.getSheet(sSheetName);
			
			if(oSheet == null){
				throw new Exception("Sheet doesnot exist...");
			}
			
			if(iRow < 1 || iCell < 1){
				throw new Exception("Row and cell Index start from 1");
			}
			
			Row oRow;
			
			oRow = oSheet.getRow(iRow-1);
			if(oRow == null ){
				return "";
			} 
			
			Cell oCell;
			
			oCell = oRow.getCell(iCell-1);
			
			if(oCell == null ){
				return "";
			} else {
				
				if(oCell.getCellType() == Cell.CELL_TYPE_NUMERIC){
					return String.valueOf((int) oCell.getNumericCellValue());
			} else {
				return oCell.getStringCellValue();
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	//----------------------------------------------------------------------------
	
	public void setCellData(String sSheetName, int iRow, int iCell, String sValue){
		try {
			
			sSheetName = sSheetName.trim();
			
			if(sSheetName.isEmpty()){
				throw new Exception("Sheet name not specified..");
			}
			
			Sheet oSheet;
			
			oSheet = oExcelWorkbook.getSheet(sSheetName);
			
			if(oSheet == null){
				throw new Exception("Sheet doesnot exist...");
			}
			
			if(iRow < 1 || iCell < 1){
				throw new Exception("Row and cell Index start from 1");
			}
			
			Row oRow;
			
			oRow = oSheet.getRow(iRow-1);
			if(oRow == null){
				oSheet.createRow(iRow-1);
				oRow = oSheet.getRow(iRow-1);
			} 
			
			Cell oCell;
			
			oCell = oRow.getCell(iCell-1);
			
			if(oCell == null ){
				oRow.createCell(iCell-1);
				oCell = oRow.getCell(iCell-1);
			} 
			oCell.setCellValue(sValue);
		}
			
			
		 catch (Exception e) {
			e.printStackTrace();
		
		}
	
	}
	
//----------------------------------------------------------------------------
}

Here, in this framework we worked with only one scenario, considering it a base, you can write multiple scripts to test with multiple test data.

This can also be extended to use POM (Page Object Model) design pattern, which I will be covering in next tutorial.

PS: For any questions queries or comment feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning ?

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: