Reputation: 113
I'm working on the automation of a website and i ran into a particular problem. I'm using protractor over gulp to run the automated tests and also a report generator included in the gulp task.
The issue at hand is the following: Whenever an alert is triggered by chrome, protractor stops and throws the "UnexpectedAlertOpenError" in the console, stopping the test run and canceling the report generation.
I would like to know if there is a way to make the spec fail and continue running the rest of the suite.
I know you can do this:
browser.get(url).catch(function () {
return browser.switchTo().alert().then(function (alert) {
alert.accept();
return browser.get(url);
});
});
But i don't want protractor to close the alert and continue, I would like to fail the test where it came up, and return an error message to continue with the run.
Is there any way to do that? Is it possible to pass an exception to the catch function and return a message? I could not find any documentation about that catch method.
Thank you!
EDIT: After going over the stack trace on the console, I've found that protractor detects as if the spec failed, and the exception comes from the reporter when it tries to take a screenshot (I'm using protractor-jasmine2-html-reporter) I'm going to paste a bit of the stack trace in case someone can figure out something, I'm lost really.
E/launcher - UnexpectedAlertOpenError: unexpected alert open: {Alert text : You have pending changes}
From: Task: WebDriver.takeScreenshot()
EDIT2: I found the real problem with my implementation. The npm plugin protractor-jasmine2-html-reporter (which i'm using) was trying to take a screenshot when the alert was open, causing the webdriver to break and the kept the report from being generated.
What i did to fix this was to fork from their repository and before trying to take a screenshot confirm if the alert was open and avoid taking the screenshot if it was:
function alertIsPresent() {
return browser.driver.switchTo().alert()
.then(function (alert) {
alert.accept();
return true;
}, function (err) {
return false;
});
};
In case it was open, i would close it and continue without taking the screenshot, otherwise take the screenshot. By doing this the report generates correctly and it documents that on the next spec report that there was an alert open.
Hope this is helpful to someone.
Upvotes: 6
Views: 9306
Reputation: 892
I've had a similar problem. Searched for hours and finally found this:
unexpectedAlertBehaviour: 'accept'
See https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#read-write-capabilities for more information. You basically create a capabilities
object and pass the desired values into it:
capabilities: {
browserName: "chrome",
unexpectedAlertBehaviour: 'accept',
chromeOptions: {
args: ["--window-size=1920,1080", "--disable-gpu"],
},
},
Hope this helps!
Upvotes: 13
Reputation: 5016
This is odd that your test Protractor test completely fails and Protractor stops working. If this is the case, please feel free to open up a Protractor issue.
My guess is that you have an it
spec that fails, and it puts up an alert causing other tests to not work. There are a couple of things you could do:
Restart the browser after each test (see the config). This quits the driver session and creates a new browser instance. As you can imagine, this will slow down your test. In your config set:
restartBrowserBetweenTests: true
Try using the postTest
plugin. Use postTest
to check if the test failed, and maybe check if there is an alert and close it.
postTest: function(passed, testInfo) {
if (!passed) {
// should check to see if there is an alert
// close the alert
return browser.switchTo().alert().then(function (alert) {
return alert.accept();
});
}
}
Upvotes: 2
Reputation: 1197
You should be able to check for these Error codes and force a failure via the jasmine fail function
browser.switchTo().alert().then(function(alert) {
alert.accept();
}, function(err) {
if (err.code == webdriver.error.ErrorCode.UNEXPECTED_ALERT_OPEN) {
fail('Fail this spec');
}
});
Upvotes: 2