コード例 #1
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
  @Test
  public void testSetHtmlTitle() {
    Document doc = Jsoup.parse("<html><head id=2><title id=1></title></head></html>");

    Element title = doc.getElementById("1");
    title.html("good");
    assertEquals("good", title.html());
    title.html("<i>bad</i>");
    assertEquals("&lt;i&gt;bad&lt;/i&gt;", title.html());

    Element head = doc.getElementById("2");
    head.html("<title><i>bad</i></title>");
    assertEquals("<title>&lt;i&gt;bad&lt;/i&gt;</title>", head.html());
  }
コード例 #2
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
  @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());
  }
コード例 #3
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
 @Test
 public void testSetHtml() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.html("<p>there</p><p>now</p>");
   assertEquals("<p>there</p><p>now</p>", TextUtil.stripNewlines(div.html()));
 }
コード例 #4
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
 @Test
 public void testAddNewText() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.appendText(" there & now >");
   assertEquals("<p>Hello</p> there &amp; now &gt;", TextUtil.stripNewlines(div.html()));
 }
コード例 #5
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
 @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());
 }
コード例 #6
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
  @Test
  public void testSetText() {
    String h = "<div id=1>Hello <p>there <b>now</b></p></div>";
    Document doc = Jsoup.parse(h);
    assertEquals("Hello there now", doc.text()); // need to sort out node whitespace
    assertEquals("there now", doc.select("p").get(0).text());

    Element div = doc.getElementById("1").text("Gone");
    assertEquals("Gone", div.text());
    assertEquals(0, doc.select("p").size());
  }
コード例 #7
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
 @Test
 public void testGetSiblings() {
   Document doc =
       Jsoup.parse("<div><p>Hello<p id=1>there<p>this<p>is<p>an<p id=last>element</div>");
   Element p = doc.getElementById("1");
   assertEquals("there", p.text());
   assertEquals("Hello", p.previousElementSibling().text());
   assertEquals("this", p.nextElementSibling().text());
   assertEquals("Hello", p.firstElementSibling().text());
   assertEquals("element", p.lastElementSibling().text());
 }
コード例 #8
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
  @Test
  public void testPrependNewHtml() {
    Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
    Element div = doc.getElementById("1");
    div.prepend("<p>there</p><p>now</p>");
    assertEquals("<p>there</p><p>now</p><p>Hello</p>", TextUtil.stripNewlines(div.html()));

    // check sibling index (reindexChildren):
    Elements ps = doc.select("p");
    for (int i = 0; i < ps.size(); i++) {
      assertEquals(i, ps.get(i).siblingIndex);
    }
  }
コード例 #9
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
  @Test
  public void testAddNewElement() {
    Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
    Element div = doc.getElementById("1");
    div.appendElement("p").text("there");
    div.appendElement("P").attr("CLASS", "second").text("now");
    // manually specifying tag and attributes should now preserve case, regardless of parse mode
    assertEquals(
        "<html><head></head><body><div id=\"1\"><p>Hello</p><p>there</p><P CLASS=\"second\">now</P></div></body></html>",
        TextUtil.stripNewlines(doc.html()));

    // check sibling index (with short circuit on reindexChildren):
    Elements ps = doc.select("p");
    for (int i = 0; i < ps.size(); i++) {
      assertEquals(i, ps.get(i).siblingIndex);
    }
  }
コード例 #10
0
  public boolean runTest(Test test) throws Exception {
    String filename = test.file.toString();
    if (this.verbose) {
      System.out.println(
          "Running "
              + filename
              + " against "
              + this.host
              + ":"
              + this.port
              + " with "
              + this.browser);
    }
    this.document = parseDocument(filename);

    if (this.baseUrl == null) {
      NodeList links = this.document.getElementsByTagName("link");
      if (links.getLength() != 0) {
        Element link = (Element) links.item(0);
        setBaseUrl(link.getAttribute("href"));
      }
    }
    if (this.verbose) {
      System.out.println("Base URL=" + this.baseUrl);
    }

    Node body = this.document.getElementsByTagName("body").item(0);
    Element resultContainer = document.createElement("div");
    resultContainer.setTextContent("Result: ");
    Element resultElt = document.createElement("span");
    resultElt.setAttribute("id", "result");
    resultElt.setIdAttribute("id", true);
    resultContainer.appendChild(resultElt);
    body.insertBefore(resultContainer, body.getFirstChild());

    Element executionLogContainer = document.createElement("div");
    executionLogContainer.setTextContent("Execution Log:");
    Element executionLog = document.createElement("div");
    executionLog.setAttribute("id", "log");
    executionLog.setIdAttribute("id", true);
    executionLog.setAttribute("style", "white-space: pre;");
    executionLogContainer.appendChild(executionLog);
    body.appendChild(executionLogContainer);

    NodeList tableRows = document.getElementsByTagName("tr");
    Element theadRow = (Element) tableRows.item(0);
    test.name = theadRow.getTextContent();
    appendCellToRow(theadRow, "Result");

    this.commandProcessor =
        new HtmlCommandProcessor(this.host, this.port, this.browser, this.baseUrl);
    String resultState;
    String resultLog;
    test.result = true;
    try {
      this.commandProcessor.start();
      test.commands = new Command[tableRows.getLength() - 1];
      for (int i = 1; i < tableRows.getLength(); i++) {
        Element stepRow = (Element) tableRows.item(i);
        Command command = executeStep(stepRow);
        appendCellToRow(stepRow, command.result);
        test.commands[i - 1] = command;
        if (command.error) {
          test.result = false;
        }
        if (command.failure) {
          test.result = false;
          // break;
        }
      }
      resultState = test.result ? "PASSED" : "FAILED";
      resultLog = (test.result ? "Test Complete" : "Error");
      this.commandProcessor.stop();
    } catch (Exception e) {
      test.result = false;
      resultState = "ERROR";
      resultLog = "Failed to initialize session\n" + e;
      e.printStackTrace();
    }
    document.getElementById("result").setTextContent(resultState);
    Element log = document.getElementById("log");
    log.setTextContent(log.getTextContent() + resultLog + "\n");
    return test.result;
  }
コード例 #11
0
ファイル: ElementTest.java プロジェクト: haggisandchips/jsoup
 @Test(expected = IllegalArgumentException.class)
 public void testThrowsOnPrependNullText() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.prependText(null);
 }