Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getElementById()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getElementById() . 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: HtmlUnitTests.java    From demo with MIT License 6 votes vote down vote up
/**
 * Tests the entire process of lending -
 * registers a book, a borrower, and lends it.
 * See {{@link SeleniumTests#test_shouldLendBook} for a
 * full javascript version that runs on Chrome
 */
@Test
public void test_shouldLendBook() {
    getPage("http://localhost:8080/demo/flyway");
    HtmlPage page = getPage("http://localhost:8080/demo/library.html");
    type(page.getElementById("register_book"), "some book");
    page = click(page.getElementById("register_book_submit"));
    page = click(page.getAnchorByText("Return"));
    type(page.getElementById("register_borrower"), "some borrower");
    page = click(page.getElementById("register_borrower_submit"));
    page = click(page.getAnchorByText("Return"));
    type(page.getElementById("lend_book"), "some book");
    type(page.getElementById("lend_borrower"), "some borrower");
    page = click(page.getElementById("lend_book_submit"));
    final DomElement result = page.getElementById("result");

    assertEquals("SUCCESS", result.getTextContent());
}
 
Example 2
Source File: HtmlUnitTests.java    From demo with MIT License 6 votes vote down vote up
/**
 * Testing that the UI for registering and logging in a user (a librarian) works without javascript.
 */
@Test
public void test_shouldRegisterAndLoginUser() {
    getPage("http://localhost:8080/demo/flyway");
    String username = "some user";
    String password = "asdflkajsdfl;aksjdfal;sdfkj";
    ApiCalls.registerUser(username, password);

    HtmlPage page = getPage("http://localhost:8080/demo/library.html");
    type(page.getElementById("login_username"), username);
    type(page.getElementById("login_password"), password);
    page = click(page.getElementById("login_submit"));
    final DomElement loginResult = page.getElementById("result");

    assertTrue("result was " + loginResult.getTextContent(),
            loginResult.getTextContent().contains("access granted"));
}
 
Example 3
Source File: WebClient8Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if something goes wrong
 */
@Test
public void iFrameTextContent() throws Exception {
    final String html = "<html>\n"
            + "<head><title>foo</title></head>\n"
            + "<body>\n"
            + "  <iframe id='tester' src='second.html'></iframe>\n"
            + "</body></html>";

    final String plainContent = "plain frame content";

    try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
        final MockWebConnection webConnection = getMockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(plainContent, 200, "OK", MimeType.TEXT_PLAIN);
        webClient.setWebConnection(webConnection);

        final HtmlPage page = webClient.getPage(URL_FIRST);

        final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
        assertEquals(plainContent, ((TextPage) iFrame.getEnclosedWindow().getEnclosedPage()).getContent());
    }
}
 
Example 4
Source File: PageFunctionTest.java    From tool.accelerate.core with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
// TODO: This method of testing does not work for angular, need to find an alternative method of testing
public void techFormTest() {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    HtmlPage page;
    String port = System.getProperty("liberty.test.port");
    try {
        page = webClient.getPage("http://localhost:" + port + "/start/");
        DomElement techForm = page.getElementById("techTable");
        DomElement formBody = techForm.getFirstElementChild();
        int count = formBody.getChildElementCount();
        // We expect there to be more than one child element, otherwise the 
        // javascript has not created the tech table properly.
        assertTrue("Expected more than one element in the tech table, instead found " + count, count > 1);
    } catch (Exception e){
        org.junit.Assert.fail("Caught exception: " + e.getCause().toString());
    } finally {
        webClient.close();
    }
}
 
Example 5
Source File: ChannelServiceTestUtils.java    From joynr with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a list of channel IDs that are displayed on the channels.html page for the bounce proxy.
 * 
 * @param webClient
 * @param url
 * @return
 * @throws Exception
 */
public static List<String> getChannelIdsOnChannelsHtml(WebClient webClient, String url) throws Exception {

    HtmlPage page = webClient.getPage(url);
    webClient.waitForBackgroundJavaScript(2000);

    DomElement channelsTable = page.getElementById("channels");

    List<String> channelIds = new LinkedList<String>();
    for (DomElement channelsTableRows : channelsTable.getChildElements()) {
        if (channelsTableRows.getTagName().equals("tbody")) {

            for (DomElement channelRows : channelsTableRows.getChildElements()) {

                String channelId = channelRows.getChildNodes().get(0).getTextContent();
                if (isProperChannelId(channelId)) {
                    channelIds.add(channelId);
                }
            }
        }
    }
    return channelIds;
}
 
