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

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlElement#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: WebClientTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test reuse of a single {@link HtmlPage} object to submit the same form multiple times.
 * @throws Exception if test fails
 */
@Test
public void reusingHtmlPageToSubmitFormMultipleTimes() throws Exception {
    final String firstContent = "<html><head><title>First</title></head>\n"
            + "<body onload='document.myform.mysubmit.focus()'>\n"
            + "<form action='" + URL_SECOND + "' name='myform'>\n"
            + "<input type='submit' name='mysubmit'>\n"
            + "</form></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body>Second</body></html>";

    final WebClient webClient = getWebClient();

    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setDefaultResponse(secondContent);

    webClient.setWebConnection(webConnection);

    final HtmlPage page = webClient.getPage(URL_FIRST);
    for (int i = 0; i < 100; i++) {
        final HtmlElement button = page.getFormByName("myform").getInputByName("mysubmit");
        button.click();
    }
}
 
Example 2
Source File: PopupTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the opened window becomes the current one.
 * @throws Exception if the test fails
 */
@Test
@Alerts("Pop-up window is Open")
public void popupWindowBecomesCurrent() throws Exception {
    final String content = "<html><head><title>First</title><body>\n"
        + "<span id='button' onClick='openPopup()'>Push me</span>\n"
        + "<script>\n"
        + "  function openPopup() {\n "
        + "    window.open('', '_blank', 'width=640, height=600, scrollbars=yes');\n"
        + "    alert('Pop-up window is Open');\n "
        + "  }\n"
        + "</script>\n"
        + "</body></html>";

    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadPage(content, collectedAlerts);
    final HtmlElement button = page.getHtmlElementById("button");

    final HtmlPage secondPage = button.click();
    final String[] expectedAlerts = {"Pop-up window is Open"};
    assertEquals(expectedAlerts, collectedAlerts);
    assertEquals("about:blank", secondPage.getUrl());
    assertSame(secondPage.getEnclosingWindow(), secondPage.getWebClient().getCurrentWindow());
}
 
Example 3
Source File: DockerServerCredentialsTest.java    From docker-commons-plugin with MIT License 6 votes vote down vote up
@Test
public void configRoundTripUpdateCertificates() throws Exception {
    CredentialsStore store = CredentialsProvider.lookupStores(j.getInstance()).iterator().next();
    assertThat(store, instanceOf(SystemCredentialsProvider.StoreImpl.class));
    Domain domain = new Domain("docker", "A domain for docker credentials", Collections.singletonList(new DockerServerDomainSpecification()));
    DockerServerCredentials credentials = new DockerServerCredentials(CredentialsScope.GLOBAL, "foo", "desc", Secret.fromString("key"), "client-cert", "ca-cert");
    store.addDomain(domain, credentials);

    HtmlForm form = getUpdateForm(domain, credentials);
    for (HtmlElement button : form.getElementsByAttribute("input", "class", "secret-update-btn")) {
        button.click();
    }

    form.getTextAreaByName("_.clientKeySecret").setText("new key");
    form.getTextAreaByName("_.clientCertificate").setText("new cert");
    form.getTextAreaByName("_.serverCaCertificate").setText("new ca cert");
    j.submit(form);

    DockerServerCredentials expected = new DockerServerCredentials(
            credentials.getScope(), credentials.getId(), credentials.getDescription(),
            Secret.fromString("new key"), "new cert", "new ca cert");
    j.assertEqualDataBoundBeans(expected, findFirstWithId(credentials.getId()));
}
 
