browser.switchTo().alert() not working in protractor.
sample code:
browser.get('http://google.com');
browser.switchTo().alert().accept();
Message:
UnexpectedAlertOpenError: unexpected alert open
(Session info: chrome=39.0.2171.95)
(Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64)
Stacktrace:
UnexpectedAlertOpenError: unexpected alert open
C:\Users\Vikas.Gahlaut\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113
var template = new Error(this.message);
^
UnexpectedAlertOpenError: unexpected alert open
Tried everything and it is not related to chrome driver issue as same is working fine with webdriver-java native APIs.
Unless you've set ignoreSynchronization
, the fact that you're navigating to a non angular app (google.com) is messing it up.
Try
browser.driver.get('http://google.com');
browser.switchTo().alert().accept();
or
browser.ignoreSynchronization = true
browser.get('http://google.com');
browser.switchTo().alert().accept();
Sometimes the alert takes a time until it appears, you can try adding a wait before you make an accept:
browser.ignoreSynchronization = true
browser.get('http://google.com');
browser.wait(protractor.ExpectedConditions.alertIsPresent(), 10000);
browser.switchTo().alert().accept();
This is what I did, so that it does not fail if no alert is open:
browser.ignoreSynchronization = true;
browser.get('http://google.com');
browser.switchTo().alert().then(function() {
browser.switchTo().alert().accept();
}, function(){});
browser.ignoreSynchronization = false;
browser.get("your.address.com");
browser.switchTo().alert().then(function(alert) {
alert.accept();
}).catch(function(error) {
});