UnhandledAlertHandlingWebDriver extends DecoratedWebDriver { public void registerAlertHandler(UnhandledAlertHandler handler) { handlers.add(handler); } UnhandledAlertHandlingWebDriver(final WebDriver driver); void registerAlertHandler(UnhandledAlertHandler handler); @Override Object onErrorGlobal(Decorated<?> target, Method method, InvocationTargetException e, Object[] args);  }
@Test void testAlertIsIgnored() { Fixture fixture = new Fixture(); SimpleUnhandledAlertHandler handler = new SimpleUnhandledAlertHandler(); fixture.alertHandlingDriver.registerAlertHandler(handler); when(fixture.mockedDriver.getTitle()) .thenThrow(new UnhandledAlertException("Unhandled alert", "Alert text")) .thenReturn("Page title"); String title = fixture.driver.getTitle(); assertThat(title, is("Page title")); assertThat(handler.alertText, is("Alert text")); }
@Test void testSecondAlertIsNotIgnored() { Fixture fixture = new Fixture(); SimpleUnhandledAlertHandler handler = new SimpleUnhandledAlertHandler(); fixture.alertHandlingDriver.registerAlertHandler(handler); when(fixture.mockedDriver.getTitle()) .thenThrow(new UnhandledAlertException("Unhandled alert", "Alert text 1")) .thenThrow(new UnhandledAlertException("Unhandled alert", "Alert text 2")); Throwable exception = assertThrows(UnhandledAlertException.class, () -> fixture.driver.getTitle()); assertThat(((UnhandledAlertException) exception).getAlertText(), is("Alert text 2")); assertThat(handler.alertText, is("Alert text 1")); }
@Test void testOtherExceptionsAreNotIgnored() { Fixture fixture = new Fixture(); SimpleUnhandledAlertHandler handler = new SimpleUnhandledAlertHandler(); fixture.alertHandlingDriver.registerAlertHandler(handler); when(fixture.mockedDriver.getTitle()).thenThrow(new WebDriverException()); assertThrows(WebDriverException.class, () -> fixture.driver.getTitle()); }