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

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getByXPath() . 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: GWT250Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void dynaTable() throws Exception {
    startWebServer("src/test/resources/libraries/GWT/" + getDirectory() + "/DynaTable",
            new String[] {"src/test/resources/libraries/GWT/" + getDirectory() + "/gwt-servlet.jar"});

    final WebClient client = getWebClient();

    final String url = URL_FIRST + "DynaTable.html";
    final HtmlPage page = client.getPage(url);
    client.waitForBackgroundJavaScriptStartingBefore(2000);

    final String[] firstRow = {"Inman Mendez",
        "Majoring in Phrenology", "Mon 9:45-10:35, Tues 2:15-3:05, Fri 8:45-9:35, Fri 9:45-10:35"};

    final List<?> detailsCells = page.getByXPath("//table[@class='table']//tr[2]/td");
    assertEquals(firstRow.length, detailsCells.size());
    for (int i = 0; i < firstRow.length; i++) {
        final HtmlTableDataCell cell = (HtmlTableDataCell) detailsCells.get(i);
        assertElementValue(cell, firstRow[i]);
    }
}
 
Example 2
Source File: TvMaoCrawler.java    From MyTv with Apache License 2.0 6 votes vote down vote up
/**
 * 抓取指定城市下的所有电视台
 * 
 * @param htmlPage
 * @param city
 * @return
 */
private List<TvStation> getAllTvStationOfCity(HtmlPage htmlPage, String city) {
	List<TvStation> resultList = new ArrayList<TvStation>();
	List<?> elements = htmlPage
			.getByXPath("//div[@class='chlsnav']//div[@class='plst']/parent::*");
	for (int i = 0, size = elements == null ? 0 : elements.size(); i < size; i++) {
		try {
			HtmlAnchor anchor = (HtmlAnchor) elements.get(i);
			String href = anchor.getHrefAttribute();
			if (!href.startsWith("/program/")) {
				continue;
			}
			logger.debug(anchor.getTextContent()
					+ " program table of tvmao: " + ", url: " + href);
			TimeUnit.MILLISECONDS.sleep(getRandomSleepTime());
			HtmlPage p = (HtmlPage) WebCrawler.crawl(TV_MAO_URL_PREFIX
					+ href);
			resultList.addAll(getTvStations(p, city));
		} catch (Exception e) {
			logger.error("error occur while get all tv station of city: "
					+ city, e);
			continue;
		}
	}
	return resultList;
}
 
Example 3
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the elements are in the right order.
 * @throws Exception if test fails
 */
@Test
@SuppressWarnings("unchecked")
public void elementOrder() throws Exception {
    final String content
        = "<html><head><title>First</title><script>\n"
        + "</script></head><body>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(content);
    final List<?> list = page.getByXPath("//*");

    final String[] expected = {"html", "head", "title", "script", "body"};
    final List<String> actualNames = new ArrayList<>();
    for (final DomNode node : (List<DomNode>) list) {
        actualNames.add(node.getNodeName());
    }
    assertEquals(expected, actualNames);
}
 
Example 4
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains an input element with the specified name.
 *
 * @param page the page to check
 * @param name the name of the input element to look for
 */
public static void assertInputPresent(final HtmlPage page, final String name) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
}
 
Example 5
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an element matching the specified XPath
 * expression.
 *
 * @param page the page to check
 * @param xpath the XPath expression which is expected to not match an element in the page
 */
