Site Loader
Auckland, New Zealand
In www.executeautomation.comso far we have seen some series of articles on getting started with Selenium like We saw all these post and many friends who are reading these posts found really interesting and said they are really simple and useful. But in all the post we just saw the very basic stuff on Selenium, but once our test becomes complex, we need to deal with some real codes. Many of my friends who are dealing with manual QA see coding as the weird part in automation, all they want is Click record button, perform some operation, stop recording and replay the same. That’s fine for Selenium IDE, but moving forward we need to make our code more robust to withstand complex applications. There are lots of limitations in using Selenium IDE, some are
  • Using Conditional Statements
  • Using External Data
  • Logging
  • Exception handling
  • Reporting etc
In this topic we are going to deal with one of the most important yet very useful stuff for automation in Selenium with is “Data Driven Testing”. We can drive Selenium testing using data sources like XML, Excel sheets, Database Tables etc. But wait, what if I just need to store some of the application properties like Object Name, Application Window name, global properties etc, do I need to use the data sources you are talking about ? Well, the answer is Yes or No, depending upon the complexity of your application, if your application has very few objects and their properties to be identified are very minimal, you can use “Property file” instead. Here is the sample code to read data from Sample file in JAVA.
public void ReadProperty() throws IOException {
	  System.out.println("In Read Property....");
	  //Create Property Object
	  Properties p = new Properties();
            //Load the Property file available in same package
            //Give the propery name to access
	  String propName = p.getProperty("name");
            //Output the property value
  Now if your application becomes very complex and you rely each and every data to be driven from external data sources, then you SHOULD use data sources like Excel, Database table etc. Here we are going to talk about driving data from Excel sheet using JXL (JAVA EXCEL API). You can read more about JXL from the site, but for now, let’s consider JXL is an API to read and write data from Excel Sheets. We are going to use JXL’s Excel as our Dataprovider to read data from external data source. There are some other API available to read and write data from excel sheet like POI-HSSF and POI-XSSF , but we are going to deal only with JXL here. Before getting started with JXL coding, let’s get to know some basic steps of how to read data from Excel sheet, below is the diagrammatic representation. excelflow Well, here is how the code to start working with JXL. Lets go with the diagrammatic representation. Step 1
import java.util.Date;
import jxl.*; 

Workbook workbook = Workbook.getWorkbook(new File("C:\\data.xls"));
Step 2 Open worksheet
Sheet sheet = workbook.getSheet("sheet1");
We can also give the index of sheet as
Sheet sheet = workbook.getSheet(0);
Step 3 Read the data from Excel Cell
String value = sheets.getCell(Column, Row).getContents();
Here Column is the column number and row is the row number from the cell which you are interested to read data. Step 4 Now close the worksheet
That’s it !!! Karthik KK  

Post Author: Karthik kk

2 Replies to “Data driven testing In Selenium using JXL (Part 1)”

Leave a Reply

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