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

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlButton#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
/**
 * Previous window should become current window after current window is closed while loading the page.
 * @throws Exception if an error occurs
 */
@Test
public void windowTracking_SpecialCase2() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection conn = new MockWebConnection();

    final String html1 = "<html><head><title>First</title></head>\n"
            + "<body><form name='form1'>\n"
            + "<button id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</button>\n"
            + "</form></body></html>";
    conn.setResponse(URL_FIRST, html1);

    final String html2 = "<html><head><title>Third</title>\n"
            + "<script type=\"text/javascript\">\n"
            + "     window.close();\n"
            + "</script></head></html>";
    conn.setDefaultResponse(html2);

    webClient.setWebConnection(conn);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    buttonA.click();
    assertNotNull(webClient.getCurrentWindow().getEnclosedPage());
    assertEquals("First", ((HtmlPage) webClient.getCurrentWindow().getEnclosedPage()).getTitleText());
}
 
Example 2
Source File: Event3Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void testEventOnClick_Shift_Ctrl_Alt(final boolean shiftKey,
        final boolean ctrlKey, final boolean altKey, final String[] expectedAlerts) throws Exception {
    final String htmlContent
        = "<html><head><title>foo</title></head><body>\n"
        + "<form id='form1'>\n"
        + "  <button name='button' type='button' id='button'>Push me</button>\n"
        + "</form>\n"
        + "<script>\n"
        + "function handler(_e) {\n"
        + "  var e = _e ? _e : window.event;\n"
        + "  alert(e.shiftKey + ',' + e.ctrlKey + ',' + e.altKey);\n"
        + "}\n"
        + "document.getElementById('button').onclick = handler;\n"
        + "</script>\n"
        + "</body></html>";
    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadPage(htmlContent, collectedAlerts);
    final HtmlButton button = page.getHtmlElementById("button");

    final HtmlPage secondPage = button.click(shiftKey, ctrlKey, altKey);

    assertEquals(expectedAlerts, collectedAlerts);

    assertSame(page, secondPage);
}
 
Example 3
Source File: HTMLFormElement2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void formSubmit_MultipleButtons() throws Exception {
    final String html
        = "<html><head><title>first</title></head><body>\n"
        + "<p>hello world</p>\n"
        + "<form name='form1' method='get' action='" + URL_SECOND + "'>\n"
        + "  <button type='submit' name='button1' id='button1'/>\n"
        + "  <button type='submit' name='button2' />\n"
        + "</form>\n"
        + "</body></html>";
    final String secondContent
        = "<html><head><title>second</title></head><body>\n"
        + "<p>hello world</p>\n"
        + "</body></html>";

    getMockWebConnection().setDefaultResponse(secondContent);

    final HtmlPage page = loadPageWithAlerts(html);
    assertEquals("first", page.getTitleText());

    final HtmlButton button = page.getHtmlElementById("button1");
    final HtmlPage secondPage = button.click();
    assertEquals("second", secondPage.getTitleText());
    assertEquals(URL_SECOND + "?button1=", secondPage.getUrl());
}
 
Example 4
Source File: KubernetesCloudTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultWorkspaceVolume() throws Exception {
    KubernetesCloud cloud = new KubernetesCloud("kubernetes");
    j.jenkins.clouds.add(cloud);
    j.jenkins.save();
    JenkinsRule.WebClient wc = j.createWebClient();
    HtmlPage p = wc.goTo("configureClouds/");
    HtmlForm f = p.getFormByName("config");
    HtmlButton buttonExtends = HtmlFormUtil.getButtonByCaption(f, "Pod Templates...");
    buttonExtends.click();
    HtmlButton buttonAdd = HtmlFormUtil.getButtonByCaption(f, "Add Pod Template");
    buttonAdd.click();
    HtmlButton buttonDetails = HtmlFormUtil.getButtonByCaption(f, "Pod Template details...");
    buttonDetails.click();
    DomElement templates = p.getElementByName("templates");
    HtmlInput templateName = getInputByName(templates, "_.name");
    templateName.setValueAttribute("default-workspace-volume");
    j.submit(f);
    cloud = j.jenkins.clouds.get(KubernetesCloud.class);
    PodTemplate podTemplate = cloud.getTemplates().get(0);
    assertEquals("default-workspace-volume", podTemplate.getName());
    assertEquals(WorkspaceVolume.getDefault(), podTemplate.getWorkspaceVolume());
}
 
