Example usage for org.openqa.selenium NoAlertPresentException NoAlertPresentException

List of usage examples for org.openqa.selenium NoAlertPresentException NoAlertPresentException

Introduction

In this page you can find the example usage for org.openqa.selenium NoAlertPresentException NoAlertPresentException.

Prototype

public NoAlertPresentException(Throwable cause) 

Source Link

Usage

From source file:com.partnet.automation.HtmlView.java

License:Apache License

/**
 * Clicks, then handles the resulting alert.
 * <p>//from w  w w  .  j a  v a  2s  . c o m
 * This method uses the {@link HtmlView#clickElem(WebElement)} method.
 *
 * @param elm - element to be clicked
 * @param accept - true to accept alert, false to dismiss.
 * @param throwIfNoAlertPresent - true to throw exception if there is no alert present, false otherwise
 * @return the string of the alert message
 */
private String clickAndHandleAlert(WebElement elm, boolean accept, boolean throwIfNoAlertPresent) {
    String alertMsg = null;
    LOG.debug("{} alert created by clicking button {}", accept ? "accept" : "dismiss", elm);

    Browser browser = getBrowser();

    // headless browsers need to inject javascript before the button is clicked
    // to handle the alert correctly
    if (browser.isHeadless()) {

        // webDriver.manage().deleteCookieNamed(ALERT_COOKIE_NAME);
        StringBuilder alertJs = new StringBuilder();

        alertJs.append("window.alert = window.confirm = function(msg){ ")
                // .append( "var date = new Date();")
                // .append( "date.setDate(date.getDate() + 1);")

                // cookies don't like to store new lines. This becomes a problem when
                // taking a screenshot for HTMLUNIT, and
                // transferring the cookie to PhantomJs.
                // This prevents newlines from being injected into the cookie. Later
                // on, the return string containing these
                // newline keywords will be replaced with actual newlines.
                .append("msg = msg.replace(/(\\r\\n|\\n|\\r)/gm, '" + ALERT_NEW_LINE_REPLACE + "');")
                .append("document.cookie = '" + ALERT_COOKIE_NAME + "=' + msg + '';").append("return %s;")
                .append("};");
        executeScript(String.format(alertJs.toString(), accept));
    }

    clickElem(elm);

    if (browser.isHeadless()) {
        Cookie alertCookie = webDriver.manage().getCookieNamed(ALERT_COOKIE_NAME);

        for (Cookie cook : webDriver.manage().getCookies()) {
            System.err.print(cook.getName());
        }

        if (alertCookie != null) {
            // replace all newline keywords, to get original message
            alertMsg = StringUtils.trimToNull(alertCookie.getValue());

            if (alertMsg != null)
                alertMsg = alertMsg.replaceAll(ALERT_NEW_LINE_REPLACE, "\n");

            LOG.debug("Headless browser msg: {}", alertMsg);
        } else {
            LOG.debug("Cookie where headless alert messages are stored is null!");
        }

        if (StringUtils.isBlank(alertMsg)) {
            if (throwIfNoAlertPresent) {
                throw new NoAlertPresentException(
                        String.format("No alert message found for headless browser %s!", browser));
            }
        }
    } else {

        Alert alert;

        // IE needs to wait for the alert to appear because we are using native
        // events
        try {
            if (browser.isInternetExplorer()) {
                alert = waitForAlertToBePresent();
            } else {
                alert = webDriver.switchTo().alert();
            }

            alertMsg = alert.getText();

            if (accept) {
                alert.accept();
            } else {
                alert.dismiss();
            }
        } catch (NoAlertPresentException | TimeoutException e) {
            if (throwIfNoAlertPresent) {
                throw e;
            } else {
                LOG.debug("No alert is present! return...");
            }
            return null;
        }
    }

    LOG.debug("{} alert message: {}", accept ? "Accepted" : "Dismissed", alertMsg);
    return alertMsg;
}

From source file:com.redhat.darcy.webdriver.WebDriverAlertTest.java

License:Open Source License

@Test
public void shouldReturnFalseForIsPresentIfDriverCannotSwitchToAlert() {
    WebDriver mockedDriver = mock(WebDriver.class);
    TargetLocator mockedTargetLocator = mock(TargetLocator.class);

    when(mockedDriver.switchTo()).thenReturn(mockedTargetLocator);
    when(mockedTargetLocator.alert()).thenThrow(new NoAlertPresentException("No alert present."));

    WebDriverAlert alert = new WebDriverAlert(mockedDriver);

    assertFalse(alert.isPresent());//from   w w w.ja  v  a  2 s .c  om
}