public static void assertElementNotPresentByXPath(final HtmlPage page, final String xpath) {
    final List<?> elements = page.getByXPath(xpath);
    if (!elements.isEmpty()) {
        final String msg = "The page does not contain any elements matching the XPath expression '" + xpath
                        + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 6
Source File: ServletTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
private String extractContent(final String endUrl, final String xpath) throws IOException {
    final String url = base.toExternalForm() + "jbatch/" + endUrl;
    final WebClient webClient = newWebClient();

    final HtmlPage page = webClient.getPage(url);
    final List<?> byXPath = page.getByXPath("//div[@id=\"content\"]" + xpath);
    assertTrue(byXPath.size() >= 1);

    final Object next = byXPath.iterator().next();
    if (!DomNode.class.isInstance(next)) {
        throw new IllegalArgumentException("Can't find text for " + next);
    }
    return DomNode.class.cast(next).asText();
}
 
Example 7
Source File: UIServletTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testRender() throws Exception {
    ScheduledTime end = new ScheduledTime("2015-09-03T13:17Z");
    ScheduledTime start = new ScheduledTime("2015-09-03T13:11Z");
    NavigableSet<ScheduledTime> tileTimes = new TreeSet<>(ImmutableSet.of(new ScheduledTime("2015-09-03T13:10Z"), new ScheduledTime("2015-09-03T13:15Z")));
    WorkflowID id = new WorkflowID("foo");
    List<WorkflowGroup> groups = ImmutableList.of(new WorkflowGroup("All workflows", ImmutableList.of(id)));
    WorkflowInfo workflowInfo = new WorkflowInfo(new URL("http://example.com"), ImmutableList.of());
    SlotState state1 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:16Z")), SlotState.Status.FAILURE);
    SlotState state2 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:12Z")), SlotState.Status.WAITING);
    List<SlotState> slotStates = ImmutableList.of(state1, state2);
    Map<WorkflowID, WorkflowStatus> statuses = ImmutableMap.of(id, new WorkflowStatus(workflowInfo, slotStates, false));
    UIConfiguration conf = new UIConfiguration(start, end, tileTimes, groups, statuses, new URL("http://example.com"));
    
    StringWebResponse response = new StringWebResponse(UIServlet.render(conf), new URL("http://example.com"));
    WebClient webClient = new WebClient();
    webClient.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = HTMLParser.parse(response, new TopLevelWindow("top", webClient));
    
    // Some basic sanity checking
    
    List<HtmlTableDataCell> slotCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'slot')]");
    Assert.assertEquals("fail", slotCells.get(0).getTextContent());
    Assert.assertEquals("wait", slotCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> hourCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'hour')]");
    Assert.assertEquals("1315", hourCells.get(0).getTextContent());
    Assert.assertEquals("1310", hourCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> workflowCells = (List<HtmlTableDataCell>) page.getByXPath("//td[@class='workflow']");
    Assert.assertEquals("foo", workflowCells.get(0).getTextContent());
    
    System.out.println(response.getContentAsString());
}
 
Example 8
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the input element with the specified name on the specified page contains the
 * specified value.
 *
 * @param page the page to check
 * @param name the name of the input element to check
 * @param value the value to check for
 */
public static void assertInputContainsValue(final HtmlPage page, final String name, final String value) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
    final HtmlInput input = (HtmlInput) list.get(0);
    final String s = input.getValueAttribute();
    if (!s.equals(value)) {
        throw new AssertionError("The input element named '" + name + "' contains the value '" + s
                        + "', not the expected value '" + value + "'.");
    }
}
 
Example 9
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an input element with the specified name.
 *
 * @param page the page to check
 * @param name the name of the input element to look for
 */
public static void assertInputNotPresent(final HtmlPage page, final String name) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (!list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
}
 
Example 10
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains an input element with the specified name.
 *
 * @param page the page to check
 * @param name the name of the input element to look for
 */
public static void assertInputPresent(final HtmlPage page, final String name) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
}
 
Example 11
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an element matching the specified XPath
 * expression.
 *
 * @param page the page to check
 * @param xpath the XPath expression which is expected to not match an element in the page
 */
