com.gargoylesoftware.htmlunit.AlertHandler Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.AlertHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * The JavaScript function {@code alert()}.
 * @param message the message
 */
@JsxFunction
public void alert(final Object message) {
    // use Object as parameter and perform String conversion by ourself
    // this allows to place breakpoint here and "see" the message object and its properties
    final String stringMessage = Context.toString(message);
    final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
    if (handler == null) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("window.alert(\"" + stringMessage + "\") no alert handler installed");
        }
    }
    else {
        handler.handleAlert(document_.getPage(), stringMessage);
    }
}
 
Example #2
Source File: HiddenHtmlAlert.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
@Override
@PublicAtsApi
public void clickOk() {

    isProcessed = false;
    webClient.setAlertHandler(new AlertHandler() {

        @Override
        public void handleAlert(
                                 Page alertPage,
                                 String alertText ) {

            isProcessed = true;
            //do nothing, by default it clicks the OK button
        }
    });
}
 
Example #3
Source File: HiddenHtmlAlert.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
@Override
@PublicAtsApi
public void clickOk(
                     final String expectedAlertText ) {

    isProcessed = false;
    webClient.setAlertHandler(new AlertHandler() {

        @Override
        public void handleAlert(
                                 Page alertPage,
                                 String alertText ) {

            isProcessed = true;
            if (!alertText.equals(expectedAlertText)) {

                throw new VerificationException("The expected alert message was: '" + expectedAlertText
                                                + "', but actually it is: '" + alertText + "'");
            }
        }
    });
}
 
Example #4
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The JavaScript function {@code alert()}.
 * @param message the message
 */
@JsxFunction
public void alert(final Object message) {
    // use Object as parameter and perform String conversion by ourself
    // this allows to place breakpoint here and "see" the message object and its properties
    final String stringMessage = Context.toString(message);
    final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
    if (handler == null) {
        LOG.warn("window.alert(\"" + stringMessage + "\") no alert handler installed");
    }
    else {
        handler.handleAlert(document_.getPage(), stringMessage);
    }
}
 
Example #5
Source File: TestReportUiTest.java    From junit-plugin with MIT License 5 votes vote down vote up
/**
 * Validate CSS styles present to prevent duration text from wrapping
 */
@Issue("JENKINS-24352")
@Test
public void testDurationStyle() throws Exception {
    AbstractBuild b = configureTestBuild("render-test");

    JenkinsRule.WebClient wc = j.createWebClient();

    wc.setAlertHandler(new AlertHandler() {
        @Override
        public void handleAlert(Page page, String message) {
            throw new AssertionError();
        }
    });

    HtmlPage pg = wc.getPage(b, "testReport");

    // these are from the test result file:
    String duration14sec = Util.getTimeSpanString((long) (14.398 * 1000));
    String duration3_3sec = Util.getTimeSpanString((long) (3.377 * 1000));
    String duration2_5sec = Util.getTimeSpanString((long) (2.502 * 1000));

    Assert.assertNotNull(pg.getFirstByXPath("//td[contains(text(),'" + duration3_3sec + "')][contains(@class,'no-wrap')]"));

    pg = wc.getPage(b, "testReport/org.twia.vendor");

    Assert.assertNotNull(pg.getFirstByXPath("//td[contains(text(),'" + duration3_3sec + "')][contains(@class,'no-wrap')]"));
    Assert.assertNotNull(pg.getFirstByXPath("//td[contains(text(),'" + duration14sec + "')][contains(@class,'no-wrap')]"));

    pg = wc.getPage(b, "testReport/org.twia.vendor/VendorManagerTest");

    Assert.assertNotNull(pg.getFirstByXPath("//td[contains(text(),'" + duration2_5sec + "')][contains(@class,'no-wrap')]"));
}
 
Example #6
Source File: Selection2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void test(final String action, final String x, final String alert)
    throws Exception {

    final String html = "<html>\n"
        + "<body onload='test()'>\n"
        + "  <span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n"
        + "  <input type='button' id='b' onclick=\"" + action + ";test();\" value='click'></input>\n"
        + "<script>\n"
        + "  var selection = document.selection; // IE\n"
        + "  if(!selection) selection = window.getSelection(); // FF\n"
        + "  var s1 = document.getElementById('s1');\n"
        + "  var s2 = document.getElementById('s2');\n"
        + "  function test() {\n"
        + "    try {\n"
        + "      var x = " + x + ";\n"
        + "      alert(" + alert + ");\n"
        + "    } catch (e) {\n"
        + "      alert('unsupported action');\n"
        + "    }\n"
        + "  }\n"
        + "</script>\n"
        + "</body></html>";

    final WebClient client = getWebClient();

    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client.setWebConnection(conn);

    final List<String> actual = new ArrayList<>();
    final AlertHandler alertHandler = new CollectingAlertHandler(actual);
    client.setAlertHandler(alertHandler);

    final HtmlPage page = client.getPage(URL_FIRST);
    final DomNode s1Text = page.getHtmlElementById("s1").getFirstChild();
    final DomNode s2Text = page.getHtmlElementById("s2").getFirstChild();
    final HtmlInput input = page.getHtmlElementById("b");

    final org.w3c.dom.ranges.Range range = new SimpleRange();
    range.setStart(s1Text, 2);
    range.setEnd(s2Text, 1);
    page.setSelectionRange(range);
    input.click();

    assertEquals(getExpectedAlerts(), actual);
}
 
Example #7
Source File: LockableResourceRootActionSEC1361Test.java    From lockable-resources-plugin with MIT License 4 votes vote down vote up
private void checkXssWithResourceName(String resourceName) throws Exception {
  LockableResourcesManager.get().createResource(resourceName);

  j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
  j.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy());

  JenkinsRule.WebClient wc = j.createWebClient();
  wc.login("user");

  final AtomicReference<String> lastAlertReceived = new AtomicReference<>();
  wc.setAlertHandler(
      new AlertHandler() {
        @Override
        public void handleAlert(Page page, String s) {
          lastAlertReceived.set(s);
        }
      });

  HtmlPage htmlPage = wc.goTo("lockable-resources");
  assertThat(lastAlertReceived.get(), nullValue());

  // currently only one button but perhaps in future version of the core/plugin,
  // other buttons will be added to the layout
  List<HtmlElement> allButtons = htmlPage.getDocumentElement().getElementsByTagName("button");
  assertThat(allButtons.size(), greaterThanOrEqualTo(1));

  HtmlElement reserveButton = null;
  for (HtmlElement b : allButtons) {
    String onClick = b.getAttribute("onClick");
    if (onClick != null && onClick.contains("reserve")) {
      reserveButton = b;
    }
  }
  assertThat(reserveButton, not(nullValue()));

  try {
    HtmlElementUtil.click(reserveButton);
  } catch (FailingHttpStatusCodeException e) {
    // only happen if we have a XSS, but it's managed using the AlertHandler to ensure it's a XSS
    // and not just an invalid page
  }
  assertThat(lastAlertReceived.get(), nullValue());
}