简体   繁体   English

没有警报/没有找到模态对话框 - WebDriver无法捕获JS错误

[英]No Alert is present/No modal dialog found — WebDriver unable to catch JS error

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. 当输入id,或已存在的电子邮件或名称时,会弹出一个服务错误,表明该员工无法注册。 Even though this box comes up, the test passes :(. I tried using the driver.switchTo().alert() function in my code. This is what happened: 即使这个框出现了,测试通过:(。我尝试在我的代码中使用driver.switchTo()。alert()函数。这就是发生的事情:

Alert alert=driver.switchTo().alert();
String text=alert.getText();
System.out.println(text);

With Firefox 7 + Selenium WebDriver 2.8.0 : 使用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 : 使用Chrome +网络驱动程序:

[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 ? 如果它不是那些东西,我怎么在页面上找到它的存在?

A lot of times what happens is the Selenium code executes before the browser renders the alert. 很多时候发生的事情是Selenium代码在浏览器呈现警报之前执行。 What I tend to do is something like this: 我倾向于做的是这样的事情:

Alert getAlert() {
    try {
        Alert alert = driver.switchTo().alert();
        return alert;
    } catch (NoAlertPresentException e) {
        // no alert to dismiss, so return null
        return null;
    }
}

I don't really think this is ideal, but I don't know of a better way in Selenium at the moment. 我真的不认为这是理想的,但我现在还不知道Selenium有更好的方法。 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. 如果您希望来自应用程序的警报并希望在继续之前将其解除,请将其包装在while循环中。

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. 你要做的是找到ok buttion的xpath并继续。

I have done the same and successfully done it. 我也做了同样的事并成功完成了它。

Find the webelement and give wait between the webelement and the click of 'ok' button. 找到webelement并在webelement和点击'ok'按钮之间等待。

Do not use any alert like driver.switchto.alert.accept(); 不要使用像driver.switchto.alert.accept(); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM