Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlTextInput

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlTextInput. These examples are extracted from open source projects. 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 Project: demo   Source File: HtmlUnitTests.java    License: MIT License 6 votes vote down vote up
private void type(DomElement input, String text) {
    try {
        // try to send text assuming it's a text input...
        ((HtmlTextInput) input).type(text);
    } catch (Exception ex) {
        // if we fail *because* it's a password input
        if (ex.getMessage().contains("HtmlPasswordInput cannot be cast to class com.gargoylesoftware.htmlunit.html.HtmlTextInput")) {
            try {
                // try to use it as a password input.
                ((HtmlPasswordInput) input).type(text);
            } catch (Exception ex1) {
                throw new RuntimeException(ex1);
            }
        }
    }
}
 
Example 2
Source Project: htmlunit   Source File: JavaScriptEngineTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void setJavascriptEnabled_false() throws Exception {
    final String html
        = "<html><head><title>foo</title><script>\n"
        + "  document.form1.textfield1 = 'blue'"
        + "</script></head><body>\n"
        + "<p>hello world</p>\n"
        + "<form name='form1'>\n"
        + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
        + "  <input type='text' name='textfield2' id='textfield2'/>\n"
        + "</form>\n"
        + "</body></html>";

    getWebClientWithMockWebConnection().getOptions().setJavaScriptEnabled(false);

    final HtmlPage page = loadPageWithAlerts(html);

    final HtmlTextInput textInput = page.getHtmlElementById("textfield1");
    assertEquals("foo", textInput.getValueAttribute());
}
 
Example 3
Source Project: htmlunit   Source File: JavaScriptEngineTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tries to set the value of a text input field.
 * @throws Exception if the test fails
 */
@Test
public void setInputValue() throws Exception {
    final String content
        = "<html><head><title>foo</title><script>\n"
        + "function doTest() {\n"
        + "  document.form1.textfield1.value = 'blue'"
        + "}\n"
        + "</script></head><body onload='doTest()'>\n"
        + "<p>hello world</p>\n"
        + "<form name='form1'>\n"
        + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
        + "  <input type='text' name='textfield2' id='textfield2'/>\n"
        + "</form>\n"
        + "</body></html>";
    final List<String> collectedAlerts = null;
    final HtmlPage page = loadPage(content, collectedAlerts);

    final HtmlTextInput textInput = page.getHtmlElementById("textfield1");
    assertEquals("blue", textInput.getValueAttribute());
}
 
Example 4
Source Project: htmlunit   Source File: HTMLInputElement2Test.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"hello", "me te"})
public void selectionRange() throws Exception {
    final String html
        = "<html><head><title>foo</title><script>\n"
        + "function test() {\n"
        + "  var input = document.getElementById('myInput');\n"
        + "  input.setSelectionRange(2, 7);\n"
        + "  alert('hello');\n"
        + "}\n"
        + "</script></head>\n"
        + "<body onload='test()'>\n"
        + "  <input id='myInput' value='some test'>\n"
        + "</body></html>";

    final String[] expected = getExpectedAlerts();
    setExpectedAlerts(new String[] {expected[0]});
    final HtmlPage page = loadPageWithAlerts(html);
    final HtmlTextInput input = page.getHtmlElementById("myInput");

    assertEquals(expected[1], input.getSelectedText());
}
 
Example 5
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldShowNodeConfigurationPage() throws Exception {
    EC2FleetCloud cloud = new EC2FleetCloud(null, null, null, null, null, null, null,
            null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud);

    j.jenkins.addNode(new EC2FleetNode("node-name", "", "", 1,
            Node.Mode.EXCLUSIVE, "", new ArrayList<NodeProperty<?>>(), cloud,
            j.createComputerLauncher(null)));

    HtmlPage page = j.createWebClient().goTo("computer/node-name/configure");

    assertTrue(StringUtils.isNotBlank(((HtmlTextInput) getElementsByNameWithoutJdk(page, "_.name").get(0)).getText()));
}
 