Example 5
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 6 votes vote down vote up
private static HtmlPage registerClient(HtmlPage registerPage,
                                        String clientName, String redirectURI,
                                        String clientAudience,
                                        String logoutURI,
                                        boolean confidential) throws IOException {
    final HtmlForm form = registerPage.getForms().get(0);

    // Set new client values
    final HtmlTextInput clientNameInput = form.getInputByName("client_name");
    clientNameInput.setValueAttribute(clientName);
    final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
    clientTypeSelect.setSelectedAttribute(confidential ? "confidential" : "public", true);
    final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
    redirectURIInput.setValueAttribute(redirectURI);
    final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
    clientAudienceURIInput.setValueAttribute(clientAudience);
    final HtmlTextInput clientLogoutURI = form.getInputByName("client_logoutURI");
    clientLogoutURI.setValueAttribute(logoutURI);

    final HtmlButton button = form.getButtonByName("submit_button");
    return button.click();
}
 
Example 6
Source File: MockMvcWebClientCreateTaskTests.java    From spring4-sandbox with Apache License 2.0 6 votes vote down vote up
@Test
	public void testCreateTasks() throws Exception {

		HtmlPage createTaskPage = webClient.getPage("http://localhost:8080/tasks/new");

		HtmlForm form = createTaskPage.getHtmlElementById("form");
		HtmlTextInput nameInput = createTaskPage.getHtmlElementById("name");
		nameInput.setValueAttribute("My first task");
		HtmlTextArea descriptionInput = createTaskPage.getHtmlElementById("description");
		descriptionInput.setText("Description of my first task");
		HtmlButton submit = form.getOneHtmlElementByAttribute("button", "type", "submit");
		HtmlPage taskListPage = submit.click();

		Assertions.assertThat(taskListPage.getUrl().toString()).endsWith("/tasks");
//		String id = taskListPage.getHtmlElementById("todolist").getTextContent();
//		assertThat(id).isEqualTo("123");
//		String summary = newMessagePage.getHtmlElementById("summary").getTextContent();
//		assertThat(summary).isEqualTo("Spring Rocks");
//		String text = newMessagePage.getHtmlElementById("text").getTextContent();
//		assertThat(text).isEqualTo("In case you didn't know, Spring Rocks!");

	}
 
Example 7
Source File: WebClientTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Previous window should become current window after current window is closed in onLoad event.
 * @throws Exception if an error occurs
 */
@Test
public void windowTracking_SpecialCase1() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection conn = new MockWebConnection();

    final String html1 = "<html><head><title>First</title></head>\n"
            + "<body><form name='form1'>\n"
            + "<button id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</button>\n"
            + "</form></body></html>";
    conn.setResponse(URL_FIRST, html1);

    final String html2 = "<html><head><title>Second</title></head>\n"
            + "<body onload='doTest()'>\n"
            + "<script>\n"
            + "  function doTest() {\n"
            + "    window.close();\n"
            + "  }\n"
            + "</script></body></html>";
    conn.setDefaultResponse(html2);

    webClient.setWebConnection(conn);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    buttonA.click();
    assertNotNull(webClient.getCurrentWindow().getEnclosedPage());
    assertEquals("First", ((HtmlPage) webClient.getCurrentWindow().getEnclosedPage()).getTitleText());
}
 
Example 8
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 hello() throws Exception {
    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadGWTPage("Hello", collectedAlerts, "//button");
    final HtmlButton button = page.getFirstByXPath("//button");
    final DomText buttonLabel = (DomText) button.getChildren().iterator().next();
    assertEquals("Click me", buttonLabel.getData());
    button.click();
    final String[] expectedAlerts = {"Hello, AJAX"};
    assertEquals(expectedAlerts, collectedAlerts);
}
 
Example 9
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 json() throws Exception {
    final HtmlPage page = loadGWTPage("JSON", null, "//button");
    final HtmlButton button = page.getFirstByXPath("//button");
    button.click();

    page.getWebClient().waitForBackgroundJavaScriptStartingBefore(2000);

    final HtmlSpan span =
        page.getFirstByXPath("//div[@class='JSON-JSONResponseObject']/div/div/table//td[2]/div/span");
    assertEquals("ResultSet", span.getFirstChild().getNodeValue());
}
 
Example 10
Source File: WindowTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void setOpenerLocationHrefRelative() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    final String aContent
        = "<html><head><title>A</title></head><body>\n"
        + "<button id='clickme' onClick='window.open(\"b/b.html\");'>Click me</a>\n"
        + "</body></html>";
    final String bContent
        = "<html><head><title>B</title></head><body>\n"
        + "<button id='clickme' onClick='opener.location.href=\"../c.html\";'>Click me</a>\n"
        + "</body></html>";
    final String cContent
        = "<html><head><title>C</title></head><body></body></html>";
    final String failContent
        = "<html><head><title>FAILURE!!!</title></head><body></body></html>";

    webConnection.setResponse(new URL("http://opener/test/a.html"), aContent);
    webConnection.setResponse(new URL("http://opener/test/b/b.html"), bContent);
    webConnection.setResponse(new URL("http://opener/test/c.html"), cContent);
    webConnection.setResponse(new URL("http://opener/c.html"), failContent);

    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage("http://opener/test/a.html");
    assertEquals("A", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage pageB = buttonA.click();
    assertNotNull("B", pageB);
    assertEquals("B", pageB.getTitleText());

    final HtmlButton buttonB = pageB.getHtmlElementById("clickme");
    final HtmlPage thirdPage = buttonB.click();
    assertSame("Page B has lost focus", pageB, thirdPage);
    assertEquals("C", ((HtmlPage) firstPage.getEnclosingWindow().getEnclosedPage()).getTitleText());
}
 
