我正在使用 POM 框架并将测试数据保存在属性文件中(此代码运行没有任何问题),但根据当前要求,我应该将测试数据保存在 Excel 文件中。根据我的代码,数据是从 Excel 读取的,但值没有发送到 chrome(我通过在控制台中打印值进行交叉检查),当我调试时,我知道数据返回空值。问题在于 p.load(fs1);由于数据未加载而线路。
// below code is for properties file and its working without any issue.
/* public static String readTestData(String key) throws IOException {
String filename = "testData";
String path = System.getProperty("user.dir") + "/data/testData.properties";
if (path == null || path.length() == 0) {
path = System.getProperty("user.dir") + "/data/" + filename + ".properties";
}
Properties p = new Properties();
FileInputStream fs = new FileInputStream(path);
System.out.print("File Input Stream value is "+fs);
p.load(fs);
System.out.println("Value of login username is "+(String)p.get(key));
return (String) p.get(key);
}*/
// Below code is for reading test data from xlsx
public static String readTestData(String key) throws IOException {
String filename = "testData";
String path = System.getProperty("user.dir") + "/data/testData.xlsx";
if (path == null || path.length() == 0) {
path = System.getProperty("user.dir") + "/data/" + filename + ".xlsx";
}
Properties p = new Properties();
FileInputStream fs = new FileInputStream(path);
Workbook SapWorkbook = null;
StringBuffer sbf = new StringBuffer();
SapWorkbook = new XSSFWorkbook(fs);
Sheet SapSheet = SapWorkbook.getSheet("Sheet1");
int rowCount = SapSheet.getLastRowNum()-SapSheet.getFirstRowNum();
for (int i = 0; i < rowCount+1; i++) {
Row row = SapSheet.getRow(i);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print Excel data in console
sbf.append(row.getCell(j).getStringCellValue());
System.out.print(row.getCell(j).getStringCellValue()+"|| ");
}
System.out.println();
}
byte[] bytes = sbf.toString().getBytes();
ByteArrayInputStream fs1 = new ByteArrayInputStream(bytes);
p.load(fs1);
System.out.println("Value of login username is "+(String)p.get(key));
return (String) p.get(key);
}
public static void enterText(String key, String data) throws IOException, InterruptedException {
try {
waitForPresenceAndVisibilityOfElementry(readobjectRepo(key));
WebElement ele = driver.findElement(By.xpath(readobjectRepo(key)));
ele.clear();
Thread.sleep(1200);
System.out.println("about to read Base page");
ele.sendKeys(readTestData(data));
System.out.println("data read");
startExtent.log(LogStatus.PASS, "Entering data.. " + readTestData(data) + " is sucessful");
Thread.sleep(1200);
} catch (Exception e) {
e.printStackTrace();
reportFailure("Click on element is unsucessful");
}
}
<小时/>
In the console result.
loginUserName|| ABCD@gmail.com||
loginPassword|| abc@1a||
Value of login username is null
最佳答案
您的代码看起来很困惑,需要进行一些清理。该 block 毫无意义:
String filename = "testData";
String path = System.getProperty("user.dir") + "/data/testData.xlsx";
if (path == null || path.length() == 0) {
path = System.getProperty("user.dir") + "/data/" + filename + ".xlsx";
}
你只是用不同的方式对同一件事进行硬编码,坚持下去
String path = System.getProperty("user.dir") + "/data/testData.xlsx";
目前还不清楚您要在这里做什么,但我猜您想要第二列中与第一列中的键关联的值。创建属性对象的所有内容似乎完全过时了,因此我将您的代码重写为:
Workbook SapWorkbook = new XSSFWorkbook(fs);
Sheet SapSheet = SapWorkbook.getSheet("Sheet1");
int rowCount = SapSheet.getLastRowNum() - SapSheet.getFirstRowNum();
for (int i = 0; i < rowCount + 1; i++) {
Row row = SapSheet.getRow(i);
if (key.equals(row.getCell(0).getStringCellValue())) {
return row.getCell(1).getStringCellValue();
}
}
throw new IllegalArgumentException(String.format("%s not found!", key));
现在将滚动浏览每一行,搜索“键”的第一个实例,然后返回关联的“值”。如果找不到 key ,则会抛出异常。将其全部打包为一个方法:
public static String readTestData(String key) throws IllegalArgumentException, IOException {
String path = System.getProperty("user.dir") + "/data/testData.xlsx";
FileInputStream fs = new FileInputStream(path);
Workbook SapWorkbook = new XSSFWorkbook(fs);
Sheet SapSheet = SapWorkbook.getSheet("Sheet1");
int rowCount = SapSheet.getLastRowNum() - SapSheet.getFirstRowNum();
for (int i = 0; i < rowCount + 1; i++) {
Row row = SapSheet.getRow(i);
if (key.equals(row.getCell(0).getStringCellValue())) {
return row.getCell(1).getStringCellValue();
}
}
throw new IllegalArgumentException(String.format("%s not found!", key));
}
关于java - 从excel中读取测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56422731/