public static void assertElementNotPresentByXPath(final HtmlPage page, final String xpath) {
    final List<?> elements = page.getByXPath(xpath);
    if (!elements.isEmpty()) {
        final String msg = "The page does not contain any elements matching the XPath expression '" + xpath
                        + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 12
Source File: TvMaoCrawler.java    From MyTv with Apache License 2.0 5 votes vote down vote up
/**
 * 解析指定城市下的电视台
 * 
 * @param htmlPage
 * @param city
 *            所属城市
 * @return
 */
private List<TvStation> getTvStations(HtmlPage htmlPage, String city) {
	String html = htmlPage.asXml();
	List<?> elements = htmlPage
			.getByXPath("//div[@class='chlsnav']/div[@class='pbar']/b");
	HtmlBold hb = (HtmlBold) elements.get(0);
	String classify = hb.getTextContent().trim();
	MyTvUtils.outputCrawlData(getCrawlerName(), html,
			getCrawlFileName(city, classify));
	List<TvStation> stationList = parseTvStation(city, html);
	logger.debug("tv station crawled." + stationList);
	return stationList;
}
 
Example 13
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an input element with the specified name.
 *
 * @param page the page to check
 * @param name the name of the input element to look for
 */
public static void assertInputNotPresent(final HtmlPage page, final String name) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (!list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
}
 
Example 14
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains an element matching the specified XPath expression.
 *
 * @param page the page to check
 * @param xpath the XPath expression which is expected to match an element in the page
 */
public static void assertElementPresentByXPath(final HtmlPage page, final String xpath) {
    final List<?> elements = page.getByXPath(xpath);
    if (elements.isEmpty()) {
        final String msg = "The page does not contain any elements matching the XPath expression '" + xpath
                        + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 15
Source File: GWT250Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void verifyStartMailBody(final HtmlPage page, final String... details) {
    final List<?> detailsCells = page.getByXPath("//div[@class='MGI']/text()");
    for (int i = 0; i < details.length; i++) {
        final DomText text = (DomText) detailsCells.get(i);
        assertEquals(details[i], text.asText());
    }
}
 
Example 16
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 17
Source File: HostExtractor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void fillMDNJavaScriptGlobalObjects(final WebClient webClient, final Set<String> set)
    throws Exception {
    final HtmlPage page
        = webClient.getPage("https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects");
    for (final Object o : page.getByXPath("//*[name()='code']/text()")) {
        String value = o.toString();
        if (!value.isEmpty()) {
            if (value.endsWith("()")) {
                value = value.substring(0, value.length() - 2);
            }

            set.add(value);
        }
    }
}
 
Example 18
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the input element with the specified name on the specified page does not
 * contain the specified value.
 *
 * @param page the page to check
 * @param name the name of the input element to check
 * @param value the value to check for
 */
public static void assertInputDoesNotContainValue(final HtmlPage page, final String name, final String value) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
    final HtmlInput input = (HtmlInput) list.get(0);
    final String s = input.getValueAttribute();
    if (s.equals(value)) {
        throw new AssertionError("The input element named '" + name + "' contains the value '" + s
                        + "', not the expected value '" + value + "'.");
    }
}
 
Example 19
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the input element with the specified name on the specified page contains the
 * specified value.
 *
 * @param page the page to check
 * @param name the name of the input element to check
 * @param value the value to check for
 */
public static void assertInputContainsValue(final HtmlPage page, final String name, final String value) {
    final String xpath = "//input[@name='" + name + "']";
    final List<?> list = page.getByXPath(xpath);
    if (list.isEmpty()) {
        throw new AssertionError("Unable to find an input element named '" + name + "'.");
    }
    final HtmlInput input = (HtmlInput) list.get(0);
    final String s = input.getValueAttribute();
    if (!s.equals(value)) {
        throw new AssertionError("The input element named '" + name + "' contains the value '" + s
                        + "', not the expected value '" + value + "'.");
    }
}
 
Example 20
Source File: HostExtractor.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private static void fillMDNWebAPI(final WebClient webClient, final Set<String> set) throws Exception {
    final HtmlPage page = webClient.getPage("https://developer.mozilla.org/en-US/docs/Web/API");
    for (final Object o : page.getByXPath("//*[@class='indexListTerm']")) {
        set.add(((HtmlElement) o).asText());
    }
}