Example 6
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReplaceCloudForNodesAfterConfigurationSave() throws Exception {
    EC2FleetCloud cloud = new EC2FleetCloud(null, null, null, null, null, null, null,
            null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud);

    j.jenkins.addNode(new EC2FleetNode("mock", "", "", 1,
            Node.Mode.EXCLUSIVE, "", new ArrayList<NodeProperty<?>>(), cloud,
            j.createComputerLauncher(null)));

    HtmlPage page = j.createWebClient().goTo("configure");
    HtmlForm form = page.getFormByName("config");

    ((HtmlTextInput) getElementsByNameWithoutJdk(page, "_.name").get(0)).setText("a");

    HtmlFormUtil.submit(form);

    final Cloud newCloud = j.jenkins.clouds.get(0);
    assertNotSame(cloud, newCloud);

    assertSame(newCloud, ((EC2FleetNode) j.jenkins.getNode("mock")).getCloud());
}
 
Example 7
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldShowMultipleClouds() throws IOException, SAXException {
    Cloud cloud1 = new EC2FleetCloud("a", null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud1);

    Cloud cloud2 = new EC2FleetCloud("b", null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud2);

    HtmlPage page = j.createWebClient().goTo("configure");

    List<DomElement> elementsByName = getElementsByNameWithoutJdk(page, "_.name");
    assertEquals(2, elementsByName.size());
    assertEquals("a", ((HtmlTextInput) elementsByName.get(0)).getText());
    assertEquals("b", ((HtmlTextInput) elementsByName.get(1)).getText());
}
 
Example 8
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldShowMultipleCloudsWithDefaultName() throws IOException, SAXException {
    Cloud cloud1 = new EC2FleetCloud(null, null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud1);

    Cloud cloud2 = new EC2FleetCloud(null, null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud2);

    HtmlPage page = j.createWebClient().goTo("configure");

    List<DomElement> elementsByName = getElementsByNameWithoutJdk(page, "_.name");
    assertEquals(2, elementsByName.size());
    assertEquals("FleetCloud", ((HtmlTextInput) elementsByName.get(0)).getText());
    assertEquals("FleetCloud", ((HtmlTextInput) elementsByName.get(1)).getText());
}
 
Example 9
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateProperCloudWhenMultiple() throws IOException, SAXException {
    EC2FleetCloud cloud1 = new EC2FleetCloud(null, null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud1);

    EC2FleetCloud cloud2 = new EC2FleetCloud(null, null, null, null, null, null,
            null, null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud2);

    HtmlPage page = j.createWebClient().goTo("configure");
    HtmlForm form = page.getFormByName("config");

    ((HtmlTextInput) getElementsByNameWithoutJdk(page, "_.name").get(0)).setText("a");

    HtmlFormUtil.submit(form);

    assertEquals("a", j.jenkins.clouds.get(0).name);
    assertEquals("FleetCloud", j.jenkins.clouds.get(1).name);
}
 
Example 10
Source Project: cxf-fediz   Source File: AbstractOIDCTest.java    License: 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 11
Source Project: tutorials   Source File: HtmlUnitAndSpringLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenAMessage_whenSent_thenItShows() throws Exception {
    String text = "Hello world!";
    HtmlPage page;

    String url = "http://localhost/message/showForm";
    page = webClient.getPage(url);

    HtmlTextInput messageText = page.getHtmlElementById("message");
    messageText.setValueAttribute(text);

    HtmlForm form = page.getForms().get(0);
    HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
    HtmlPage newPage = submit.click();

    String receivedText = newPage.getHtmlElementById("received").getTextContent();

    Assert.assertEquals(receivedText, text);
}
 
Example 12
@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 13
Source Project: htmlunit   Source File: HTMLInputElement.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Select this element.
 */
@JsxFunction
public void select() {
    final HtmlInput input = getDomNodeOrDie();
    if (input instanceof HtmlTextInput) {
        ((HtmlTextInput) input).select();
    }
    // currently nothing for other input types
}
 
Example 14
Source Project: htmlunit   Source File: JavaScriptEngineTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test case for bug 707134. Currently I am unable to reproduce the problem.
 * @throws Exception if the test fails
 */
