Пример #1
0
 @Test
 public void testPrependElement() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.prependElement("p").text("Before");
   assertEquals("Before", div.child(0).text());
   assertEquals("Hello", div.child(1).text());
 }
Пример #2
0
  @Test
  public void testChildThrowsIndexOutOfBoundsOnMissing() {
    Document doc = Jsoup.parse("<div><p>One</p><p>Two</p></div>");
    Element div = doc.select("div").first();

    assertEquals(2, div.children().size());
    assertEquals("One", div.child(0).text());

    try {
      div.child(3);
      fail("Should throw index out of bounds");
    } catch (IndexOutOfBoundsException e) {
    }
  }
Пример #3
0
  @Test
  public void testGetElementById() {
    Document doc = Jsoup.parse(reference);
    Element div = doc.getElementById("div1");
    assertEquals("div1", div.id());
    assertNull(doc.getElementById("none"));

    Document doc2 =
        Jsoup.parse("<div id=1><div id=2><p>Hello <span id=2>world!</span></p></div></div>");
    Element div2 = doc2.getElementById("2");
    assertEquals("div", div2.tagName()); // not the span
    Element span = div2.child(0).getElementById("2"); // called from <p> context should be span
    assertEquals("span", span.tagName());
  }