View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link DomComment}.
24   *
25   * @author Karel Kolman
26   * @author Ahmed Ashour
27   * @author Philip Graf
28   */
29  @RunWith(BrowserRunner.class)
30  public class DomCommentTest extends SimpleWebTestCase {
31  
32      /**
33       * Test the comment not visible when viewed by user.
34       * @throws Exception if the test fails
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        * Test the comment correctness.
45        * @throws Exception if the test fails
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        * Test comment and character data sibling correctness.
59        * @throws Exception if the test fails
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       * @throws Exception if an error occurs
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       * Tests if {@code getCanonicalXPath()} returns the correct XPath for a
84       * comment node.
85       *
86       * @throws Exception if an error occurs
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       * Tests if {@code getCanonicalXPath()} returns the correct XPath for a
99       * comment node with other comment node siblings.
100      *
101      * @throws Exception if an error occurs
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 }