Selenium Lesson 15 – Make your Script Data Driven

Lesson 15 - Data Driven Script

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(By.id("title")).clear();
driver.findElement(By.id("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(By.id("title")).clear();
driver.findElement(By.id("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.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


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 java.io.File("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);
 if(ObjectName.getContents()!="")
 {
 data.put(ObjectName.getContents(), ObjectValue.getContents());
 System.out.println("Input VariableName: "+ObjectName.getContents()+"Variable Value from Excel::"+ ObjectValue.getContents() );
 
 }
 else
 
 break;
 
 }
 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;


 @Before
 public void setUp() throws Exception {
 data = DataReader.testcaseid();
 driver = new FirefoxDriver();
 baseUrl = "http://desithemes.com/wp-admin/post-new.php";
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }

 @Test
 public void testDelete() throws Exception {

 driver.findElement(By.id("user_login")).clear();
 driver.findElement(By.id("user_login")).sendKeys(data.get("userName")); 

 }

}

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

You might also like More from author

Loading...