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