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.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link Comment}.
23   *
24   * @author Mirko Friedenhagen
25   * @author Ahmed Ashour
26   * @author Frank Danek
27   * @author Ronald Brill
28   */
29  public class CommentTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts("[object Comment]")
36      public void simpleScriptable() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><head>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41              + "function test() {\n"
42              + "  log(document.body.firstChild);\n"
43              + "}\n"
44              + "</script></head><body onload='test()'><!-- comment --></body></html>";
45  
46          loadPageVerifyTitle2(html);
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      @Alerts({"after", "comment"})
54      public void textContent() throws Exception {
55          final String html = DOCTYPE_HTML
56              + "<html><body>\n"
57              + "<div id='it'><!--comment-->after</div>\n"
58              + "<script>\n"
59              + LOG_TITLE_FUNCTION
60              + "var node = document.getElementById('it');\n"
61              + "log(node.textContent);\n"
62              + "log(node.firstChild.textContent);\n"
63              + "</script></body></html>";
64  
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"after", "undefined"})
73      public void innerText() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html><body>\n"
76              + "<div id='it'><!--comment-->after</div>\n"
77              + "<script>\n"
78              + LOG_TITLE_FUNCTION
79              + "var node = document.getElementById('it');\n"
80              + "log(node.innerText);\n"
81              + "log(node.firstChild.innerText);\n"
82              + "</script></body></html>";
83          loadPageVerifyTitle2(html);
84      }
85  
86      private void property(final String property) throws Exception {
87          final String html = DOCTYPE_HTML
88              + "<html><body>\n"
89              + "<div id='it'><!--abcdefg-->after</div>\n"
90              + "<script>\n"
91              + LOG_TITLE_FUNCTION
92              + "var node = document.getElementById('it');\n"
93              + "log(node.firstChild." + property + ");\n"
94              + "</script></body></html>";
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * @throws Exception if the test fails
100      */
101     @Test
102     @Alerts("undefined")
103     public void id() throws Exception {
104         property("id");
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts("undefined")
112     public void className() throws Exception {
113         property("className");
114     }
115 
116     /**
117      * @throws Exception if the test fails
118      */
119     @Test
120     @Alerts("undefined")
121     public void tagName() throws Exception {
122         property("tagName");
123     }
124 
125     /**
126      * @throws Exception if the test fails
127      */
128     @Test
129     @Alerts("undefined")
130     public void text() throws Exception {
131         property("text");
132     }
133 
134     /**
135      * @throws Exception if the test fails
136      */
137     @Test
138     @Alerts("undefined")
139     public void document() throws Exception {
140         property("document");
141     }
142 
143 }