MCPcopy Create free account
hub / github.com/HtmlUnit/htmlunit / getByXPath

Method getByXPath

src/test/java/org/htmlunit/html/DomNodeTest.java:317–354  ·  view source on GitHub ↗

@throws Exception if the test fails

()

Source from the content-addressed store, hash-verified

315 * @throws Exception if the test fails
316 */
317 @Test
318 public void getByXPath() throws Exception {
319 final String htmlContent = DOCTYPE_HTML
320 + "<html>\n"
321 + " <head>\n"
322 + " <title>my title</title>\n"
323 + " </head>"
324 + " <body>\n"
325 + " <div id='d1'>\n"
326 + " <ul>\n"
327 + " <li>foo 1</li>\n"
328 + " <li>foo 2</li>\n"
329 + " </ul>\n"
330 + " </div>\n"
331 + " <div><span>bla</span></div>\n"
332 + "</body>\n"
333 + "</html>";
334 final HtmlPage page = loadPage(htmlContent);
335
336 final List<?> results = page.getByXPath("//title");
337 assertEquals(1, results.size());
338 final HtmlTitle title = (HtmlTitle) results.get(0);
339 assertEquals("my title", title.asNormalizedText());
340
341 final HtmlHead head = (HtmlHead) title.getParentNode();
342 assertEquals(results, head.getByXPath("//title"));
343 assertEquals(results, head.getByXPath("./title"));
344 assertTrue(head.getByXPath("/title").isEmpty());
345 assertEquals(results, head.getByXPath("title"));
346
347 final HtmlElement div = page.getFirstByXPath("//div");
348 assertSame(div, page.getHtmlElementById("d1"));
349 final List<?> lis = div.getByXPath("ul/li");
350 assertEquals(2, lis.size());
351 assertEquals(lis, page.getByXPath("//ul/li"));
352
353 assertEquals(2, div.<Number>getFirstByXPath("count(//li)").intValue());
354 }
355
356 /**
357 * @throws Exception if the test fails

Callers 15

cloneNodeMethod · 0.45
getLocalNameMethod · 0.45
xpath2Method · 0.45
getByXPath_XMLMethod · 0.45
getCanonicalXPathMethod · 0.45
elementOrderMethod · 0.45
listAttributesResultMethod · 0.45
xpathMethod · 0.45
fillMDNWebAPIMethod · 0.45

Calls 11

getHtmlElementByIdMethod · 0.95
assertTrueMethod · 0.80
assertSameMethod · 0.80
isEmptyMethod · 0.65
loadPageMethod · 0.45
assertEqualsMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45
asNormalizedTextMethod · 0.45
getParentNodeMethod · 0.45
getFirstByXPathMethod · 0.45

Tested by

no test coverage detected