Example 6
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteWithoutCsrfFieldFailsWithStatusCode403() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/exception-delete");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(403, result.getWebResponse()
        .getStatusCode());
}
 
Example 7
Source File: HiddenFormMethodIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
private HtmlForm setHiddenMethodInForm(final HtmlPage page, final String method) {
    final HtmlForm form = (HtmlForm) page.getElementById("form");
    final HtmlHiddenInput hiddenMethod = (HtmlHiddenInput) page
        .getElementById("hidden-method");
    hiddenMethod.setValueAttribute(method);
    return form;
}
 
Example 8
Source File: HiddenFormMethodIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProcessFormMethodWhenNoHiddenMethodIsSpecified() throws Exception {
    final HtmlPage formPage = webClient
        .getPage(baseURL + "resources/forms?without-hidden-field=true");
    final HtmlForm form = (HtmlForm) formPage.getElementById("form");

    final HtmlPage okPage = submit(form);

    assertEquals("POST", okPage.getElementById("invoked-method").getTextContent());
}
 
Example 9
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostWithCsrfFieldWorksWithStatusCode200() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/ok-post");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(200, result.getWebResponse()
        .getStatusCode());
}
 
Example 10
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatchWithCsrfFieldWorksWithStatusCode200() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/ok-patch");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(200, result.getWebResponse()
        .getStatusCode());
}
 
Example 11
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteWithCsrfFieldWorksWithStatusCode200() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/ok-delete");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(200, result.getWebResponse()
        .getStatusCode());
}
 
Example 12
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutWithoutCsrfFieldFailsWithStatusCode403() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/exception-put");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(403, result.getWebResponse()
        .getStatusCode());
}
 
Example 13
Source File: CsrfValidateFilterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatchWithoutCsrfFieldFailsWithStatusCode403() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/csrf-methods/exception-patch");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final Page result = button.click();

    assertEquals(403, result.getWebResponse()
        .getStatusCode());
}
 
Example 14
Source File: PropertyTable.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
/**
 * Creates a new instance of {@link PropertyTable}.
 *
 * @param page
 *         the whole details HTML page
 * @param property
 *         the property tab to extract
 */
@SuppressFBWarnings("BC")
public PropertyTable(final HtmlPage page, final String property) {
    super(page);

    title = getTitleOfTable(page, property);

    DomElement propertyElement = page.getElementById(property);
    assertThat(propertyElement).isInstanceOf(HtmlTable.class);

    HtmlTable table = (HtmlTable) propertyElement;
    List<HtmlTableRow> tableHeaderRows = table.getHeader().getRows();
    assertThat(tableHeaderRows).hasSize(1);

    HtmlTableRow header = tableHeaderRows.get(0);
    List<HtmlTableCell> cells = header.getCells();
    assertThat(cells).hasSize(3);

    propertyName = cells.get(0).getTextContent();
    assertThat(cells.get(1).getTextContent()).isEqualTo("Total");
    assertThat(cells.get(2).getTextContent()).isEqualTo("Distribution");

    List<HtmlTableBody> bodies = table.getBodies();
    assertThat(bodies).hasSize(1);
    List<HtmlTableRow> contentRows = bodies.get(0).getRows();

    for (HtmlTableRow row : contentRows) {
        List<HtmlTableCell> rowCells = row.getCells();
        rows.add(new PropertyRow(rowCells));
    }
}
 
Example 15
Source File: AnnotationDrivenConverterIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCorrectCustomConverterIsUsedForDoubleValue() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/converter-annotations");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final HtmlPage resultPage = button.click();

    final double result = Double.parseDouble(resultPage.getElementById("result").getTextContent());

    assertEquals(42.0D, result, 0);
}
 
Example 16
Source File: ConverterPriorityIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCorrectCustomConverterIsUsedForDoubleValue() throws Exception {
    final HtmlPage page1 = webClient.getPage(baseURL + "resources/converter");
    final HtmlForm form = (HtmlForm) page1.getElementById("form");
    final HtmlSubmitInput button = form.getInputByName("submit");

    final HtmlPage resultPage = button.click();

    final double result = Double.parseDouble(resultPage.getElementById("result").getTextContent());

    assertEquals(42.0D, result, 0);
}
 
