我正在为我们的生产站点自动执行基于“添加员工”表单的测试。当输入已经存在的 ID、电子邮件或姓名时,会弹出服务错误,显示该员工无法注册。即使出现此框,测试也通过了:(。我尝试在代码中使用 driver.switchTo().alert() 函数。这就是发生的情况:
Alert alert=driver.switchTo().alert();
String text=alert.getText();
System.out.println(text);
使用 Firefox 7 + Selenium WebDriver 2.8.0:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information); duration or timeout: 2.11 seconds
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:48'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.37.6-0.7-desktop', java.version: '1.6.0_26'
Driver info: driver.version: RemoteWebDriver
使用 Chrome + Web 驱动程序:
[1011/131949:ERROR:automation_json_requests.cc(59)] JSON request failed: GetAppModalDialogMessage
with error: No modal dialog is showing
错误快照:
问题:
如果我看不到对话框的文本也没关系。这是唯一会出现的警报。因此,只要知道出现了警报就可以解决我的问题。但是,这两件事都表明不存在警报/模式对话框:(
谢谢。
编辑:我也尝试过这个:
Selenium selenium=new WebDriverBackedSelenium(driver,baseUrl);
System.out.println(selenium.isAlertPresent());
这给出了错误。这不是一个警报框吗?它也不是模态对话框吗?如果不是这些东西,我如何在页面上找到它的存在?
最佳答案
很多时候,Selenium 代码会在浏览器呈现警报之前执行。我倾向于做这样的事情:
Alert getAlert() {
try {
Alert alert = driver.switchTo().alert();
return alert;
} catch (NoAlertPresentException e) {
// no alert to dismiss, so return null
return null;
}
}
我真的不认为这很理想,但目前我不知道 Selenium 中有更好的方法。我倾向于定期从我的测试中盲目地调用它,如果它返回警报,我就会忽略它。如果您希望应用程序发出警报并希望在继续之前将其关闭,请将其包装在 while 循环中。
关于java - 不存在警报/未找到模式对话框 - WebDriver 无法捕获 JS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7722940/