Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnexpectedAlertOpenError #308

Closed
mvolkmann opened this issue Nov 27, 2013 · 22 comments
Closed

UnexpectedAlertOpenError #308

mvolkmann opened this issue Nov 27, 2013 · 22 comments

Comments

@mvolkmann
Copy link

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.

@juliemr
Copy link
Member

juliemr commented Nov 27, 2013

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
Copy link
Member

juliemr commented Dec 6, 2013

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

@juliemr juliemr closed this as completed Dec 6, 2013
@enghen10
Copy link

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.

@juliemr
Copy link
Member

juliemr commented Feb 1, 2014

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

@tombatossals
Copy link
Contributor

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

@andrewboni
Copy link

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
Copy link

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?

@wcandillon
Copy link

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

@andrewboni
Copy link

Anyone have any luck with this?

@facultymatt
Copy link

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
Copy link

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

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

@facultymatt
Copy link

@vikasgahlaut
Copy link

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??

@skidvd
Copy link

skidvd commented Apr 10, 2015

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?

@ramdog
Copy link

ramdog commented Sep 10, 2015

Unfortunately, the hack above in #308 (comment) is not working for me. I have another hack that seems to be working:

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

@fyodorvi
Copy link

fyodorvi commented Apr 5, 2016

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

    });

});

@tomyam1
Copy link

tomyam1 commented May 22, 2016

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)

@inthegarage
Copy link

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);
                });
            });

        });

@Sankannavar
Copy link

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

@inthegarage
Copy link

inthegarage commented Aug 18, 2017

@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.

@Sankannavar
Copy link

@inthegarage Thanks for information.

@twaraich
Copy link

twaraich commented Feb 7, 2018

For anyone who's still struggling, I have another solution which worked for me

        browser
        .switchTo()
        .window(windowHandle)
        .then(() => {
          browser
            .refresh()
            .then(() => {
              console.log("success referesh");
            })
            .catch(err => {
              browser
                .switchTo()
                .alert()
                .then(alert => {
                  browser.sleep(5000);
                  alert.accept();
                  browser.sleep(5000);
                });
            });
        });
    });```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests