Make your Script Data Driven


Make your Script Data Driven


In this lesson we will look at how to incorporate the data driven functionality in your script. it is one of most important or you can say mandatory requirement while creating the automation script. In data driven script we pass the input, expected output and other required values which are definite in nature from outside of the script. for an instance Excel. this makes your script more flesible and you can run the same script just by passing different values to it. it increases you functional coverage and provide facility for test application for different datasets.

You can store the data in various forms

  • Excel
  • Text file
  • xml
  • Database
  • etc.

in this lesson, we will take most commonly used form which is Excel.

Step 1: Design the Excel :  first of all you need to design the excelsheet which you will use as datasheet it is most important other wise you will not able to develop the logic to extract values and use the same in the code.

lets say in my code i want to enter title of post.

my code is:

driver.findElement("title")).sendKeys("This is My Title");

so if i defined any variable for title then i code will look like this..

String postTitle = " This is My Title";
driver.findElement("title")).sendKeys(postTitle );

but still i cannot pass value from outside of the script and can not make real data driven script. here what i need? I need Variable and Value. Variable name should be fixed inside my script which i can use as many times as i want and need value which i want to send from external sheet.

so i will use following format to design my sheet. here i have used rows to define variables, you can use columns as well.

data driven excel


Step 2: Code to Retrieve the Data from Excel and store it.

here we are going to use two Java APIs.

  1. JXL : to read and write the excel. you can also use POI but JXL usage is simple than POI so we will use JXL.
  2. HashMap Class in java. this class is basically store values in key value fashion.

here is code to read the excel and store it HashMap:

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class DataReader {
 public static Map<String, String> data;
 public static Map testcaseid() throws IOException, BiffException
 // create map to store web elements
 data = new HashMap<String, String>();
 Workbook workbook = Workbook.getWorkbook(new"File path")); // it should be xls file and not xlsx.
 Sheet sheet = workbook.getSheet(0); // Sheet index , Sheet1 = 0, Sheet2 =1 etc
 int rowcount = sheet.getRows();
 System.out.println("Datasheet inputs");
 for(int i=1;i<rowcount;i++)

 Cell ObjectName = sheet.getCell(0, i); 
 Cell ObjectValue = sheet.getCell(1, i);
 data.put(ObjectName.getContents(), ObjectValue.getContents());
 System.out.println("Input VariableName: "+ObjectName.getContents()+"Variable Value from Excel::"+ ObjectValue.getContents() );
 return data;

Above method will return data from excel.

Step 3: How to use this data in test case:  Following the code where you can use the above  stored values in the code.

// import the class where you have written data extraction code ( code in step 3) like import pagekage name.DataReader;

public class Delete {
 private WebDriver driver;
 private String baseUrl;
 public static Map<String, String> data;

 public void setUp() throws Exception {
 data = DataReader.testcaseid();
 driver = new FirefoxDriver();
 baseUrl = "";
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 public void testDelete() throws Exception {




I hope this will clear you the how to do data driven code.

Leave A Comment