I am running with protractor and cucumber. For a number of tests, the oute is problematic, and will sometimes produce an alert box.
what I'd like to do is in my beginning method for each test, check to see if there is an alert box, and then close/dismiss it. Then continue. The problem I'm facing is, I can't guarantee that there will always be an alert box, and if there isn't one, I get a NoSuchAlertError: no alert open
and the entire script stops.
Is there any way around this?
Current code:
try {
browser.switchTo().alert().dismiss();
}catch(err){
}
I am running with protractor and cucumber. For a number of tests, the oute is problematic, and will sometimes produce an alert box.
what I'd like to do is in my beginning method for each test, check to see if there is an alert box, and then close/dismiss it. Then continue. The problem I'm facing is, I can't guarantee that there will always be an alert box, and if there isn't one, I get a NoSuchAlertError: no alert open
and the entire script stops.
Is there any way around this?
Current code:
try {
browser.switchTo().alert().dismiss();
}catch(err){
}
Share
Improve this question
edited Aug 27, 2014 at 15:09
Sakamoto Kazuma
asked Jul 30, 2014 at 15:48
Sakamoto KazumaSakamoto Kazuma
2,5797 gold badges38 silver badges76 bronze badges
3
- Did you try using a `try/catch' block? Try to dismiss the alert and continue, if no alert ignore and continue. – Mark Rowlands Commented Jul 30, 2014 at 16:07
- I did. Didn't seem like the try/catch block was able to catch it. – Sakamoto Kazuma Commented Jul 30, 2014 at 17:15
-
2
That's odd. I have a very similar situation in one of my suites. Admittedly this is with Python but I simply do the following
def accept_alert_if_present(self): try: self.driver.switch_to_default_content() alert = self.driver.switch_to_alert() alert.accept() except NoSuchAlertError: pass
– Mark Rowlands Commented Jul 31, 2014 at 8:09
4 Answers
Reset to default 5driver.switchTo().alert().then(
function (alert) { alert.dismiss(); },
function (err) { }
);
This worked for me. It'll dismiss the alert if it is there, and do nothing if it isn't.
Change dismiss()
for accept()
if you want to accept the alert (OK) instead of dismissing (Cancel).
//dismiss "wrong credentials" alert
browser.driver.sleep(2000);
browser.switchTo().alert().accept().then(null, function(e) {
if (e.code !== webdriver.ErrorCode.NO_SUCH_ALERT) {
throw e;
}
});
This worked for me in Protractor 2.0. I am sure there is a better solution that doesn't involve using sleep(). After a couple hours of frustration, though, I was just happy to see something that worked.
Exception handling from: https://code.google./p/selenium/wiki/WebDriverJs
As in https://code.google./p/selenium/wiki/WebDriverJs, you could try/catch to handle the alert
try {
driver.switchTo().alert().dismiss();
} catch (NoAlertPresentException ignored) {
}
try to override the javascript method to confirm a dialogue if exist
window.confirm = function() { return true; }
this solution works for me with capybara and cucumber