Example 4
Source File: GWT250Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void mail() throws Exception {
    final HtmlPage page = loadGWTPage("Mail", null, "//div[@class='MGJ']");
    assertSame(page.getEnclosingWindow(), page.getWebClient().getCurrentWindow());
    final HtmlDivision cell = page.getFirstByXPath("//div[@class='MGJ']");
    assertElementValue(cell, "Welcome back, [email protected]");

    final String[] selectedRow = {"markboland05", "[email protected]", "URGENT -[Mon, 24 Apr 2006 02:17:27 +0000]"};

    final List<?> selectedRowCells = page.getByXPath("//tr[@class='MKI']/td");
    assertEquals(selectedRow.length, selectedRowCells.size());
    for (int i = 0; i < selectedRow.length; i++) {
        final HtmlTableDataCell selectedRowCell = (HtmlTableDataCell) selectedRowCells.get(i);
        assertElementValue(selectedRowCell, selectedRow[i]);
    }

    verifyStartMailBody(page, "Dear Friend,",
            "I am Mr. Mark Boland the Bank Manager of ABN AMRO BANK 101 Moorgate, London, EC2M 6SB.");

    // click on email from Hollie Voss
    final HtmlElement elt = page.getFirstByXPath("//td[text() = 'Hollie Voss']");
    final HtmlPage page2 = elt.click();
    assertSame(page, page2);
    verifyStartMailBody(page, ">> Componentes e decodificadores; confira aqui;",
            "http://br.geocities.com/listajohn/index.htm",
            "THE GOVERNING AWARD");
}
 
Example 5
Source File: JavaScriptEngineTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * If a reference has been hold on a page and the page is not
 * anymore the one contained in "its" window, JavaScript should not be executed.
 * (see {@link com.gargoylesoftware.htmlunit.javascript.host.WindowConcurrencyTest#cleanSetTimeout}).
 * @throws Exception if the test fails
 */
@Test
public void scopeInInactivePage() throws Exception {
    final String firstContent
        = "<html><head>\n"
        + "<script>\n"
        + "var foo = 'foo';\n"
        + "</script>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <a href='page2.html'>to page 2</a>\n"
        + "  <div id='testdiv' onclick='alert(foo)'>foo</div>\n"
        + "</body>\n"
        + "</html>";

    final WebClient client = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse("<html></html>");
    webConnection.setResponse(URL_FIRST, firstContent);
    client.setWebConnection(webConnection);

    final String[] expectedAlerts = {"foo"};

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement div = page.getHtmlElementById("testdiv");

    div.click();
    assertEquals(expectedAlerts, collectedAlerts);
    collectedAlerts.clear();

    page.getAnchors().get(0).click();

    // ignore response, and click in the page again that is not "active" anymore
    div.click();
    assertEquals(Collections.emptyList(), collectedAlerts);
}
 
Example 6
Source File: AttachmentTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * This was causing a ClassCastException in Location.setHref as of 2013-10-08 because the TextPage
 * used for the attachment was wrongly associated to the HTMLDocument of the first page.
 * @throws Exception if an error occurs
 */
@Test
public void jsChangeLocationAfterReceptionOfAttachment() throws Exception {
    final String html = "<html><body>\n"
        + "<form action='action'>\n"
        + "<input type='submit' onclick='window.location=\"foo\"; return false'>\n"
        + "</form>\n"
        + "<a href='" + URL_SECOND + "'>download</a>\n"
        + "</body></html>";

    final WebClient client = getWebClient();
    final List<Attachment> attachments = new ArrayList<>();
    client.setAttachmentHandler(new CollectingAttachmentHandler(attachments));

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Content-Disposition", "attachment"));

    final MockWebConnection conn = getMockWebConnection();
    conn.setDefaultResponse("");
    conn.setResponse(URL_SECOND, "some text", 200, "OK", MimeType.TEXT_PLAIN, headers);

    final HtmlPage page = loadPage(html);
    // download text attachment
    page.getAnchors().get(0).click();
    assertEquals(1, attachments.size());

    final HtmlElement htmlElement = (HtmlElement) page.getFirstByXPath("//input");
    htmlElement.click(); // exception was occurring here
}
 
Example 7
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Basic test for the <tt>showModalDialog</tt> method. See bug #703.
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = {"undefined", "Jane", "Smith", "sdg", "finished"},
        CHROME = "not available",
        FF = "not available",
        FF68 = "not available",
        FF60 = "not available")
public void showModalDialog() throws Exception {
    final String html1
        = "<html><head><script>\n"
        + "  function test() {\n"
        + "    if (!window.showModalDialog) {alert('not available'); return; }\n"
        + "    alert(window.returnValue);\n"
        + "    var o = new Object();\n"
        + "    o.firstName = 'Jane';\n"
        + "    o.lastName = 'Smith';\n"
        + "    var ret = showModalDialog('myDialog.html', o, 'dialogHeight:300px; dialogLeft:200px;');\n"
        + "    alert(ret);\n"
        + "    alert('finished');\n"
        + "  }\n"
        + "</script></head><body>\n"
        + "  <button onclick='test()' id='b'>Test</button>\n"
        + "</body></html>";

    final String html2
        = "<html><head><script>\n"
        + "  var o = window.dialogArguments;\n"
        + "  alert(o.firstName);\n"
        + "  alert(o.lastName);\n"
        + "  window.returnValue = 'sdg';\n"
        + "</script></head>\n"
        + "<body>foo</body></html>";

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

    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
    client.setWebConnection(conn);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement button = page.getHtmlElementById("b");
    final HtmlPage dialogPage = button.click();

    if (getExpectedAlerts().length > 1) {
        final DialogWindow dialog = (DialogWindow) dialogPage.getEnclosingWindow();
        dialog.close();
    }

    assertEquals(getExpectedAlerts(), actual);
}
 
Example 8
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Test for the <tt>showModalDialog</tt> method.
 * This tests blocking until the window gets closed.
 * Can not currently be tested with WebDriver
 * https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/284
 *
 * To fix this, we need to allow user to interact with the opened dialog before showModalDialog() returns
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = {"undefined", "result", "finished"},
        CHROME = {"undefined", "not available"},
        FF = {"undefined", "not available"},
        FF68 = {"undefined", "not available"},
        FF60 = {"undefined", "not available"})