Example 11
Source File: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onfocus text1", "onfocus text2", "onfocus text1", "onfocus text2"})
public void onFocusOnWindowFocusGain() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onfocus='alert(\"onfocus text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onfocus='alert(\"onfocus text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 12
Source File: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onblur text2", "onblur text1"})
public void onBlurOnWindowFocusChange() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onblur='alert(\"onblur text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onblur='alert(\"onblur text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 13
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static HtmlPage deleteClient(final HtmlPage registeredClientPage) throws IOException {
    final HtmlForm deleteForm = registeredClientPage.getFormByName("deleteForm");
    assertNotNull(deleteForm);

    // Delete the client
    final HtmlButton button = deleteForm.getButtonByName("submit_delete_button");
    return button.click();
}
 
Example 14
Source File: WebClientTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Previous window should become current window after current window is closed.
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = {},
        IE = "Third page loaded")
public void windowTracking_SpecialCase3() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String html1 = "<html><head><title>First</title></head>\n"
            + "<body>\n"
            + "<button id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</button>\n"
            + "</body></html>";
    conn.setResponse(URL_FIRST, html1);

    final String html2 = "<html><head><title>Second</title></head>\n"
            + "<body onUnload='doTest()'>\n"
            + "<form name='form1' action='" + URL_THIRD + "'>\n"
            + "<button id='clickme' type='button' onclick='postBack();'>Submit</button></form>\n"
            + "<script>\n"
            + "    function doTest() {\n"
            + "      window.close();\n"
            + "    }\n"
            + "    function postBack() {\n"
            + "      var frm = document.forms[0];\n"
            + "      frm.submit();\n"
            + "    }\n"
            + "</script></body></html>";
    conn.setResponse(URL_SECOND, html2);

    final String html3 = "<html><head><title>Third</title>\n"
            + "<script type=\"text/javascript\">\n"
            + "     alert('Third page loaded');\n"
            + "     window.close();\n"
            + "</script></head></html>";
    conn.setResponse(URL_THIRD, html3);
    conn.setDefaultResponse(html3);

    webClient.setWebConnection(conn);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    buttonA.click();
    final HtmlPage secondPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
    assertEquals("Second", secondPage.getTitleText());

    final HtmlButton buttonB = secondPage.getHtmlElementById("clickme");
    buttonB.click();
    assertEquals("First", ((HtmlPage) webClient.getCurrentWindow().getEnclosedPage()).getTitleText());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 15
Source File: GSWorker.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
protected IStatus initSession() {
	IStatus s;
	try {
		// load main page
		HtmlPage page = webClient.getPage(scholar);

		// Timeouts
		s = waitLikeUser();
		if (!s.isOK())
			return s;
		s = waitForJs(page);
		if (!s.isOK())
			return s;

		// select link to settings form, load settings form
		HtmlAnchor configLink = page.getFirstByXPath("//a[contains(@href, '/scholar_settings')]");
		HtmlPage configFormPage = webClient
				.getPage(configLink.getBaseURI() + configLink.getHrefAttribute().substring(1));

		// Timeouts
		s = waitLikeUser();
		if (!s.isOK())
			return s;
		s = waitForJs(page);
		if (!s.isOK())
			return s;

		// select settings form and enable bibtex
		HtmlForm configForm = configFormPage.getFirstByXPath("//form[@action='/scholar_setprefs']");
		for (HtmlInput r : configForm.getInputsByName("scis")) {
			if (r.getAttribute("value").equals("yes")) {
				r.setChecked(true);
				break;
			}
		}

		// click save
		HtmlButton configFormSave = configForm.getButtonByName("save");
		HtmlPage readyForSearch = configFormSave.click();

		// Timeouts
		s = waitLikeUser();
		if (!s.isOK())
			return s;
		s = waitForJs(page);
		if (!s.isOK())
			return s;
	} catch (FailingHttpStatusCodeException | IOException e) {
		return new Status(Status.ERROR, "de.tudresden.slr.googlescholar",
				"Could not initialize Google Scholar session", e);
	}

	return Status.OK_STATUS;
}