title: How to handle UnexpectedAlertOpenError in Protractor
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I would like to know if there is a way to make the spec fail and continue running the rest of the suite.</p>
  
  <p>I know you can do this:</p>
  
  <pre><code>browser.get(url).catch(function () {
  
      return browser.switchTo().alert().then(function (alert) {
  
        alert.accept();
        return browser.get(url);
  
      });
    });
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thank you!</p>
  
  <p>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.</p>
  
  <pre><code> E/launcher - UnexpectedAlertOpenError: unexpected alert open: {Alert text : You have pending changes}
  
  From: Task: WebDriver.takeScreenshot()
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>  function alertIsPresent() {
      return browser.driver.switchTo().alert()
       .then(function (alert) {
         alert.accept();
         return true;
         }, function (err) {
        return false;
       });
    };
  </code></pre>
  
  <p>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.</p>
  
  <p>Hope this is helpful to someone.</p>