Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle UnexpectedAlertOpenError in Protractor

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.

like image 411
Alfie Barboza Avatar asked Jan 04 '17 16:01

Alfie Barboza


1 Answers

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!

like image 155
jdstaerk Avatar answered Oct 01 '22 20:10

jdstaerk