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