Can some one please provide codes/logic to retrieve Excel (.xlsx
) data. I need the data to be retrieved in such a way that I can get the value and pass it any where in code for testing web page. This is the code how I need to get:
driver.findElement(By.id("")).sendkeys(getExceldata(row, column));
I just need to define the row and column and it should get the data from Excel sheet by using any method like getExceldata(1,2)
public String cellValue(String filepath,String sheetname,int r,int c)
{
try{
FileInputStream fis = new FileInputStream(new File(filepath));
book = WorkbookFactory.create(fis);
sh = book.getSheet(sheetname);
System.out.println(sh.getSheetName());
row = sh.getRow(r);
cell = row.getCell(c);
return cell.getStringCellValue();
}catch(Exception e)
{
return null;
}
}
public int getRows(String filepath,String sheetname)
{
try{
FileInputStream fis= new FileInputStream(filepath);
book= new XSSFWorkbook(fis);
return book.getSheet(sheetname).getLastRowNum();
}
catch(Exception e)
{
return 0;
}
----------------------------New class below--------------------------------
WebDriver driver;
Excel excel= new Excel();
public static String filepath="âĒD:\\Chinmaya Work\\Work Space\\simpleProject\\src\\Newcustomerdata.xlsx";
public static String sheetname="newcusotomer";
public void setUp() throws InterruptedException
{
driver=new FirefoxDriver();
driver.get("http://www.demo.guru99.com/V4/index.php");
Thread.sleep(5000);
}
public void loginToApplication()
{
Homepagegurru99 home=new Homepagegurru99(driver);
home.enterUsername("mngr45812");
home.enterPassword("chinu@221");
home.clickLoogin();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
}
public void gotoNewcustomepage()
{
Dashbardgurru99 dash=new Dashbardgurru99(driver);
dash.gotonewCustomerpage();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
}
public void createNewcustomer()
{
Newcustomeradd create=new Newcustomeradd(driver);
int rowCount=excel.getRows(filepath, sheetname);
System.out.println(rowCount);
create.eneterCustomername(excel.cellValue(filepath, sheetname, 2,0));
create.selectSex();
create.enterDob(excel.cellValue(filepath, sheetname, 3,0));
create.enterAddress(excel.cellValue(filepath, sheetname, 4,0));
create.enterCity(excel.cellValue(filepath, sheetname, 5,0));
create.enterState(excel.cellValue(filepath, sheetname, 6,0));
create.enterPin(excel.cellValue(filepath, sheetname, 7,0));
create.enterMobileno(excel.cellValue(filepath, sheetname, 8,0));
create.enterEmail(excel.cellValue(filepath, sheetname, 9,0));
create.enterPassword(excel.cellValue(filepath, sheetname, 10,0));
create.clickSubmit();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
driver.switchTo().alert().accept();
}
public void logOut()
{
driver.findElement(By.linkText("Log out")).click();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
driver.switchTo().alert().accept();
driver.close();
}