@Test
public void functionDefinedInSameFile() throws Exception {
    final String htmlContent
        = "<html><head><title>First</title><script>\n"
        + "function showFoo(foo) {\n"
        + "  alert('Foo is: |' + foo + '|');\n"
        + "}\n"
        + "</script>\n"
        + "</head><body><form name='form1'>\n"
        + "<input name='text1' type='text'>\n"
        + "<input name='button1' type='button' onclick='showFoo(document.form1.text1.value);'>\n"
        + "</form></body></html>";

    final List<String> collectedAlerts = new ArrayList<>();

    final HtmlPage page = loadPage(htmlContent, collectedAlerts);
    assertEquals("First", page.getTitleText());

    final HtmlForm form = page.getFormByName("form1");
    final HtmlTextInput textInput = form.getInputByName("text1");
    textInput.setValueAttribute("flintstone");

    final HtmlButtonInput button = form.getInputByName("button1");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);

    button.click();

    assertEquals(new String[] {"Foo is: |flintstone|"}, collectedAlerts);
}
 
Example 15
Source Project: HtmlUnit-Android   Source File: HTMLInputElement.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Select this element.
 */
@JsxFunction
public void select() {
    final HtmlInput input = getDomNodeOrDie();
    if (input instanceof HtmlTextInput) {
        ((HtmlTextInput) input).select();
    }
    // currently nothing for other input types
}
 
Example 16
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldShowAsHiddenCloudIdAsOldId() throws IOException, SAXException {
    Cloud cloud = new EC2FleetCloud(null, null, null, null, null, null, null,
            null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud);

    HtmlPage page = j.createWebClient().goTo("configure");

    assertTrue(StringUtils.isNotBlank(((HtmlTextInput) getElementsByNameWithoutJdk(page, "_.oldId").get(0)).getText()));
}
 
Example 17
Source Project: ec2-spot-jenkins-plugin   Source File: UiIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldShowInConfigurationClouds() throws IOException, SAXException {
    Cloud cloud = new EC2FleetCloud(null, null, null, null, null, null, null,
            null, null, null, false, false,
            0, 0, 0, 0, false, false,
            false, 0, 0, false,
            10, false);
    j.jenkins.clouds.add(cloud);

    HtmlPage page = j.createWebClient().goTo("configure");

    assertEquals("ec2-fleet", ((HtmlTextInput) getElementsByNameWithoutJdk(page, "_.labelString").get(1)).getText());
}
 
Example 18
Source Project: cxf-fediz   Source File: AbstractOIDCTest.java    License: Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testEditClient() throws Exception {
    try (WebClient webClient = setupWebClientIDP("alice", "ecila")) {
        HtmlPage registeredClientPage = login(oidcEndpointBuilder("/console/clients/" + publicClientId),
            webClient);

        final HtmlPage editClientPage = registeredClientPage.getAnchorByText("public-client").click();

        final HtmlForm form = editClientPage.getForms().get(0);

        // Set new client values
        final HtmlTextInput clientNameInput = form.getInputByName("client_name");
        final String newClientName = "public-client-modified";
        clientNameInput.setValueAttribute(newClientName);
        final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
        assertTrue(clientTypeSelect.isDisabled());
        final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
        assertEquals(REDIRECT_URL, redirectURIInput.getText());
        final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
        assertEquals("https://ws.apache.org", clientAudienceURIInput.getText());
        final HtmlTextInput clientLogoutURI = form.getInputByName("client_logoutURI");
        assertEquals(LOGOUT_URL, clientLogoutURI.getText());

        registeredClientPage = form.getButtonByName("submit_button").click();
        assertNotNull(registeredClientPage.getAnchorByText(newClientName));

        final HtmlPage registeredClientsPage = registeredClientPage.getAnchorByText("registered Clients").click();

        HtmlTable table = registeredClientsPage.getHtmlElementById("registered_clients");
        assertEquals("2 clients", table.getRows().size(), 3);
        boolean updatedClientFound = false;
        for (final HtmlTableRow row : table.getRows()) {
            if (newClientName.equals(row.getCell(0).asText())) {
                updatedClientFound = true;
                break;
            }
        }
        assertTrue(updatedClientFound);
    }
}