开发者

No Alert is present/No modal dialog found -- WebDriver unable to catch JS error

开发者 https://www.devze.com 2023-04-12 17:05 出处:网络
I am automating an \"Add Employee\" form-based test for our production site. Whenan idis entered, or an email , or a name, that already exists, then, a service error is p开发者_Python百科opped up that

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 p开发者_Python百科opped 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:

No Alert is present/No modal dialog found -- WebDriver unable to catch JS 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. 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. 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.


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();.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号