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. Even though this box comes up, the test passes :(. I tried using the driver.switchTo().alert() function in my code. This is what happened:

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.
发布了430 篇原创文章 · 获赞 415 · 访问量 925万+


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客