我的硒测试随机失败。我正在测试一个警报窗口,但似乎随机的selenium测试确实忽略了警报窗口。问题是什么?请帮忙。
我正在使用以下代码
public String checkAlert() {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
String txt = alert.getText();
alert.dismiss();
return txt;
}
答案 0 :(得分:0)
试试这个“自定义”等待处理程序:
public static String waitJSResponse(WebDriver driver, String script) {
String ReturnedValue = null;
int sleeper = 10;
Boolean flag = false;
int timeOut = 30000;
int i = 0;
while ((!flag) & ((i*sleeper)<timeOut)) {
try {
Thread.sleep(sleeper);
ReturnedValue = (String) driver.executeScript(script);
} catch (Exception e) {
flag = false;
i++;
}
if (ReturnedValue != null) {
flag = true;
System.out.println("Overall response time : "+(i * sleeper)+" ms \n\r");
break;
}
}
return ReturnedValue;
}
在您的脚本中(作为String)使用以下代码:
function getAlert() {
var elem = document.scripts;
for (var i = 0, len = elem.length; i < len; i++) {
var txt = elem[i].textContent.match(/alert\(['"]([^'"]+)['"]\)/);
if (txt) { return txt[1]; }
}
}