1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class DomCommentTest extends SimpleWebTestCase {
31
32
33
34
35
36 @Test
37 public void asNormalizedText() throws Exception {
38 final String content = DOCTYPE_HTML + "<html><body><!-- a comment --></body></html>";
39 final HtmlPage page = loadPage(content);
40 assertEquals("", page.asNormalizedText());
41 }
42
43
44
45
46
47 @Test
48 public void asXml() throws Exception {
49 final String comment = "<!-- a comment -->";
50 final String content = DOCTYPE_HTML + "<html><body><span id='foo'>" + comment + "</span></body></html>";
51 final HtmlPage page = loadPage(content);
52 final HtmlElement elt = page.getHtmlElementById("foo");
53 final DomNode node = elt.getFirstChild();
54 assertEquals(comment, node.asXml());
55 }
56
57
58
59
60
61 @Test
62 public void textSibling() throws Exception {
63 final String content = DOCTYPE_HTML + "<html><body id='body'><!-- c1 -->text<!-- c2 --></body></html>";
64 final HtmlPage page = loadPage(content);
65 final DomNode node = page.getHtmlElementById("body").getFirstChild();
66 assertEquals(DomText.NODE_NAME, node.getNextSibling().getNodeName());
67 }
68
69
70
71
72 @Test
73 public void setTextContent() throws Exception {
74 final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--></span></body></html>";
75 final HtmlPage page = loadPage(html);
76 final DomComment comment = (DomComment) page.getElementById("s").getFirstChild();
77 assertEquals("abc", comment.getTextContent());
78 comment.setTextContent("xyz");
79 assertEquals("xyz", comment.getTextContent());
80 }
81
82
83
84
85
86
87
88 @Test
89 public void getCanonicalXPath_withoutCommentSiblings() throws Exception {
90 final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--></span></body></html>";
91 final HtmlPage page = loadPage(html);
92 final DomComment comment = (DomComment) page.getElementById("s").getFirstChild();
93 assertEquals("/html/body/span/comment()", comment.getCanonicalXPath());
94 assertEquals(comment, page.getFirstByXPath(comment.getCanonicalXPath()));
95 }
96
97
98
99
100
101
102
103 @Test
104 public void getCanonicalXPath_withCommentSiblings() throws Exception {
105 final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--><br/><!--def--></span></body></html>";
106 final HtmlPage page = loadPage(html);
107
108 final DomComment comment1 = (DomComment) page.getElementById("s").getFirstChild();
109 assertEquals("abc", comment1.getData());
110 assertEquals("/html/body/span/comment()[1]", comment1.getCanonicalXPath());
111 assertEquals(comment1, page.getFirstByXPath(comment1.getCanonicalXPath()));
112
113 final DomComment comment2 = (DomComment) page.getElementById("s").getChildNodes().get(2);
114 assertEquals("def", comment2.getData());
115 assertEquals("/html/body/span/comment()[2]", comment2.getCanonicalXPath());
116 assertEquals(comment2, page.getFirstByXPath(comment2.getCanonicalXPath()));
117 }
118
119 }