protractor icon indicating copy to clipboard operation
protractor copied to clipboard

UnexpectedAlertOpenError

Open mvolkmann opened this issue 13 years ago • 22 comments

My web app calls alert on startup if the user is using an old version of IE such as IE8. I need my Protractor test to dismiss that alert. Just attempting to access it throws an UnexpectedAlertOpenError. Here is the line of code.

var dialog = ptor.switchTo().alert();

I was hoping after getting a reference to it, I could close it.

mvolkmann avatar Nov 27 '13 16:11 mvolkmann

There's an example of closing an alert here: https://github.com/angular/protractor/blob/master/spec/basic/actions_spec.js#L18

Since you will sometimes have an alert and sometimes not, you may need to handle promise errors for the situation when there is not an alert.

juliemr avatar Nov 27 '13 22:11 juliemr

Seems like this is resolved, closing for tidiness. Please open a new issue or pull request if there is still an issue.

juliemr avatar Dec 06 '13 15:12 juliemr

Hi julie, we are running protractor tests locally triggered by Jenkins on a build server. Are there any other way to handle somewhat unexpected alerts more generally? Kind of annoying that the browser will stay open and all of the remaining tests will crash after the alert appeared.

I think its generally a bad practice to handle errors with alerts, but unfortunately we have implemented that temporarily so it would be great if there had been a setting to just omit alerts and just fail the test.

enghen10 avatar Jan 29 '14 12:01 enghen10

I suppose you could do a try/catch block and look for webdriver.UnhandledAlertError.

juliemr avatar Feb 01 '14 02:02 juliemr

Hi, I have been fighting recently with this behaviour. I finally solved this using the webdriver Error Handling propagation. Something similar to this:

var alert = ptor.driver.switchTo().alert().then(function(alert) {
    alert.accept();
}, function(error) {
  // Here we can handle the exception if we want
});

More documentation and examples about this can be found on the WebdriverJS User's Guide (search for a "alert()" text and you will find the solution): https://code.google.com/p/selenium/wiki/WebDriverJs

tombatossals avatar Feb 20 '14 21:02 tombatossals

Also having this issue- seems to sometimes work and sometimes not for me.

I have a page where if a user tries to navigate away without saving changes, an alert pops up. I'd like to dismiss this alert.

screenshot 2014-07-24 10 51 40

Here is the code:

...
        # Navigate away from the page
        browser.get "/home"
        # But, there are unsaved changes, so an alert pops up
        # Dismiss the alert
        browser.switchTo().alert().then (alert) ->
          alert.accept()

But the code fails to dismiss the alert:

UnexpectedAlertOpenError: unexpected alert open

Any ideas?

andrewboni avatar Jul 24 '14 17:07 andrewboni

Looks like the issue is that the dialog isn't an alert per se- it's triggered with a window.unbeforeunload

Did some Googling on the issue, but still unable to get it to close properly. There are hacky ways of getting around it, like this:

    browser.executeScript "window.onbeforeunload = function(){};"

But I'd like the right way of doing it. Has anyone solved this issue?

andrewboni avatar Jul 24 '14 19:07 andrewboni

In my case I would like to catch UnexpectedAlertOpenError exceptions. How could I do that?

wcandillon avatar Aug 04 '14 19:08 wcandillon

Anyone have any luck with this?

andrewboni avatar Oct 14 '14 22:10 andrewboni

Having a similar issue. Previously browser.switchTo().alert(); worked for alerts and dialogs (when user navigated away from the page) but seems now it no longer works for dialogs.

facultymatt avatar Nov 01 '14 15:11 facultymatt

Also tried this solution which doesn't work for me:

    browser.getAllWindowHandles().then(function (handles) {
        browser.switchTo().window(handles[0]);
    });

facultymatt avatar Nov 01 '14 15:11 facultymatt

Related to https://code.google.com/p/chromedriver/issues/detail?id=29

facultymatt avatar Nov 01 '14 15:11 facultymatt

I am still unable to use browser.switchTo().alert();

Tried everything and it is not related to chrome driver issue as same is working fine with webdriver-java native APIs. This is blocking usage of protractor and I am afraid I need to revert to webdriver - java API. Please update any luck on this??

vikasgahlaut avatar Jan 14 '15 11:01 vikasgahlaut

Having the same issue here.

Hacky workaround:

browser.executeScript("window.onbeforeunload = function(){};");

Above will close the onbeforenload created dialog, but obviously precludes the ability to test any associated logic and/or behavior related to it. Are there any updates on this issue or if/when it will be resolved?

skidvd avatar Apr 10 '15 13:04 skidvd

Unfortunately, the hack above in https://github.com/angular/protractor/issues/308#issuecomment-91562827 is not working for me. I have another hack that seems to be working:

browser.executeScript( "window.angular.element(window).off('beforeunload');" );

ramdog avatar Sep 10 '15 01:09 ramdog

For anyone who's still struggling, this seems to be a proper solution:

browser.get(url).catch(function () {

    return browser.switchTo().alert().then(function (alert) {

        alert.accept();
        return browser.get(url);

    });

});

fyodorvi avatar Apr 05 '16 22:04 fyodorvi

Thanks @fyodorvi ! Your solution works. I just had to change .catch(...) with .then(null, ...). Probably .catch is only available in newer version (i use 3.0.0)

tomyam1 avatar May 22 '16 15:05 tomyam1

I can confirm @fyodorvi solution works. This ticket should be closed. My exact code for reference:

        browser.getCurrentUrl().then(function(url) {
            browser.navigate().refresh().catch(function() {
                return browser.switchTo().alert().then(function (alert) {
                    alert.accept();
                    return browser.get(url);
                });
            });

        });

inthegarage avatar Jul 08 '16 10:07 inthegarage

How to handle alerts in the application when we are not sure when alerts will come, for intermittent errors/alerts.

Sankannavar avatar Aug 18 '17 10:08 Sankannavar

@Sankannavar that's really the point of testing though is that these are known routes with set-out test criteria that give expected and predictable results. If your application is throwing up a whole bunch of "random" alerts your application isn't working correctly. You need to keep each test simple and perform a single operation, which either does or doesn't give an error alert.

inthegarage avatar Aug 18 '17 10:08 inthegarage