Data Driven Testing through WebDriver using jxl
Prerequisite
Prerequisite
1- Download jxl jar file and add it in to path
2- Junit jar file
3- Selenium-server-standalone-2.x.jar
Add these three jar file in build path .
2- Junit jar file
3- Selenium-server-standalone-2.x.jar
Add these three jar file in build path .
In general when we say Data Driven then only thing that should come in to mind is thatinput is going to be read from some xls file, xml,csv or some other table oriented file and might be output would also be written in xls,xml or csx file. All read data from various files are stored in variables and finally used by scripts to run the test cases.
Data Driven testing is mainly divided in two part
1- Reading data and storing in to variable
2- Using data stored in variable in to some generic script.
Data Driven testing is mainly divided in two part
1- Reading data and storing in to variable
2- Using data stored in variable in to some generic script.
Reading Data and storing in to variableSince Webdriver don’t have structure like other automation tools like QTP to have its own Data table to store data to run tests in Data Driven framework. So we normally two jar file(Binaries) JXL(Java Excel API) and Apache POI to make Data Driven test framework for WebDriver.
I am using JXL binary in this example. so for reading data from xls file we need to follow these step
I am using JXL binary in this example. so for reading data from xls file we need to follow these step
1- Opening Excel file , so to open excel file we would use these two line. I have created one excel file r.xls and now we would write code to reach to this file and to open this excel sheet.
import java.io.FileInputStream;
import jxl.Workbook;
import java.io.FileInputStream;
import jxl.Workbook;
FileInputStream fi = new FileInputStream(“C:\\Users\\kaushal\\Desktop\\r.xls”);
Workbook w = Workbook.getWorkbook(fi);
In above code
FileInputStream
obtains input bytes from a file in a file system
2- Opening worksheet
import jxl.Sheet;
Sheet sh = w.getSheet(0); or w.getSheet(Sheetnumber)
Here 0 as argument states about firest sheet, if we want to read second sheet then we may use 1 in place of 0
3- Reading data
code used is
code used is
String variable1 = s.getCell(column, row).getContents();
These are 3 steps that is used in reading data from excel sheet
In my example or code I am going to write code to test login to Gmail for various user and Username and Password is save in excel sheet and we will keep reading this data by using loop and we would store these data in two variable username and password
package learing;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Data{
public static void main(String args[]) throws IOException, JXLException,BiffException,FileNotFoundException, InterruptedException{
WebDriver driver = new FirefoxDriver();
Sheet s;
FileInputStream fi = new FileInputStream("D:\\data1.xls");
System.out.println("Brahmi");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(0);
for(int row=1; row <=s.getRows();row++)
{
String username = s.getCell(0, row).getContents();
System.out.println("Username "+username);
driver.get("http://www.gmail.com");
driver.findElement(By.name("Email")).sendKeys(username);
String password= s.getCell(1, row).getContents();
System.out.println("Password "+password);
driver.findElement(By.name("Passwd")).sendKeys(password);
Thread.sleep(10000);
driver.findElement(By.name("signIn")).click();
System.out.println("Waiting for page to load fully...");
Thread.sleep(30000);
}
driver.quit();
}
}
please mail you personal ID to my mail ID : kcprabhacse@gmail.com
ReplyDeleteI am getting Below error :-
ReplyDeleteBrahmi
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:318)
at data.data.main(data.java:27)
hi Yog salunke,
ReplyDeleteYour problem has nothing to do with Selenium, it is more about Excel library you use.suggests that you are trying to access a cell that does not exist in the given sheet.