问题
I have a protractor setup with multiple browsers configured through multiCapabilities, running tests on browserstack.
One of my key protractor specs/tests contain the following afterEach() block:
afterEach(function() {
browser.manage().logs().get("browser").then(function (browserLog) {
expect(browserLog.length).toEqual(0);
});
});
that checks that the browser console is empty (no errors on the console).
The problem is: when I run this spec against Internet Explorer, I'm getting an UnknownError:
UnknownError: Command not found: POST /session/6b838fe8-f4a6-4b31-b245-f4bf8f37537c/log
After a quick research, I've found out that IE selenium webdriver does not yet support session logs:
- [IE] Add support for fetching logs using the webdriver.manage().logs() mechanism
The question is: how can I catch this UnknownError and let the spec pass in case of this specific error?
Or, to turn it around, is it possible to have an afterEach() block capability/browser-specific, or know which currently running capability is it?
I've tried to use try/catch and try relying on exception sender, but console.log() is not executed:
afterEach(function() {
try {
browser.manage().logs().get("browser").then(function (browserLog) {
expect(browserLog.length).toEqual(0);
});
}
catch (e) {
console.log(e.sender);
}
});
As a workaround, I'm duplicating the same spec but without that failing afterEach() block, specifically for Internet Explorer.
回答1:
Found one option - using getCapabilities() to retrieve the current browser name:
afterEach(function() {
browser.driver.getCapabilities().then(function(caps) {
var browserName = caps.caps_.browserName;
if (browserName !== "internet explorer") {
browser.manage().logs().get("browser").then(function (browserLog) {
expect(browserLog.length).toEqual(0);
});
}
});
});
In this case browser logs would not be checked if running against Internet Explorer.
来源:https://stackoverflow.com/questions/27587571/handling-unknown-errors-in-protractor