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

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getFirstByXPath() . 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: HTMLParserTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the new HTMLParser on a simple HTML string.
 * @throws Exception failure
 */
@Test
public void simpleHTMLString() throws Exception {
    final WebClient webClient = getWebClient();
    final WebResponse webResponse = new StringWebResponse(
        "<html><head><title>TITLE</title></head><body><div>TEST</div></body></html>", URL_FIRST);

    final HtmlPage page = webClient.getPageCreator().getHtmlParser()
                                        .parseHtml(webResponse, webClient.getCurrentWindow());

    final String stringVal = page.<HtmlDivision>getFirstByXPath("//div").getFirstChild().getNodeValue();
    assertEquals("TEST", stringVal);

    final HtmlElement node = (HtmlElement) page.getFirstByXPath("//*[./text() = 'TEST']");
    assertEquals(node.getTagName(), HtmlDivision.TAG_NAME);
}
 
Example 2
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test if option/text() is cleaned like other text().
 * @throws Exception if test fails
 */
@Test
public void optionText_getFirstByXPath() throws Exception {
    final String content = "<html><head><title>Test page</title></head>\n"
        + "<body><form name='foo'>\n"
        + "<select name='test'><option value='1'>foo&nbsp;and&nbsp;foo</option></select>\n"
        + "</form></body></html>";

    final HtmlPage page = loadPage(content);
    final String value = page.getFirstByXPath("string(//option)");
    final int[] expectedValues = {102, 111, 111, 160, 97, 110, 100, 160, 102, 111, 111};
    int index = 0;
    for (final int v : expectedValues) {
        if (value.codePointAt(index++) != v) {
            fail();
        }
    }
}
 
Example 3
Source File: JadeIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfiguration() throws Exception {
    final HtmlPage page = webClient.getPage(baseURL + "jade/config");
    final DomElement systemProperties = page.getFirstByXPath("//p[@class='SystemProperties']");
    assertEquals("SystemProperties", systemProperties.getTextContent());
    final DomElement configFile = page.getFirstByXPath("//p[@class='ConfigFile']");
    assertEquals("ConfigFile", configFile.getTextContent());
}
 
Example 4
Source File: S2jhHtmlParseFilter.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isParseDataFetchLoaded(HtmlPage page) {
    HtmlDivision div = page.getFirstByXPath("//DIV[@id='description']/DIV[@class='content ke-post']");
    if (div != null && div.getChildElementCount() > 0) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Product description content HTML: {}", asString(div));
        }
        return true;
    }
    return false;
}
 
Example 5
Source File: JadeIT.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfiguration() throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "jade/config");
    HtmlParagraph systemProperties = page.getFirstByXPath("//p[@class='SystemProperties']");
    assertEquals("SystemProperties", systemProperties.asText());
    HtmlParagraph configFile = page.getFirstByXPath("//p[@class='ConfigFile']");
    assertEquals("ConfigFile", configFile.asText());
}
 
Example 6
Source File: JadeIT.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsesModel() throws Exception {
    String path = webUrl + "jade?user=mvc%d";
    HtmlPage page = webClient.getPage(String.format(path, 0));
    assertTrue(page.getTitleText().contains("Jade"));
    for (int i = 0; i < 10; i++) { // just to ensure that not the whole page is cached
        page = webClient.getPage(String.format(path, i));
        HtmlHeading1 h1 = page.getFirstByXPath("//html/body/h1");
        assertTrue(h1.asText().contains("mvc" + i));
    }
}
 
Example 7
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 8
Source File: JadeIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsesModel() throws Exception {
    String path = baseURL + "jade?user=mvc%d";
    final HtmlPage page = webClient.getPage(String.format(path, 0));
    assertTrue(page.getTitleText().contains("Jade"));
    for (int i = 0; i < 10; i++) { // just to ensure that not the whole page is cached
        final HtmlPage subPage = webClient.getPage(String.format(path, i));
        final DomElement h1 = subPage.getFirstByXPath("//html/body/h1");
        assertTrue(h1.getTextContent().contains("mvc" + i));
    }
}
 
Example 9
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Test evaluation relative from elements other than the whole page.
 * @throws Exception if test fails
 */
@Test
public void xpathFromElement() throws Exception {
    final String content = "<html><head><title>Test page</title></head>\n"
        + "<body><a href='foo.html' id='myLink'>foo</a></body>\n"
        + "</html>";

    final HtmlPage page = loadPage(content);
    final HtmlBody body = page.getFirstByXPath("/html/body");

    assertEquals(page.getHtmlElementById("myLink"), body.getFirstByXPath("./a"));
}
 
Example 10
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 11
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 12
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 13
Source File: JadeIT.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Test
public void testIncludesViews() throws Exception {
    final HtmlPage page = webClient.getPage(baseURL + "jade");
    final DomElement footer = page.getFirstByXPath("//p[@class='footer']");
    assertTrue(footer.getTextContent().contains("Eclipse Krazo committers and contributors"));
}
 
Example 14
Source File: JadeIT.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Test
public void testHelper() throws Exception {
    final HtmlPage page = webClient.getPage(baseURL + "jade/helper");
    final DomElement element = page.getFirstByXPath("//p[@class='result']");
    assertEquals("3", element.getTextContent());
}
 
Example 15
Source File: JadeIT.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Test
public void testIncludesViews() throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "jade");
    HtmlParagraph footer = page.getFirstByXPath("//p[@class='footer']");
    assertTrue(footer.asText().contains("Ivar Grimstad"));
}
 
Example 16
Source File: JadeIT.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Test
public void testUsesFilters() throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "jade/markdown");
    HtmlUnorderedList ul = page.getFirstByXPath("//html/body/ul");
    assertEquals(3, ul.getChildElementCount());
}
 
Example 17
Source File: JadeIT.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Test
public void testHelper() throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "jade/helper");
    HtmlParagraph result = page.getFirstByXPath("//p[@class='result']");
    assertEquals("3", result.asText());
}
 
Example 18
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;
}