如果测试用例出现故障,如何从文本文件中检索下一个值?
这是我的代码:
public void openFile(){
try{
x = new Scanner(new File("C:\\Project1\\ids.txt"));
public void readFile(){
}
}catch(Exception e){
System.out.println("not find file");
}
while(x.hasNext()){
String a = x.next();
driver.findElement(By.xpath("//*[@id=\"in_member_id\"]")).sendKeys(a);
}
}
如果文件ids.text
的第1行中的值错误,我希望它将第二个值放到第三个值,依此类推。如果它是正确的我希望它继续到文件的最后一个。
答案 0 :(得分:0)
如果您的文件不是非常大,您可以尝试的一种策略是预取所有行并将它们存储在列表中。然后循环并打破作为最终语句,这表示成功意味着您可以停止尝试。这可能看起来像这样:
// Let's just assume the file is always found for example's sake
Scanner in = new Scanner(new File("C:\\Project1\\ids.txt"));
List<String> fileLines = new ArrayList<>();
// Pre fetch all the lines in the file
while (in.hasNextLine()) {
String line = in.nextLine();
if (!line.isEmpty()) {
fileLines.add(line);
}
}
// Try each id until one succeeds and the loop is broken
for (String aLine : fileLines) {
try {
driver.findElement(By.xpath("//*[@id=\"in_member_id\"]")).sendKeys(a);
// Here is where you would check for failures that don't throw an exception, if you need to...
// If this break is reached, then no failures were detected
break;
// If a failure happens that results in an exception
} catch (Exception e) {
System.out.println("An error happened, trying next line");
}
}