Skip to main content

Development

Data driven testing using Selenium

Let’s see what Data Driven testing is?

“It is an automation framework where test input and/or output values are read from data files. The different data files may include ODBC sources, csv files, Excel files, ADO objects, etc. The data is then loaded into variables in recorded or manually coded scripts.”

Now let’s take an example which shows why do we need data driven testing?

Suppose there is an online store, where in multiple items are displayed with their attributes. The customer who visits the store online, needs to enter the following information in order to add the product into the cart: –

–       Name

–       Product

–       Quantity

–       Date of Purchase

–       Street

–       City

Let’s look at Automation (Junit) pseudo code to automate the above mentioned process: –

/* Packages to import */

Public class DemoScript

{

// code to open the application that needs to be automated.

selenium.type (“Name”, “John”);

selenium.type (“Product”, “FamilyAlbum”);

selenium.type (“Quantity”, “5”);

selenium.type (“Date of Purchase”, “20/04/2008”);

selenium.type (“Street”, “13 park street”);

selenium.type (“City”, “Sydney, Australia”);

selenium.click(“Submit”);

}

The above code will input all the mentioned values into the required fields in the web page.

So In order to submit a request for 1 customer we have written 7 lines of code. Now say suppose there are 100 customers willing to buy 100 different products. We need to write 700 lines of code, which will make our script quite uneasy to maintain. Hence we can use the Data Driven approach as follows:-

We can write only those mandatory 7 lines of code required to complete one transaction and make that piece of code to repeat 100 times every time taking a new customer and choosing the desired product. Let’s look at the task that needs to be performed: –

Objective: – Enter requests of 100 customers with different products as per the requirement by following the Data Driven approach.

Resources: – Selenium, Apache POI library.

Solution: – First of all we need to set up testdata in an external Excel file (TestBook.xls) as shown below. (Sample screenshot shown is for 10 data, we can configure that to 100).

TextBook.xls

TextBook.xls

We can ask our Selenium script to communicate with the external file and take input data from the same. We have an existing library called Apache POI. This is a powerful library which can perform read/write operations on Excel file.

You can download the required jar file in the below location: –

http://poi.apache.org/download.html

Below is the complete code to perform the task: –

 

package Demo;

import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 

public class ExcelInt {

private Selenium selenium;

HSSFWorkbook workbook;

@Before

public void setUp() throws Exception {

selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “<URL of the application>”);

selenium.start();

}

@Test

public void testExcelInt() throws Exception {

selenium.windowMaximize();

selenium.open(“/”);

//path of your Excel file located in the system

FileInputStream fileInputStream = new FileInputStream(“D:\\Demo1.xls”);

workbook = new HSSFWorkbook(fileInputStream);

HSSFSheet worksheet = workbook.getSheet(“Sheet1”);

//The below code will execute till it identifies the last row in Excel containing data

for (int i=0;i<=worksheet.getLastRowNum();i++)

{

HSSFRow row1 = worksheet.getRow(i);

HSSFCell cellA1 = row1.getCell((short) 0);

String a1Val = cellA1.getStringCellValue();

selenium.type (“Name”, row1.getCell((short)0).getStringCellValue());

selenium.type (“Product”, row1.getCell((short)1).getStringCellValue());

selenium.type (“Quantity”, row1.getCell((short)2).getStringCellValue());

selenium.type (“Date of Purchase”, row1.getCell((short)3).getStringCellValue());

selenium.type (“Street”, row1.getCell((short)4).getStringCellValue());

selenium.type (“City”, row1.getCell((short)5).getStringCellValue());

selenium.click(“Submit”);

}

}

}

@After

public void tearDown() throws Exception {

selenium.stop();

}

}

//The above code will take 1st row in particular from the data sheet then take all values from all the columns (cells) of 1st row. Then moving on to 2nd row and perform the same operation. This will go on till it finds a blank value in Excel data row (i.e. End of file).

Using Apache POI library we can create our customized report as well. Like we have asked our java code to read data from Excel file, also we can insert data into the same Excel sheet.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sohit Kanwar

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram