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.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.htmlunit.util.MimeType;
21  import org.junit.jupiter.api.Test;
22  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  
25  /**
26   * Unit tests for {@link HTMLBodyElement}.
27   *
28   * @author Daniel Gredler
29   * @author Ahmed Ashour
30   * @author Marc Guillemot
31   * @author Ronald Brill
32   * @author Frank Danek
33   */
34  public class HTMLBodyElementTest extends WebDriverTestCase {
35  
36      /**
37       * Tests the default body padding and margins.
38       * @throws Exception if an error occurs
39       */
40      @Test
41      @Alerts({"0px,0px,0px,0px,0px", ",,,,", "8px,8px,8px,8px,8px", ",,,,"})
42      @HtmlUnitNYI(CHROME = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
43              EDGE = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
44              FF = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
45              FF_ESR = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"})
46      public void defaultPaddingAndMargins() throws Exception {
47          final String html = DOCTYPE_HTML
48              + "<html>\n"
49              + "  <head>\n"
50              + "    <script>\n"
51              + LOG_TITLE_FUNCTION
52              + "      function test() {\n"
53              + "        var b = document.getElementById('body');\n"
54              + "        var s = b.currentStyle ? b.currentStyle : getComputedStyle(b, null);\n"
55              + "        log(s.padding + ',' + s.paddingLeft + ',' + s.paddingRight + ',' + s.paddingTop + ',' + s.paddingBottom);\n"
56              + "        log(b.style.padding + ',' + b.style.paddingLeft + ',' + b.style.paddingRight + ',' + b.style.paddingTop + ',' + b.style.paddingBottom);\n"
57              + "        log(s.margin + ',' + s.marginLeft + ',' + s.marginRight + ',' + s.marginTop + ',' + s.marginBottom);\n"
58              + "        log(b.style.margin + ',' + b.style.marginLeft + ',' + b.style.marginRight + ',' + b.style.marginTop + ',' + b.style.marginBottom);\n"
59              + "      }\n"
60              + "    </script>\n"
61              + "  </head>\n"
62              + "  <body id='body' onload='test()'>blah</body>\n"
63              + "</html>";
64  
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if an error occurs
70       */
71      @Test
72      @Alerts("TypeError")
73      public void attachEvent() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html>\n"
76              + "  <head>\n"
77              + "    <script>\n"
78              + LOG_TITLE_FUNCTION
79              + "      function handler() {\n"
80              + "        log(event);\n"
81              + "      }\n"
82              + "      function test() {\n"
83              + "        try {\n"
84              + "          document.body.attachEvent('onclick', handler);\n"
85              + "        } catch(e) { logEx(e); }\n"
86              + "      }\n"
87              + "    </script>\n"
88              + "  </head>\n"
89              + "  <body onload='test()'>\n"
90              + "    <input type='button' id='myInput' value='Test me'>\n"
91              + "  </body>\n"
92              + "</html>";
93  
94          final WebDriver driver = loadPageVerifyTitle2(html);
95  
96          driver.findElement(By.id("myInput")).click();
97      }
98  
99      /**
100      * @throws Exception if an error occurs
101      */
102     @Test
103     @Alerts("no")
104     public void doScroll() throws Exception {
105         final String html = DOCTYPE_HTML
106             + "<html>\n"
107             + "  <head>\n"
108             + "    <script>\n"
109             + LOG_TITLE_FUNCTION
110             + "      function test() {\n"
111             + "        if(document.body.doScroll) {\n"
112             + "          log('yes');\n"
113             + "          document.body.doScroll();\n"
114             + "          document.body.doScroll('down');\n"
115             + "        } else {\n"
116             + "          log('no');\n"
117             + "        }\n"
118             + "      }\n"
119             + "    </script>\n"
120             + "  </head>\n"
121             + "  <body onload='test()'>\n"
122             + "  </body>\n"
123             + "</html>";
124 
125         loadPageVerifyTitle2(html);
126     }
127 
128     /**
129      * @throws Exception if an error occurs
130      */
131     @Test
132     @Alerts({"", "#0000aa", "x"})
133     public void aLink() throws Exception {
134         final String html = DOCTYPE_HTML
135             + "<html>\n"
136             + "  <head>\n"
137             + "    <script>\n"
138             + LOG_TITLE_FUNCTION
139             + "      function test() {\n"
140             + "        var b = document.getElementById('body');\n"
141             + "        log(b.aLink);\n"
142             + "        b.aLink = '#0000aa';\n"
143             + "        log(b.aLink);\n"
144             + "        b.aLink = 'x';\n"
145             + "        log(b.aLink);\n"
146             + "      }\n"
147             + "    </script>\n"
148             + "  </head>\n"
149             + "  <body id='body' onload='test()'>blah</body>\n"
150             + "</html>";
151 
152         loadPageVerifyTitle2(html);
153     }
154 
155     /**
156      * @throws Exception if an error occurs
157      */
158     @Test
159     @Alerts({"", "http://www.foo.com/blah.gif", "blah.gif"})
160     public void background() throws Exception {
161         final String html = DOCTYPE_HTML
162             + "<html>\n"
163             + "  <head>\n"
164             + "    <script>\n"
165             + LOG_TITLE_FUNCTION
166             + "      function test() {\n"
167             + "        var b = document.getElementById('body');\n"
168             + "        log(b.background);\n"
169             + "        b.background = 'http://www.foo.com/blah.gif';\n"
170             + "        log(b.background);\n"
171             + "        b.background = 'blah.gif';\n"
172             + "        log(b.background);\n"
173             + "      }\n"
174             + "    </script>\n"
175             + "  </head>\n"
176             + "  <body id='body' onload='test()'>blah</body>\n"
177             + "</html>";
178         getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
179 
180         loadPageVerifyTitle2(html);
181     }
182 
183     /**
184      * @throws Exception if an error occurs
185      */
186     @Test
187     @Alerts({"", "#0000aa", "x"})
188     public void bgColor() throws Exception {
189         final String html = DOCTYPE_HTML
190             + "<html>\n"
191             + "  <head>\n"
192             + "    <script>\n"
193             + LOG_TITLE_FUNCTION
194             + "      function test() {\n"
195             + "        var b = document.getElementById('body');\n"
196             + "        log(b.bgColor);\n"
197             + "        b.bgColor = '#0000aa';\n"
198             + "        log(b.bgColor);\n"
199             + "        b.bgColor = 'x';\n"
200             + "        log(b.bgColor);\n"
201             + "      }\n"
202             + "    </script>\n"
203             + "  </head>\n"
204             + "  <body id='body' onload='test()'>blah</body>\n"
205             + "</html>";
206 
207         loadPageVerifyTitle2(html);
208     }
209 
210     /**
211      * @throws Exception if an error occurs
212      */
213     @Test
214     @Alerts({"", "#0000aa", "x"})
215     public void link() throws Exception {
216         final String html = DOCTYPE_HTML
217             + "<html>\n"
218             + "  <head>\n"
219             + "    <script>\n"
220             + LOG_TITLE_FUNCTION
221             + "      function test() {\n"
222             + "        var b = document.getElementById('body');\n"
223             + "        log(b.link);\n"
224             + "        b.link = '#0000aa';\n"
225             + "        log(b.link);\n"
226             + "        b.link = 'x';\n"
227             + "        log(b.link);\n"
228             + "      }\n"
229             + "    </script>\n"
230             + "  </head>\n"
231             + "  <body id='body' onload='test()'>blah</body>\n"
232             + "</html>";
233 
234         loadPageVerifyTitle2(html);
235     }
236 
237     /**
238      * @throws Exception if an error occurs
239      */
240     @Test
241     @Alerts({"", "#0000aa", "x"})
242     public void text() throws Exception {
243         final String html = DOCTYPE_HTML
244             + "<html>\n"
245             + "  <head>\n"
246             + "    <script>\n"
247             + LOG_TITLE_FUNCTION
248             + "      function test() {\n"
249             + "        var b = document.getElementById('body');\n"
250             + "        log(b.text);\n"
251             + "        b.text = '#0000aa';\n"
252             + "        log(b.text);\n"
253             + "        b.text = 'x';\n"
254             + "        log(b.text);\n"
255             + "      }\n"
256             + "    </script>\n"
257             + "  </head>\n"
258             + "  <body id='body' onload='test()'>blah</body>\n"
259             + "</html>";
260 
261         loadPageVerifyTitle2(html);
262     }
263 
264     /**
265      * @throws Exception if an error occurs
266      */
267     @Test
268     @Alerts({"", "#0000aa", "x"})
269     public void vLink() throws Exception {
270         final String html = DOCTYPE_HTML
271             + "<html>\n"
272             + "  <head>\n"
273             + "    <script>\n"
274             + LOG_TITLE_FUNCTION
275             + "      function test() {\n"
276             + "        var b = document.getElementById('body');\n"
277             + "        log(b.vLink);\n"
278             + "        b.vLink = '#0000aa';\n"
279             + "        log(b.vLink);\n"
280             + "        b.vLink = 'x';\n"
281             + "        log(b.vLink);\n"
282             + "      }\n"
283             + "    </script>\n"
284             + "  </head>\n"
285             + "  <body id='body' onload='test()'>blah</body>\n"
286             + "</html>";
287 
288         loadPageVerifyTitle2(html);
289     }
290 
291     /**
292      * @throws Exception if an error occurs
293      */
294     @Test
295     @Alerts({"function HTMLBodyElement() { [native code] }", ""
296                     + "ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, ENTITY_REFERENCE_NODE, "
297                     + "ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, "
298                     + "DOCUMENT_FRAGMENT_NODE, NOTATION_NODE, DOCUMENT_POSITION_DISCONNECTED, "
299                     + "DOCUMENT_POSITION_PRECEDING, "
300                     + "DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_CONTAINED_BY, "
301                     + "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, "})
302     public void enumeratedProperties() throws Exception {
303         final String html = DOCTYPE_HTML
304             + "<html><head>\n"
305             + "<script>\n"
306             + LOG_TITLE_FUNCTION
307             + "  function test() {\n"
308             + "    var str = '';\n"
309             + "    try {\n"
310             + "      log(HTMLBodyElement);\n"
311             + "      var str = '';\n"
312             + "      for (var i in HTMLBodyElement)\n"
313             + "        str += i + ', ';\n"
314             + "      log(str);\n"
315             + "    } catch(e) { logEx(e)}\n"
316             + "  }\n"
317             + "</script>\n"
318             + "</head>\n"
319             + "<body onload='test()'>\n"
320             + "</body></html>";
321 
322         loadPageVerifyTitle2(html);
323     }
324 
325     /**
326      * @throws Exception if an error occurs
327      */
328     @Test
329     @Alerts({"keydown (target)", "keydown (body)"})
330     public void eventHandler() throws Exception {
331         final String html = DOCTYPE_HTML
332             + "<html>\n"
333             + "  <head>\n"
334             + "    <script>\n"
335             + LOG_TITLE_FUNCTION
336             + "      function test() {\n"
337             + "        var target = document.getElementById('target');\n"
338             + "        target.onkeydown = function() {\n"
339             + "          log('keydown (target)');\n"
340             + "        };\n"
341             + "        document.body.onkeydown = function() {\n"
342             + "          log('keydown (body)');\n"
343             + "        };\n"
344             + "      }\n"
345             + "    </script>\n"
346             + "  </head>\n"
347             + "  <body onload='test()'>\n"
348             + "    <input id='target' type='checkbox'>\n"
349             + "  </body>\n"
350             + "</html>";
351 
352         final WebDriver driver = loadPage2(html);
353         driver.findElement(By.id("target")).sendKeys("a");
354 
355         verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts());
356     }
357 
358     /**
359      * @throws Exception if an error occurs
360      */
361     @Test
362     @Alerts({"0", "8"})
363     @HtmlUnitNYI(CHROME = {"0", "0"},
364             EDGE = {"0", "0"},
365             FF = {"0", "0"},
366             FF_ESR = {"0", "0"})
367     public void top() throws Exception {
368         final String html = DOCTYPE_HTML
369             + "<html>\n"
370             + "  <head>\n"
371             + "    <script>\n"
372             + LOG_TITLE_FUNCTION
373             + "      function test() {\n"
374             + "        var b = document.getElementById('body');\n"
375             + "        log(b.offsetTop);\n"
376             + "        log(b.getBoundingClientRect().top);\n"
377             + "      }\n"
378             + "    </script>\n"
379             + "  </head>\n"
380             + "  <body id='body' onload='test()'>blah</body>\n"
381             + "</html>";
382 
383         loadPageVerifyTitle2(html);
384     }
385 
386 }