We can perform data driven using excel file with help of Apache POI library.
Here is the very basic program to perform data driven from excel file using Apache POI library.
Detailed explanation for the above program is as follows:
Here is the very basic program to perform data driven from excel file using Apache POI library.
Create a testdata.xls file in the C drive with following details:
Use below code to perform data driven from excel file:Search Text |
hello selenium |
abhishek yadav qa |
Java Source code:
package com.helloselenium.selenium.test; import java.io.*; import java.util.concurrent.TimeUnit; import org.apache.poi.hssf.usermodel.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class DataDrivenUsingExcelFile{ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); driver.manage().window().maximize(); WebElement searchbox = driver.findElement(By.name("q")); try { FileInputStream file = new FileInputStream(new File("C:\\testdata.xls")); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(0); for (int i=1; i <= sheet.getLastRowNum(); i++){ String keyword = sheet.getRow(i).getCell(0).getStringCellValue(); searchbox.sendKeys(keyword); searchbox.submit(); driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS); } workbook.close(); file.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
import java.io.*; import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.*;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
WebElement searchbox = driver.findElement(By.name("q"));
FileInputStream file = new FileInputStream(new File("C:\\testdata.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
for (int i=1; i <= sheet.getLastRowNum(); i++){
String keyword = sheet.getRow(i).getCell(0).getStringCellValue();
searchbox.sendKeys(keyword);
searchbox.submit();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
workbook.close(); file.close();
15 Comments
good work man.
ReplyDeleteReally helpfull .Good line to line explanation
ReplyDeleteGot error in "searchbox.sendKeys(keyword);". How to solve this issue?
ReplyDeleteWhat is the error/exception? Please share the details.
DeleteGot error in sendKeys command
ReplyDeleteWhat is the error/exception? Please share the details.
DeleteHi,
ReplyDeleteI am getting below error for the code. Please help to resolve
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Thanks,
Mayur
These are not errors. These are only log4j warnings and this coming becuase you may using log4j in your project for logging.
Deletepublic String readDataFromExcel(int rowcount,int columncount,String filepath,String Sheetname )
ReplyDelete{
String data=null;
try
{
FileInputStream input= new FileInputStream(filepath);
XSSFWorkbook wb=new XSSFWorkbook(input);
XSSFSheet sh=wb.getSheet(Sheetname);
XSSFRow row=sh.getRow(rowcount);
row.getCell(columncount).toString();
}
catch(Exception e)
{
System.out.println(e);
}
return data;
}
public void writeDataFromExcel(int rowcount,int columncount,String filepath,String Sheetname,String value)
{
try
{
FileInputStream input=new FileInputStream(filepath);
XSSFWorkbook wb=new XSSFWorkbook(input);
XSSFSheet sh=wb.getSheet(Sheetname);
XSSFRow row=sh.getRow(rowcount);
FileOutputStream webdata=new FileOutputStream(filepath);
row.createCell(columncount).setCellValue(value);
wb.write(webdata);
}
catch(Exception e)
{
}
}
Thanks Stephen for posting a framework level method to read and write data from excel.
DeleteI am getting these Errors:
ReplyDeletelog4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
java.io.FileNotFoundException: C:\mydata\Selenium Tests Data\workspace\ExcelJava\src\testData\Test-Data.xlsx (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at appModules.DataDrivenUsingExcelFile.main(DataDrivenUsingExcelFile.java:26)
KIndly help to resolve these.
It seems file is not available at "C:\mydata\Selenium Tests Data\workspace\ExcelJava\src\testData\Test-Data.xlsx". If file is available there please try to do below steps to resolve your problem:
Delete1. Use filepath as in Java format - "C:\\mydata\\Selenium Tests Data\\workspace\\ExcelJava\\src\\testData\\Test-Data.xlsx"
2. You are using .XLSX file for your test data, so please refer to this blog article to drive data from .XLSX file - "http://www.helloselenium.com/2014/09/how-to-perform-data-driven-using-excel.html"
Give example for both read from excel file and result write in excel file..
ReplyDeleteNice explanation sir.
ReplyDeleteGood job sir
ReplyDeleteWhat would you like to add in my list? I look forward to reading your comments below.