如何从硒中读取excel的测试数据

时间:2016-05-04 09:45:26

标签: java selenium

我尝试从excel文件中读取测试数据,并在sendkeys中使用该数据。 这是我的代码,但它在行FileInputStream file = new FileInputStream (new File("C:\\Documents and Settings\\Deepa\\Desktop\\testdata.xls"));中给出了错误,即构造函数FileInputStream(File)未定义。 有人可以帮助我。

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();

1 个答案:

答案 0 :(得分:0)

问题是您尝试使用FileInputStream实例化com.gargoylesoftware.htmlunit.javascript.host.file.File,这确实是未定义的。如果您需要导入,则应将有问题的行更改为

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