java - 如何在selenium中读取excel中的测试数据

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

最佳答案

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

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

关于java - 如何在selenium中读取excel中的测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024082/

相关文章:

java - 我应该如何继续检查移动数据连接

Java/LibGDX 从用户数据获取 Sprite

java - 对于输入字符串 : Error

python - Selenium Python - 测试页面中是否存在元素

python - 按 id 查找元素在 Chrome Headless 模式下不起作用

JavaFX 和套接字 = 不在 FX 应用程序线程上

有 N 个叶子的 Java 树?

python - 使用selenium时如何修复异常 "Message: timeout (Session info: headless chrome=77.0.3865.50)"?

java - Java中的for循环捕获异常

python - 在 python 中使用 selenium send_keys 复制文本