不存在警报/未找到模式对话框 - WebDriver 无法捕获 JS 错误
我正在为我们的生产站点自动执行基于“添加员工”表单的测试。当输入已经存在的 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());
这给出了错误。它不是一个警报框吗?它也不是一个模式对话框吗?如果它不是其中任何一个东西,我如何在页面上找到它的存在?
I am automating an "Add Employee" form-based test for our production site. When an id is entered, or an email , or a name, that already exists, then, a service error is popped up that shows the employee cannot be registered. Even though this box comes up, the test passes :(. I tried using the driver.switchTo().alert() function in my code. This is what happened:
Alert alert=driver.switchTo().alert();
String text=alert.getText();
System.out.println(text);
With 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
With Chrome + Web Driver :
[1011/131949:ERROR:automation_json_requests.cc(59)] JSON request failed: GetAppModalDialogMessage
with error: No modal dialog is showing
Snapshot of the error:
Question:
Its fine if I don't get the text of the dialog. This is the only alert that will come up. So, just knowing that an alert has come up will solve my problem. But, both the things say that no alert/modal dialog exists :(
Thanks.
EDIT: I also tried this :
Selenium selenium=new WebDriverBackedSelenium(driver,baseUrl);
System.out.println(selenium.isAlertPresent());
This gave out false. Is it not an alert box ? Is it not a modal dialog either ? If its not any of those things, how do I find its presence on the page ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很多时候,Selenium 代码会在浏览器呈现警报之前执行。我倾向于做的是这样的:
我真的不认为这很理想,但目前我不知道 Selenium 中有更好的方法。我倾向于定期从我的测试中盲目地调用它,如果它返回警报,我就会忽略它。如果您希望应用程序发出警报并希望在继续之前将其关闭,请将其包装在 while 循环中。
A lot of times what happens is the Selenium code executes before the browser renders the alert. What I tend to do is something like this:
I don't really think this is ideal, but I don't know of a better way in Selenium at the moment. I tend to just call this blindly from my tests at regular intervals, and if it returns an alert, I'll just dismiss it. If you are expecting an alert from your app and want to dismiss it before proceeding, wrap it in a while loop.
我在模型对话框中也遇到了同样的问题。
您要做的就是找到 ok 按钮的 xpath 并继续。
我也做了同样的事情并且成功地做到了。
找到 Web 元素并在 Web 元素和单击“确定”按钮之间等待。
不要使用诸如
driver.switchto.alert.accept();
之类的任何警报。I have also faced the same problem with model dialog box.
What you have to do is find the xpath of ok buttion and proceed.
I have done the same and successfully done it.
Find the webelement and give wait between the webelement and the click of 'ok' button.
Do not use any alert like
driver.switchto.alert.accept();
.