@NotYetImplemented(IE)
public void showModalDialogWithButton() throws Exception {
    final String html1
        = "<html><head>\n"
        + "  <script>\n"
        + "    function test() {\n"
        + "      alert(window.returnValue);\n"
        + "      if (!window.showModalDialog) {alert('not available'); return; }\n"
        + "      var res = showModalDialog('myDialog.html', null, 'dialogHeight:300px; dialogLeft:200px;');\n"
        + "      alert(res);\n"
        + "      alert('finished');\n"
        + "    }\n"
        + "  </script>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <button onclick='test()' id='openDlg'>Test</button>\n"
        + "</body></html>";

    final String html2
        = "<html><head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <button id='closeDlg' onclick='window.returnValue = \"result\"; window.close();'></button>\n"
        + "</body>\n"
        + "</html>";

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

    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
    client.setWebConnection(conn);

    final HtmlPage page = getWebClient().getPage(URL_FIRST);
    final HtmlElement button = page.getHtmlElementById("openDlg");
    button.click();

    // TODO: <button id='closeDlg'> should be clicked
    assertEquals(getExpectedAlerts(), actual);
}
 
Example 9
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Basic test for the <tt>showModelessDialog</tt> method. See bug #703.
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = "",
        IE = {"[object Window]", "a"})
public void showModelessDialog() throws Exception {
    final String html1
        = "<html><head><script>\n"
        + "  var userName = '';\n"
        + "  function test() {\n"
        + "    if (window.showModelessDialog) {\n"
        + "      var newWindow = showModelessDialog('myDialog.html', window, 'status:false');\n"
        + "      alert(newWindow);\n"
        + "    }\n"
        + "  }\n"
        + "  function update() { alert(userName); }\n"
        + "</script></head><body>\n"
        + "  <input type='button' id='b' value='Test' onclick='test()'>\n"
        + "</body></html>";

    final String html2
        = "<html><head><script>\n"
        + "function update() {\n"
        + "  var w = dialogArguments;\n"
        + "  w.userName = document.getElementById('name').value;\n"
        + "  w.update();\n"
        + "}\n"
        + "</script></head><body>\n"
        + "  Name: <input id='name'><input value='OK' id='b' type='button' onclick='update()'>\n"
        + "</body></html>";

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

    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
    client.setWebConnection(conn);

    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlElement button = page.getHtmlElementById("b");
    final HtmlPage dialogPage = button.click();

    if (!dialogPage.getUrl().equals(URL_FIRST)) {
        final HtmlInput input = dialogPage.getHtmlElementById("name");
        input.setValueAttribute("a");

        final HtmlButtonInput button2 = (HtmlButtonInput) dialogPage.getHtmlElementById("b");
        button2.click();

        assertEquals(getExpectedAlerts(), actual);
    }
}
 
Example 10
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());
}