Data Driven Selenium Automation Testing using JXL (Part 2)

Data-Driven Testing in Selenium using JXL

We learned what is Data driven Selenium Automation testing and how we can use JXL to read data from Excel Sheets in Part 1 of our previous post. But the question that still needs to be addressed is, how to use JXL for data driven testing? How to use this in the Selenium framework that already exists? What are the steps which have to be taken care of to make the framework a data driven framework?

Well, for this let us not walk through the typical theoretical content that you can always find some way or other. But let’s do it with the real working code.

To perform data driven selenium automation testing, all we need to do is create a reusable library file just for Excel using JXL. The library file needs to have the following basic functionality in hand.

How to use JXL for Selenium Automation Testing?

Services - Best Selenium Automation Testing (Web Driver) Course in Pune  from Pune | ID - 5440339

Step 1

Create a library file with all the below-mentioned functionality

1. Open Excel Sheet

2. Read Excel Sheet Row Count

3. Read Cell value from a specified location

4. Create a Dictionary to store Excel Sheet Column name

5. Create a function to read from the Dictionary

The Source code for selenium automation testing looks like this.

/*
 * Author : Karthik KK
 * Description: Reusable Library file to perform Excel related operations
 * Date : 01/28/2012
 */
package DataDriver;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

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

public class ExcelSheetDriver {

	static Sheet wrksheet;
	static Workbook wrkbook =null;
	static Hashtable dict= new Hashtable();
	//Create a Constructor
	public ExcelSheetDriver(String ExcelSheetPath) throws BiffException, IOException
	{
		//Initialize
		wrkbook = Workbook.getWorkbook(new File(ExcelSheetPath));
		//For Demo purpose the excel sheet path is hardcoded, but not recommended :)
		wrksheet = wrkbook.getSheet("Sheet1");
	}

	//Returns the Number of Rows
	public static int RowCount()
	{
		return wrksheet.getRows();
	}

	//Returns the Cell value by taking row and Column values as argument
	public static String ReadCell(int column,int row)
	{
		return wrksheet.getCell(column,row).getContents();
	}

	//Create Column Dictionary to hold all the Column Names
	public static void ColumnDictionary()
	{
		//Iterate through all the columns in the Excel sheet and store the value in Hashtable
		for(int col=0;col < wrksheet.getColumns();col++)
		{
			dict.put(ReadCell(col,0), col);
		}
	}

	//Read Column Names
	public static int GetCell(String colName)
	{
		try {
			int value;
			value = ((Integer) dict.get(colName)).intValue();
			return value;
		} catch (NullPointerException e) {
			return (0);

		}
	}

}

Next, we are going to create an actual test file that is going to perform the intended operation, here we are going to perform Gmail login functionality.

Step 2

Create a TestNG Class file to perform Gmail Login

This TestNG class file should include

1. Opening a browser with Gmail

2. Perform User Name and password entry with different combinations of value by reading from the Excel sheet

Then, check the source code. It should look like this.

package DataDriver;

/*
 * Author : Karthik KK
 * Description: To perform Gmail Login using Data driven approach
 * Date : 01/28/2012
 */

import java.io.IOException;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ReadDataTest {

  //Global initialization of Variables
  static ExcelSheetDriver xlsUtil;
  WebDriver driver = new InternetExplorerDriver();

  //Constructor to initialze Excel for Data source
  public ReadDataTest() throws BiffException, IOException
  {
		//Let's assume we have only one Excel File which holds all Testcases. weird :) Just for Demo !!!
	    xlsUtil = new ExcelSheetDriver("D:\Data.xls");
	    //Load the Excel Sheet Col in to Dictionary for Further use in our Test cases.
	    xlsUtil.ColumnDictionary();
  }

  @BeforeTest
  public void EnvironmentalSetup()
  {
	  driver.get("http://www.gmail.com");
  }

  @Test
  /*
   * Author : Karthik KK
   * Description : To Perform login operation in Gmail
   */
  public void GmailLoginPage() throws InterruptedException {

	  //Create a for loop.. for iterate through our Excel sheet for all the test cases.
	  for(int rowCnt = 1;rowCnt < xlsUtil.RowCount();rowCnt++)
	  {

		  //Enter User Name by reading data from Excel
		  WebElement userName = driver.findElement(By.name("Email"));
		  userName.clear();
		  userName.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("EmailUserName"), rowCnt));

		  //Enter Password
		  WebElement password = driver.findElement(By.name("Passwd"));
		  password.clear();
		  password.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("Emailpassword"), rowCnt));

		  //Click on the Sign In Button
		  WebElement signin = driver.findElement(By.name("signIn"));
		  signin.click();

		  //Sleep for some time,so that we can see things in action @ Screen :)
		  Thread.sleep(2000);
	  }
  }

}

Step 3

Create an actual Excel Sheet which holds the data to be supplied in the TestNG class in the above step

Just create an Excel sheet, which looks something like this.

excel

Step 4

Try executing the code

So, it’s time now that you try executing the code. Note in the above code we have placed an excel sheet in D: drive. You can place it anywhere in the machine and change the same in code.

Then, your Gmail screen will look like this at the end of the test by executing all the above test data.

gmail

Now, if you wish to download the full working source code, then you may do it from here.

Isn’t it intriguing? If you look forward to taking your Selenium Automation Testing knowledge one step ahead, then you can get started with this Selenium Automation Testing Course. It is a complete course on Automation Testing that will assist you to get into professional, quality Product & Process development with hands-on software automation skills.  So, why not try this course?

Get started here and accelerate your automation career NOW!

If you still find confused on whether Automation Testing using Selenium is what you wish to do, then you can just leave your contact and let our Experts get back to you for further clarifications.

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Download Pokemon Omega Ruby and Alpha Sapphire Rom

I am regular visitor, how are you everybody? This paragraph posted at this
web site is truly fastidious.

aditinagar
aditinagar
6 years ago

Getting this error while running the code
org.testng.TestNGException:
Cannot instantiate class ReadDataTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
… 21 more
Caused by: java.lang.Error: Unresolved compilation problems:
Syntax error on token “;”, , expected
driver cannot be resolved
driver cannot be resolved
driver cannot be resolved
driver cannot be resolved

at ReadDataTest.(ReadDataTest.java:18)
… 26 more

places to visit In Sikkim

Hi! Excellent article.

3
0
Would love your thoughts, please comment.x
()
x