protected File savePageSourceToFile(String fileName, WebDriver webdriver, boolean retryIfAlert) {
    File pageSource = new File(reportsFolder, fileName + ".html");

    try {
      writeToFile(webdriver.getPageSource(), pageSource);
    } catch (UnhandledAlertException e) {
      if (retryIfAlert) {
        try {
          Alert alert = webdriver.switchTo().alert();
          log.severe(e + ": " + alert.getText());
          alert.accept();
          savePageSourceToFile(fileName, webdriver, false);
        } catch (Exception unableToCloseAlert) {
          log.severe("Failed to close alert: " + unableToCloseAlert);
        }
      } else {
        printOnce("savePageSourceToFile", e);
      }
    } catch (UnreachableBrowserException e) {
      writeToFile(e.toString(), pageSource);
      return pageSource;
    } catch (Exception e) {
      writeToFile(e.toString(), pageSource);
      printOnce("savePageSourceToFile", e);
    }
    return pageSource;
  }
  @Test
  public void shouldGetMeaningfulExceptionOnBrowserDeath() {
    ConnectionCapturingDriver driver2 = new ConnectionCapturingDriver();
    driver2.get(pages.formPage);

    try {
      driver2.keptConnection.quit();
      driver2.get(pages.formPage);
      fail("Should have thrown.");
    } catch (UnreachableBrowserException e) {
      assertThat(
          "Must contain descriptive error",
          e.getMessage(),
          containsString("Error communicating with the remote browser"));
    }
  }