@throws Exception if the test fails
()
| 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 |
no test coverage detected