Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlInput#click()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlInput#click() . 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: HTMLInputElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void click() throws IOException {
    final HtmlInput domNode = getDomNodeOrDie();
    final boolean originalState = domNode.isChecked();
    final Event event;
    if (getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(domNode, MouseEvent.TYPE_CLICK, false, false, false, MouseEvent.BUTTON_LEFT);
    }
    else {
        event = new MouseEvent(domNode, MouseEvent.TYPE_CLICK, false, false, false, MouseEvent.BUTTON_LEFT);
    }
    domNode.click(event, event.isShiftKey(), event.isCtrlKey(), event.isAltKey(), true);

    final boolean newState = domNode.isChecked();

    if (originalState != newState
            && (domNode instanceof HtmlRadioButtonInput || domNode instanceof HtmlCheckBoxInput)) {
        domNode.fireEvent(Event.TYPE_CHANGE);
    }
}
 
Example 2
Source File: HTMLInputElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void click() throws IOException {
    final HtmlInput domNode = getDomNodeOrDie();
    final boolean originalState = domNode.isChecked();
    final Event event;
    if (getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(domNode, MouseEvent.TYPE_CLICK, false, false, false, MouseEvent.BUTTON_LEFT);
    }
    else {
        event = new MouseEvent(domNode, MouseEvent.TYPE_CLICK, false, false, false, MouseEvent.BUTTON_LEFT);
    }
    domNode.click(event, true);

    final boolean newState = domNode.isChecked();

    if (originalState != newState
            && (domNode instanceof HtmlRadioButtonInput || domNode instanceof HtmlCheckBoxInput)) {
        domNode.fireEvent(Event.TYPE_CHANGE);
    }
}
 
Example 3
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 4
Source File: JmxWebHandlerTest.java    From simplejmx with ISC License 4 votes vote down vote up
@Test(timeout = 10000)
public void testSimple() throws Exception {
	WebClient webClient = new WebClient();
	HtmlPage page = webClient.getPage("http://" + WEB_SERVER_NAME + ":" + WEB_SERVER_PORT);
	assertTrue(page.asText().contains("JMX Domains"));

	String domain = "java.lang";
	HtmlAnchor anchor = page.getAnchorByText(domain);
	assertNotNull(anchor);
	page = anchor.click();
	assertTrue(page.asText().contains("Beans in domain " + domain));

	anchor = page.getAnchorByName("text");
	TextPage textPage = anchor.click();
	String bean = "type=Memory";
	assertTrue(textPage.getContent().contains(domain + ":" + bean));

	anchor = page.getAnchorByText(bean);
	page = anchor.click();
	assertTrue(page.asText().contains("Information about object " + domain + ":" + bean));

	anchor = page.getAnchorByName("text");
	textPage = anchor.click();
	assertTrue(textPage.getContent().contains("Verbose"));

	HtmlForm form = page.getFormByName("Verbose");
	assertNotNull(form);
	HtmlInput input = form.getInputByName("val");
	assertEquals("false", input.getValueAttribute());
	assertNotNull(input);
	input.setValueAttribute("true");
	HtmlElement button = (HtmlElement) page.createElement("button");
	button.setAttribute("type", "submit");
	// append the button to the form to simulate
	form.appendChild(button);
	// submit the form
	page = button.click();
	assertTrue(page.asText().contains("Information about object " + domain + ":" + bean));

	form = page.getFormByName("Verbose");
	assertNotNull(form);
	input = form.getInputByName("val");
	assertEquals("true", input.getValueAttribute());

	String operation = "gc";
	form = page.getFormByName(operation);
	assertNotNull(form);
	input = form.getInputByValue(operation);
	page = input.click();

	assertTrue(page.asText().contains("Invoking Operation " + operation));
	assertTrue(page.asText().contains(operation + " method successfully invoked."));

	anchor = page.getAnchorByName("text");
	assertNotNull(anchor);
	textPage = anchor.click();
	assertEquals(operation + " method successfully invoked.\n", textPage.getContent());
}