我正在使用保存在属性文件中的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
答案 0 :(得分:0)
您的代码看起来很凌乱,可以进行一些清理。该块毫无意义:
with
您只是以不同的方式对同一事物进行硬编码,请坚持
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));