简体   繁体   中英

How to read test data from excel in selenium

I try to read test data from excel file and that data is use in sendkeys. This is my code but it gives me error in line FileInputStream file = new FileInputStream (new File("C:\\\\Documents and Settings\\\\Deepa\\\\Desktop\\\\testdata.xls")); that The constructor FileInputStream(File) is undefined. Can somebody help me.

import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 
import jxl.Workbook; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import com.gargoylesoftware.htmlunit.javascript.host.file.File;

...

WebElement login=driver.findElement(By.cssSelector("a[data-target='#login-box']"));
login.click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

// Workbook wb = Workbook.getWorkbook(new File("samplefile.xls"));
FileInputStream file = new FileInputStream (new File("C:\\Documents and Settings\\Deepa\\Desktop\\testdata.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Workbook wBook = Workbook.getWorkbook(new File("E:\\Testdata\\ShellData.xls"));
HSSFSheet sheet = workbook.getSheetAt(0);
//get sheet
// jxl.Sheet Sheet = wBook.getSheet(0); 
int i;
//Now in application i have given my Username and Password input in following way
driver.findElement(By.id("uname_h")).sendKeys((CharSequence[]) Sheet.getCell(0, i));
driver.findElement(By.id("password_h")).sendKeys((CharSequence[]) Sheet.getCell(1, i));
driver.findElement(By.id("health_btn")).click();

The problem is that you try to instantiate the FileInputStream with a com.gargoylesoftware.htmlunit.javascript.host.file.File which is indeed undefined. If you need that import then you should change the problematic line to

FileInputStream file = new FileInputStream (new java.io.File("C:\\Documents and Settings\\Deepa\\Desktop\\testdata.xls"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM