com.gargoylesoftware.htmlunit.html.HtmlElementUtil Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlElementUtil. 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: JenkinsRule.java    From jenkins-test-harness with MIT License 5 votes vote down vote up
/**
 * Submits the form by clicking the submit button of the given name.
 *
 * @param name
 *      This corresponds to the @name of {@code <f:submit />}
 */
public HtmlPage submit(HtmlForm form, String name) throws Exception {
    for( HtmlElement e : form.getElementsByTagName("button")) {
        HtmlElement p = (HtmlElement)e.getParentNode().getParentNode();                        
        if (p.getAttribute("name").equals(name) && HtmlElementUtil.hasClassName(p, "yui-submit-button")) {
            // For YUI handled submit buttons, just do a click.
            return (HtmlPage) HtmlElementUtil.click(e);
        } else if (e.getAttribute("name").equals(name)) {
            return (HtmlPage) HtmlFormUtil.submit(form, e);
        }
    }
    throw new AssertionError("No such submit button with the name "+name);
}
 
Example #2
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());
}