Example 17
Source File: SimpleRangeTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if test fails
 */
@Test
public void toStringOneNode() throws Exception {
    final String content = "<html><head><title>Test page</title></head>\n"
        + "<body>\n"
        + "  <input type='text' id='myInput' value='abcd'>\n"
        + "</body>\n"
        + "</html>";

    final HtmlPage page = loadPage(content);
    final Node node = page.getElementById("myInput");

    // select all
    SimpleRange range = new SimpleRange(node, 0, node, 4);
    assertEquals("abcd", range.toString());

    // select part
    range = new SimpleRange(node, 1, node, 3);
    assertEquals("bc", range.toString());

    // wrong start offset
    range = new SimpleRange(node, 7, node, 3);
    assertEquals("", range.toString());

    // wrong end offset
    range = new SimpleRange(node, 0, node, 11);
    assertEquals("abcd", range.toString());
}
 
Example 18
Source File: WebClient8Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if something goes wrong
 */
@Test
public void iFrameInFragment() throws Exception {
    final String html = "<html>\n"
            + "<head><title>foo</title></head>\n"
            + "<body>\n"
            + "<p id='para'>hello</p>\n"
            + "</body></html>";

    final String html2 = "<html>\n"
            + "<head><title>frame</title></head>\n"
            + "<body>\n"
            + "</body></html>";

    final String fragment = "<iframe id='tester' src='second.html'></iframe>";

    try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
        final MockWebConnection webConnection = getMockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(html2);
        webClient.setWebConnection(webConnection);

        final HtmlPage page = webClient.getPage(URL_FIRST);
        final DomElement para = page.getElementById("para");
        page.getWebClient().getPageCreator().getHtmlParser().parseFragment(para, fragment);

        final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
        assertEquals("frame", ((HtmlPage) iFrame.getEnclosedWindow().getEnclosedPage()).getTitleText());
    }
}
 
Example 19
Source File: XMLHttpRequest3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Test for a strange error we found: An ajax running
 * in parallel shares the additional headers with a form
 * submit.
 *
 * @throws Exception if an error occurs
 */
@Test
public void ajaxInfluencesSubmitHeaders() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/content.html", ContentServlet.class);
    servlets.put("/ajax_headers.html", AjaxHeaderServlet.class);
    servlets.put("/form_headers.html", FormHeaderServlet.class);
    startWebServer("./", null, servlets);

    collectedHeaders_.clear();
    XMLHttpRequest3Test.STATE_ = 0;
    final WebClient client = getWebClient();

    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final HtmlPage page = client.getPage(URL_FIRST + "content.html");
    final DomElement elem = page.getElementById("doIt");
    while (STATE_ < 1) {
        Thread.sleep(42);
    }
    ((HtmlSubmitInput) elem).click();

    client.waitForBackgroundJavaScript(DEFAULT_WAIT_TIME);
    assertEquals(collectedHeaders_.toString(), 2, collectedHeaders_.size());

    String headers = collectedHeaders_.get(0);
    if (!headers.startsWith("Form: ")) {
        headers = collectedHeaders_.get(1);
    }
    assertTrue(headers, headers.startsWith("Form: "));
    assertFalse(headers, headers.contains("Html-Unit=is great,;"));

    headers = collectedHeaders_.get(0);
    if (!headers.startsWith("Ajax: ")) {
        headers = collectedHeaders_.get(1);
    }
    assertTrue(headers, headers.startsWith("Ajax: "));
    assertTrue(headers, headers.contains("Html-Unit=is great,;"));
}
 
Example 20
Source File: SummaryBox.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
/**
 * Creates a new summary box.
 *
 * @param buildPage
 *         the build page
 * @param id
 *         the ID of the static analysis tool
 */
public SummaryBox(final HtmlPage buildPage, final String id) {
    title = buildPage.getElementById(id + "-title");
    summary = buildPage.getElementById(id + "-summary");

    if (summary == null) {
        items = new ArrayList<>();
    }
    else {
        List<DomElement> elements = summary.getByXPath("./ul/li");
        items = elements.stream().map(el -> StringUtils.strip(el.getTextContent())).collect(Collectors.toList());
    }
}