AlertAction extends AbstractSeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { Alert alert = browser.getWebDriver().switchTo().alert(); if (alert == null) { throw new CitrusRuntimeException("Failed to access alert dialog - not found"); } if (StringUtils.hasText(text)) { log.info("Validating alert text"); String alertText = context.replaceDynamicContentInString(text); if (ValidationMatcherUtils.isValidationMatcherExpression(alertText)) { ValidationMatcherUtils.resolveValidationMatcher("alertText", alert.getText(), alertText, context); } else { Assert.isTrue(alertText.equals(alert.getText()), String.format("Failed to validate alert dialog text, " + "expected '%s', but was '%s'", alertText, alert.getText())); } log.info("Alert text validation successful - All values Ok"); } context.setVariable(SeleniumHeaders.SELENIUM_ALERT_TEXT, alert.getText()); if (accept) { alert.accept(); } else { alert.dismiss(); } } AlertAction(Builder builder); boolean isAccept(); String getText();  }
@Test public void testExecuteAccept() throws Exception { when(locator.alert()).thenReturn(alert); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .build(); action.execute(context); verify(alert).accept(); }
@Test public void testExecuteDismiss() throws Exception { WebDriver.TargetLocator locator = Mockito.mock(WebDriver.TargetLocator.class); when(webDriver.switchTo()).thenReturn(locator); when(locator.alert()).thenReturn(alert); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .dismiss() .build(); action.execute(context); verify(alert).dismiss(); }
@Test public void testExecuteTextValidation() throws Exception { when(locator.alert()).thenReturn(alert); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .text("This is a warning!") .build(); action.execute(context); verify(alert).accept(); }
@Test public void testExecuteTextValidationVariableSupport() throws Exception { when(locator.alert()).thenReturn(alert); context.setVariable("alertText","This is a warning!"); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .text("${alertText}") .build(); action.execute(context); verify(alert).accept(); }
@Test public void testExecuteTextValidationMatcherSupport() throws Exception { when(locator.alert()).thenReturn(alert); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .text("@startsWith('This is')@") .build(); action.execute(context); verify(alert).accept(); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Failed to validate alert dialog text.*") public void testExecuteTextValidationError() throws Exception { when(locator.alert()).thenReturn(alert); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .text("This is not a warning!") .build(); action.execute(context); verify(alert).accept(); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to access alert dialog - not found") public void testAlertNotFound() { WebDriver.TargetLocator locator = Mockito.mock(WebDriver.TargetLocator.class); when(webDriver.switchTo()).thenReturn(locator); when(locator.alert()).thenReturn(null); AlertAction action = new AlertAction.Builder() .browser(seleniumBrowser) .build(); action.execute(context); }