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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.WebElement;
25  import org.openqa.selenium.interactions.Actions;
26  
27  /**
28   * Tests for {@link HTMLTextAreaElement}.
29   *
30   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
31   * @author Marc Guillemot
32   * @author Ahmed Ashour
33   * @author Daniel Gredler
34   * @author Ronald Brill
35   * @author Frank Danek
36   * @author Carsten Steul
37   */
38  @RunWith(BrowserRunner.class)
39  public class HTMLTextAreaElementTest extends WebDriverTestCase {
40  
41      /**
42       * @throws Exception if the test fails
43       */
44      @Test
45      @Alerts({"1234", "PoohBear"})
46      public void getValue() throws Exception {
47          final String html = DOCTYPE_HTML
48              + "<html>\n"
49              + "<head>\n"
50              + "  <script>\n"
51              + LOG_TITLE_FUNCTION
52              + "    function doTest() {\n"
53              + "      log(document.form1.textarea1.value);\n"
54              + "      document.form1.textarea1.value = 'PoohBear';\n"
55              + "      log(document.form1.textarea1.value);\n"
56              + "    }\n"
57              + "  </script>\n"
58              + "</head>\n"
59              + "<body onload='doTest()'>\n"
60              + "  <p>hello world</p>\n"
61              + "  <form name='form1' method='post' >\n"
62              + "    <textarea name='textarea1' cols='45' rows='4'>1234</textarea>\n"
63              + "  </form>\n"
64              + "</body></html>";
65  
66          loadPageVerifyTitle2(html);
67      }
68  
69      /**
70       * @throws Exception if the test fails
71       */
72      @Test
73      @Alerts("foo")
74      public void onChange() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html>\n"
77              + "<head></head>\n"
78              + "<body>\n"
79              + "  <p>hello world</p>\n"
80              + "  <form name='form1'>\n"
81              + "    <textarea name='textarea1' onchange='alert(this.value)'></textarea>\n"
82              + "    <input name='myButton' type='button' onclick='document.form1.textarea1.value=\"from button\"'>\n"
83              + "  </form>\n"
84              + "</body></html>";
85  
86          final WebDriver driver = loadPage2(html);
87  
88          final WebElement textarea = driver.findElement(By.name("textarea1"));
89          textarea.sendKeys("foo");
90          driver.findElement(By.name("myButton")).click();
91  
92          verifyAlerts(driver, getExpectedAlerts());
93      }
94  
95      /**
96       * Tests that setValue doesn't has side effect. Test for bug 1155063.
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts({"TEXTAREA", "INPUT"})
101     public void setValue() throws Exception {
102         final String html = DOCTYPE_HTML
103             + "<html>\n"
104             + "<head></head>\n"
105             + "<body>\n"
106             + "  <form name='form1'>\n"
107             + "    <textarea name='question'></textarea>\n"
108             + "    <input type='button' name='btn_submit' value='Next'>\n"
109             + "  </form>\n"
110             + "  <script>\n"
111             + LOG_TITLE_FUNCTION
112             + "    document.form1.question.value = 'some text';\n"
113             + "    log(document.form1.elements[0].tagName);\n"
114             + "    log(document.form1.elements[1].tagName);\n"
115             + "  </script>\n"
116             + "</body>\n"
117             + "</html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 
122     /**
123      * @throws Exception if test fails
124      */
125     @Test
126     @Alerts({"11", "0"})
127     public void textLength() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html>\n"
130             + "<head></head>\n"
131             + "<body>\n"
132             + "  <textarea id='myTextArea'></textarea>\n"
133             + "  <script>\n"
134             + LOG_TITLE_FUNCTION
135             + "    var textarea = document.getElementById('myTextArea');\n"
136             + "    textarea.value = 'hello there';\n"
137             + "    log(textarea.textLength);\n"
138             + "    textarea.value = '';\n"
139             + "    log(textarea.textLength);\n"
140             + "  </script>\n"
141             + "</body>\n"
142             + "</html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 
147     /**
148      * @throws Exception if test fails
149      */
150     @Test
151     @Alerts({"0,0", "11,11", "3,11", "3,10", "7,7"})
152     public void selection() throws Exception {
153         selection(3, 10);
154     }
155 
156     /**
157      * @throws Exception if test fails
158      */
159     @Test
160     @Alerts({"0,0", "11,11", "11,11", "11,11", "7,7"})
161     public void selection_outOfBounds() throws Exception {
162         selection(-3, 15);
163     }
164 
165     /**
166      * @throws Exception if test fails
167      */
168     @Test
169     @Alerts({"0,0", "11,11", "10,11", "5,5", "7,7"})
170     public void selection_reverseOrder() throws Exception {
171         selection(10, 5);
172     }
173 
174     private void selection(final int selectionStart, final int selectionEnd) throws Exception {
175         final String html = DOCTYPE_HTML
176             + "<html>\n"
177             + "<head></head>\n"
178             + "<body>\n"
179             + "  <textarea id='myTextArea'></textarea>\n"
180             + "  <script>\n"
181             + LOG_TITLE_FUNCTION
182             + "    var textarea = document.getElementById('myTextArea');\n"
183             + "    log(textarea.selectionStart + ',' + textarea.selectionEnd);\n"
184             + "    textarea.value = 'Hello there';\n"
185             + "    log(textarea.selectionStart + ',' + textarea.selectionEnd);\n"
186             + "    textarea.selectionStart = " + selectionStart + ";\n"
187             + "    log(textarea.selectionStart + ',' + textarea.selectionEnd);\n"
188             + "    textarea.selectionEnd = " + selectionEnd + ";\n"
189             + "    log(textarea.selectionStart + ',' + textarea.selectionEnd);\n"
190             + "    textarea.value = 'nothing';\n"
191             + "    log(textarea.selectionStart + ',' + textarea.selectionEnd);\n"
192             + "  </script>\n"
193             + "</body>\n"
194             + "</html>";
195 
196         loadPageVerifyTitle2(html);
197     }
198 
199     /**
200      * @throws Exception if an error occurs
201      */
202     @Test
203     @Alerts("no")
204     public void doScroll() throws Exception {
205         final String html = DOCTYPE_HTML
206             + "<html>\n"
207             + "  <head>\n"
208             + "    <script>\n"
209             + LOG_TITLE_FUNCTION
210             + "      function test() {\n"
211             + "        var t = document.getElementById('t');\n"
212             + "        if(t.doScroll) {\n"
213             + "          log('yes');\n"
214             + "          t.doScroll();\n"
215             + "          t.doScroll('down');\n"
216             + "        } else {\n"
217             + "          log('no');\n"
218             + "        }\n"
219             + "      }\n"
220             + "    </script>\n"
221             + "  </head>\n"
222             + "  <body onload='test()'><textarea id='t'>abc</textarea></body>\n"
223             + "</html>";
224 
225         loadPageVerifyTitle2(html);
226     }
227 
228     /**
229      * Test that the new line immediately following opening tag is ignored.
230      * @throws Exception if the test fails
231      */
232     @Test
233     @Alerts("Hello\\nworld\\n")
234     public void value_ignoreFirstNewLine() throws Exception {
235         value("\nHello\nworld\n");
236     }
237 
238     /**
239      * @throws Exception if the test fails
240      */
241     @Test
242     @Alerts("\\s\\nHello\\nworld\\n")
243     public void value_spaceBeforeFirstNewLine() throws Exception {
244         value(" \nHello\nworld\n");
245     }
246 
247     private void value(final String textAreaBody) throws Exception {
248         final String html = DOCTYPE_HTML
249             + "<html>\n"
250             + "<head>\n"
251             + "  <script>\n"
252             + LOG_TITLE_FUNCTION_NORMALIZE
253             + "    function doTest() {\n"
254             + "      log(document.form1.textarea1.value);\n"
255             + "    }\n"
256             + "  </script>\n"
257             + "</head>\n"
258             + "<body onload='doTest()'>\n"
259             + "  <form name='form1' method='post' >\n"
260             + "    <textarea name='textarea1'>" + textAreaBody + "</textarea>\n"
261             + "    </textarea>\n"
262             + "  </form>\n"
263             + "</body></html>";
264 
265         loadPageVerifyTitle2(html);
266     }
267 
268     /**
269      * @throws Exception if an error occurs
270      */
271     @Test
272     @Alerts({"\\sfoo\\s\\n\\sbar\\s", "\\sfoo\\s\\n\\sbar\\s"})
273     public void defaultValue() throws Exception {
274         final String html = DOCTYPE_HTML
275             + "<html>\n"
276             + "<head>\n"
277             + "  <script>\n"
278             + LOG_TITLE_FUNCTION_NORMALIZE
279             + "    function test() {\n"
280             + "      var t = document.getElementById('textArea');\n"
281             + "      log(t.defaultValue);\n"
282             + "      log(t.value);\n"
283             + "    }\n"
284             + "  </script>\n"
285             + "</head>\n"
286             + "<body onload='test()'>\n"
287             + "  <form id='form1'>\n"
288             + "    <textarea id='textArea'>\n foo \n bar </textarea>\n"
289             + "  </form>\n"
290             + "</body></html>";
291 
292         loadPageVerifyTitle2(html);
293     }
294 
295     /**
296      * @throws Exception if an error occurs
297      */
298     @Test
299     @Alerts({"true", "false"})
300     public void readOnly() throws Exception {
301         final String html = DOCTYPE_HTML
302             + "<html>\n"
303             + "<head>\n"
304             + "  <script>\n"
305             + LOG_TITLE_FUNCTION
306             + "  function test() {\n"
307             + "    var t = document.getElementById('textArea');\n"
308             + "    log(t.readOnly);\n"
309             + "    t.readOnly = false;\n"
310             + "    log(t.readOnly);\n"
311             + "  }\n"
312             + "</script>\n"
313             + "</head>\n"
314             + "<body onload='test()'>\n"
315             + "  <form id='form1'>\n"
316             + "    <textarea id='textArea' readonly>\n foo \n bar </textarea>\n"
317             + "  </form>\n"
318             + "</body></html>";
319 
320         loadPageVerifyTitle2(html);
321     }
322 
323     /**
324      * @throws Exception if an error occurs
325      */
326     @Test
327     @Alerts({"", "A", "a", "A", "a8", "8Afoo", "8", "@"})
328     public void accessKey() throws Exception {
329         final String html = DOCTYPE_HTML
330             + "<html>\n"
331             + "<head></head>\n"
332             + "<body>\n"
333             + "  <textarea id='a1'>a1</textarea>\n"
334             + "  <textarea id='a2' accesskey='A'>a2</textarea>\n"
335 
336             + "  <script>\n"
337             + LOG_TITLE_FUNCTION
338             + "    var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
339             + "    log(a1.accessKey);\n"
340             + "    log(a2.accessKey);\n"
341 
342             + "    a1.accessKey = 'a';\n"
343             + "    log(a1.accessKey);\n"
344 
345             + "    a1.accessKey = 'A';\n"
346             + "    log(a1.accessKey);\n"
347 
348             + "    a1.accessKey = 'a8';\n"
349             + "    log(a1.accessKey);\n"
350 
351             + "    a1.accessKey = '8Afoo';\n"
352             + "    log(a1.accessKey);\n"
353 
354             + "    a1.accessKey = '8';\n"
355             + "    log(a1.accessKey);\n"
356 
357             + "    a1.accessKey = '@';\n"
358             + "    log(a1.accessKey);\n"
359             + "  </script>\n"
360             + "</body></html>";
361 
362         loadPageVerifyTitle2(html);
363     }
364 
365     /**
366      * @throws Exception if an error occurs
367      */
368     @Test
369     @Alerts({"20", "5", "8", "4", "20", "20", "20", "3"})
370     public void cols() throws Exception {
371         final String html = DOCTYPE_HTML
372             + "<html><head>\n"
373             + "<script>\n"
374             + LOG_TITLE_FUNCTION
375             + "  function setCols(e, value) {\n"
376             + "    try {\n"
377             + "      e.cols = value;\n"
378             + "    } catch(e) { logEx(e); }\n"
379             + "  }\n"
380             + "</script>\n"
381             + "</head>\n"
382 
383             + "<body>\n"
384             + "  <textarea id='a1'>a1</textarea>\n"
385             + "  <textarea id='a2' cols='5'>a2</textarea>\n"
386 
387             + "  <script>\n"
388             + "    var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
389             + "    log(a1.cols);\n"
390             + "    log(a2.cols);\n"
391 
392             + "    setCols(a1, '8');\n"
393             + "    log(a1.cols);\n"
394 
395             + "    setCols(a1, 4);\n"
396             + "    log(a1.cols);\n"
397 
398             + "    setCols(a1, 'a');\n"
399             + "    log(a1.cols);\n"
400 
401             + "    setCols(a1, '');\n"
402             + "    log(a1.cols);\n"
403 
404             + "    setCols(a1, -1);\n"
405             + "    log(a1.cols);\n"
406 
407             + "    setCols(a1, 3.4);\n"
408             + "    log(a1.cols);\n"
409             + "</script></body></html>";
410 
411         loadPageVerifyTitle2(html);
412     }
413 
414     /**
415      * @throws Exception if an error occurs
416      */
417     @Test
418     @Alerts({"2", "5", "8", "4", "2", "2", "2", "3"})
419     public void rows() throws Exception {
420         final String html = DOCTYPE_HTML
421             + "<html><head>\n"
422             + "<script>\n"
423             + LOG_TITLE_FUNCTION
424             + "  function setRows(e, value) {\n"
425             + "    try {\n"
426             + "      e.rows = value;\n"
427             + "    } catch(e) { logEx(e); }\n"
428             + "  }\n"
429             + "</script>\n"
430             + "</head>\n"
431 
432             + "<body>\n"
433             + "  <textarea id='a1'>a1</textarea>\n"
434             + "  <textarea id='a2' rows='5'>a2</textarea>\n"
435 
436             + "  <script>\n"
437             + "    var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
438             + "    log(a1.rows);\n"
439             + "    log(a2.rows);\n"
440 
441             + "    setRows(a1, '8');\n"
442             + "    log(a1.rows);\n"
443 
444             + "    setRows(a1, 4);\n"
445             + "    log(a1.rows);\n"
446 
447             + "    setRows(a1, 'a');\n"
448             + "    log(a1.rows);\n"
449 
450             + "    setRows(a1, '');\n"
451             + "    log(a1.rows);\n"
452 
453             + "    setRows(a1, -1);\n"
454             + "    log(a1.rows);\n"
455 
456             + "    setRows(a1, 3.4);\n"
457             + "    log(a1.rows);\n"
458             + "  </script>\n"
459             + "</body></html>";
460 
461         loadPageVerifyTitle2(html);
462     }
463 
464     /**
465      * @throws Exception if the test fails
466      */
467     @Test
468     @Alerts({"9", "9", "2", "7"})
469     public void selectionRange() throws Exception {
470         final String html = DOCTYPE_HTML
471             + "<html>\n"
472             + "<head>\n"
473             + "  <script>\n"
474             + LOG_TITLE_FUNCTION
475             + "    function test() {\n"
476             + "      var ta = document.getElementById('myInput');\n"
477             + "      ta.setSelectionRange(15, 15);\n"
478             + "      log(ta.selectionStart);\n"
479             + "      log(ta.selectionEnd);\n"
480             + "      ta.setSelectionRange(2, 7);\n"
481             + "      log(ta.selectionStart);\n"
482             + "      log(ta.selectionEnd);\n"
483             + "    }\n"
484             + "  </script>\n"
485             + "</head>\n"
486             + "<body onload='test()'>\n"
487             + "  <textarea id='myInput'>some test</textarea>\n"
488             + "</body></html>";
489 
490         loadPageVerifyTitle2(html);
491     }
492 
493     /**
494      * @throws Exception if an error occurs
495      */
496     @Test
497     @Alerts({"test", "4", "42", "2", "[object HTMLTextAreaElement]", "28"})
498     public void getAttributeAndSetValue() throws Exception {
499         final String html = DOCTYPE_HTML
500             + "<html>\n"
501             + "  <head>\n"
502             + "    <script>\n"
503             + LOG_TITLE_FUNCTION
504             + "      function test() {\n"
505             + "        var t = document.getElementById('t');\n"
506             + "        t.value = 'test';\n"
507             + "        log(t.value);\n"
508             + "        if (t.value != null)\n"
509             + "          log(t.value.length);\n"
510 
511             + "        t.value = 42;\n"
512             + "        log(t.value);\n"
513             + "        if (t.value != null)\n"
514             + "          log(t.value.length);\n"
515 
516             + "        t.value = document.getElementById('t');\n"
517             + "        log(t.value);\n"
518             + "        if (t.value != null)\n"
519             + "          log(t.value.length);\n"
520             + "      }\n"
521             + "    </script>\n"
522             + "  </head>\n"
523             + "  <body onload='test()'>\n"
524             + "    <textarea id='t'>abc</textarea>\n"
525             + "  </body>\n"
526             + "</html>";
527 
528         loadPageVerifyTitle2(html);
529     }
530 
531     /**
532      * @throws Exception if an error occurs
533      */
534     @Test
535     @Alerts({"null", "4", "", "0"})
536     public void getAttributeAndSetValueNull() throws Exception {
537         final String html = DOCTYPE_HTML
538             + "<html>\n"
539             + "  <head>\n"
540             + "    <script>\n"
541             + LOG_TITLE_FUNCTION
542             + "      function test() {\n"
543             + "        var t = document.getElementById('t');\n"
544             + "        t.value = 'null';\n"
545             + "        log(t.value);\n"
546             + "        if (t.value != null)\n"
547             + "          log(t.value.length);\n"
548 
549             + "        t.value = null;\n"
550             + "        log(t.value);\n"
551             + "        if (t.value != null)\n"
552             + "          log(t.value.length);\n"
553             + "      }\n"
554             + "    </script>\n"
555             + "  </head>\n"
556             + "  <body onload='test()'>\n"
557             + "    <textarea id='t'>abc</textarea>\n"
558             + "  </body>\n"
559             + "</html>";
560 
561         loadPageVerifyTitle2(html);
562     }
563 
564     /**
565      * @throws Exception if an error occurs
566      */
567     @Test
568     @Alerts({"12", "2", "[object HTMLTextAreaElement]", "28"})
569     public void getAttributeAndSetValueOther() throws Exception {
570         final String html = DOCTYPE_HTML
571             + "<html>\n"
572             + "  <head>\n"
573             + "    <script>\n"
574             + LOG_TITLE_FUNCTION
575             + "      function test() {\n"
576             + "        var t = document.getElementById('t');\n"
577             + "        t.value = 12;\n"
578             + "        log(t.value);\n"
579             + "        if (t.value != null)\n"
580             + "          log(t.value.length);\n"
581 
582             + "        t.value = t;\n"
583             + "        log(t.value);\n"
584             + "        if (t.value != null)\n"
585             + "          log(t.value.length);\n"
586             + "      }\n"
587             + "    </script>\n"
588             + "  </head>\n"
589             + "  <body onload='test()'>\n"
590             + "    <textarea id='t'>abc</textarea>\n"
591             + "  </body>\n"
592             + "</html>";
593 
594         loadPageVerifyTitle2(html);
595     }
596 
597     /**
598      * @throws Exception if the test fails
599      */
600     @Test
601     @Alerts({"-1", "null", "32", "32", "-1", "ms"})
602     public void maxLength() throws Exception {
603         final String html = DOCTYPE_HTML
604             + "<html>\n"
605             + "<head>\n"
606             + "  <script>\n"
607             + LOG_TITLE_FUNCTION
608             + "    function test() {\n"
609             + "      log(document.form1.textarea1.maxLength);\n"
610             + "      log(document.form1.textarea1.getAttribute('maxLength'));\n"
611             + "      log(document.form1.textarea2.maxLength);\n"
612             + "      log(document.form1.textarea2.getAttribute('maxLength'));\n"
613             + "      log(document.form1.textarea3.maxLength);\n"
614             + "      log(document.form1.textarea3.getAttribute('maxLength'));\n"
615             + "    }\n"
616             + "  </script>\n"
617             + "</head>\n"
618             + "<body onload='test()'>\n"
619             + "  <form name='form1' method='post'>\n"
620             + "    <textarea name='textarea1'></textarea>\n"
621             + "    <textarea name='textarea2' maxLength='32'></textarea>\n"
622             + "    <textarea name='textarea3' maxLength='ms'></textarea>\n"
623             + "  </form>\n"
624             + "</body></html>";
625 
626         loadPageVerifyTitle2(html);
627     }
628 
629     /**
630      * @throws Exception if the test fails
631      */
632     @Test
633     @Alerts({"-1", "null", "32", "32", "-1", "ms"})
634     public void minLength() throws Exception {
635         final String html = DOCTYPE_HTML
636             + "<html>\n"
637             + "<head>\n"
638             + "  <script>\n"
639             + LOG_TITLE_FUNCTION
640             + "    function test() {\n"
641             + "      log(document.form1.textarea1.minLength);\n"
642             + "      log(document.form1.textarea1.getAttribute('minLength'));\n"
643             + "      log(document.form1.textarea2.minLength);\n"
644             + "      log(document.form1.textarea2.getAttribute('minLength'));\n"
645             + "      log(document.form1.textarea3.minLength);\n"
646             + "      log(document.form1.textarea3.getAttribute('minLength'));\n"
647             + "    }\n"
648             + "  </script>\n"
649             + "</head>\n"
650             + "<body onload='test()'>\n"
651             + "  <form name='form1' method='post'>\n"
652             + "    <textarea name='textarea1'></textarea>\n"
653             + "    <textarea name='textarea2' minLength='32'></textarea>\n"
654             + "    <textarea name='textarea3' minLength='ms'></textarea>\n"
655             + "  </form>\n"
656             + "</body></html>";
657 
658         loadPageVerifyTitle2(html);
659     }
660 
661     /**
662      * @throws Exception if the test fails
663      */
664     @Test
665     @Alerts({"10", "10", "IndexSizeError/DOMException", "10", "10", "0", "0"})
666     public void setMaxLength() throws Exception {
667         final String html = DOCTYPE_HTML
668             + "<html>\n"
669             + "<head>\n"
670             + "  <script>\n"
671             + LOG_TITLE_FUNCTION
672             + "    function setMaxLength(length){\n"
673             + "      try {\n"
674             + "        document.form1.textarea1.maxLength = length;\n"
675             + "      } catch(e) { logEx(e); }\n"
676             + "    }\n"
677             + "  </script>\n"
678             + "</head>\n"
679             + "<body>\n"
680             + "  <form name='form1' method='post' >\n"
681             + "    <textarea id='textarea1'></textarea>\n"
682             + "    <script>\n"
683             + "      var a = document.getElementById('textarea1');\n"
684 
685             + "      setMaxLength(10);\n"
686             + "      log(a.maxLength);\n"
687             + "      log(a.getAttribute('maxLength'));\n"
688 
689             + "      setMaxLength(-1);\n"
690             + "      log(a.maxLength);\n"
691             + "      log(a.getAttribute('maxLength'));\n"
692 
693             + "      setMaxLength('abc');\n"
694             + "      log(a.maxLength);\n"
695             + "      log(a.getAttribute('maxLength'));\n"
696 
697             + "    </script>\n"
698             + "  </form>\n"
699             + "</body></html>";
700 
701         loadPageVerifyTitle2(html);
702     }
703 
704     /**
705      * @throws Exception if the test fails
706      */
707     @Test
708     @Alerts("[object HTMLFormElement]")
709     public void form() throws Exception {
710         final String html = DOCTYPE_HTML
711             + "<html>\n"
712             + "<body>\n"
713             + "  <form>\n"
714             + "    <textarea id='a'></textarea>\n"
715             + "  </form>"
716             + "  <script>\n"
717             + LOG_TITLE_FUNCTION
718             + "    log(document.getElementById('a').form);\n"
719             + "  </script>"
720             + "</body>"
721             + "</html>";
722 
723         loadPageVerifyTitle2(html);
724     }
725 
726     /**
727      * @throws Exception if an error occurs
728      */
729     @Test
730     @Alerts("mouse over [tester]")
731     public void mouseOverTextarea() throws Exception {
732         shutDownAll();
733         mouseOver("<textarea id='tester' onmouseover='dumpEvent(event);'>HtmlUnit</textarea>");
734     }
735 
736     /**
737      * @throws Exception if an error occurs
738      */
739     @Test
740     @Alerts("mouse over [tester]")
741     public void mouseOverTextareaDisabled() throws Exception {
742         shutDownAll();
743         mouseOver("<textarea id='tester' onmouseover='dumpEvent(event);' disabled >HtmlUnit</textarea>");
744     }
745 
746     private void mouseOver(final String element) throws Exception {
747         final String html = DOCTYPE_HTML
748             + "<html>\n"
749             + "  <head>\n"
750             + "    <script>\n"
751             + "    function dumpEvent(event) {\n"
752             + "      // target\n"
753             + "      var eTarget;\n"
754             + "      if (event.target) {\n"
755             + "        eTarget = event.target;\n"
756             + "      } else if (event.srcElement) {\n"
757             + "        eTarget = event.srcElement;\n"
758             + "      }\n"
759             + "      // defeat Safari bug\n"
760             + "      if (eTarget.nodeType == 3) {\n"
761             + "        eTarget = eTarget.parentNode;\n"
762             + "      }\n"
763             + "      var msg = 'mouse over';\n"
764             + "      if (eTarget.name) {\n"
765             + "        msg = msg + ' [' + eTarget.name + ']';\n"
766             + "      } else {\n"
767             + "        msg = msg + ' [' + eTarget.id + ']';\n"
768             + "      }\n"
769             + "      document.title += msg;\n"
770             + "    }\n"
771             + "    </script>\n"
772             + "  </head>\n"
773             + "<body>\n"
774             + "  <form id='form1'>\n"
775             + "    " + element + "\n"
776             + "  </form>\n"
777             + "</body></html>";
778 
779         final WebDriver driver = loadPage2(html);
780 
781         final Actions actions = new Actions(driver);
782         actions.moveToElement(driver.findElement(By.id("tester")));
783         actions.perform();
784 
785         assertTitle(driver, getExpectedAlerts()[0]);
786     }
787 
788     /**
789      * @throws Exception if the test fails
790      */
791     @Test
792     @Alerts({"HtmlUnit", "</> htmx rocks!"})
793     public void innerHtml() throws Exception {
794         final String html = DOCTYPE_HTML
795             + "<html>\n"
796             + "  <head>\n"
797             + "    <title>Page Title</title>\n"
798             + "    <script>\n"
799             + LOG_TEXTAREA_FUNCTION
800             + "      function test() {\n"
801             + "        var textarea = document.getElementsByTagName('textarea')[0];\n"
802             + "        log(textarea.value);\n"
803             + "        textarea.innerHTML = '</> htmx rocks!';\n"
804             + "        log(textarea.value);\n"
805             + "      }\n"
806             + "    </script>\n"
807             + "  </head>\n"
808             + "  <body onload='test()'>"
809             + "    <textarea>HtmlUnit</textarea>\n"
810             + LOG_TEXTAREA
811             + "  </body>\n"
812             + "</html>";
813 
814         loadPageVerifyTextArea2(html);
815     }
816 
817     /**
818      * @throws Exception if the test fails
819      */
820     @Test
821     @Alerts({"HtmlUnit", "<div>htmx rocks</div>"})
822     public void innerHtmlTag() throws Exception {
823         final String html = DOCTYPE_HTML
824             + "<html>\n"
825             + "  <head>\n"
826             + "    <title>Page Title</title>\n"
827             + "    <script>\n"
828             + LOG_TEXTAREA_FUNCTION
829             + "      function test() {\n"
830             + "        var textarea = document.getElementsByTagName('textarea')[0];\n"
831             + "        log(textarea.value);\n"
832             + "        textarea.innerHTML = '<div>htmx rocks</div>';\n"
833             + "        log(textarea.value);\n"
834             + "      }\n"
835             + "    </script>\n"
836             + "  </head>\n"
837             + "  <body onload='test()'>\n"
838             + "    <textarea>HtmlUnit</textarea>\n"
839             + LOG_TEXTAREA
840             + "  </body>\n"
841             + "</html>";
842 
843         loadPageVerifyTextArea2(html);
844     }
845 
846     /**
847      * @throws Exception if the test fails
848      */
849     @Test
850     @Alerts({"", "</> htmx rocks!"})
851     public void innerHtmlEscaping() throws Exception {
852         final String html = DOCTYPE_HTML
853             + "<html>\n"
854             + "  <head>\n"
855             + "    <title>Page Title</title>\n"
856             + "    <script>\n"
857             + LOG_TEXTAREA_FUNCTION
858             + "      function test() {\n"
859             + "        var textarea = document.getElementsByTagName('textarea')[0];\n"
860             + "        log(textarea.value);\n"
861             + "        textarea.innerHTML = '&lt;/> htmx rocks!';\n"
862             + "        log(textarea.value);\n"
863             + "      }\n"
864             + "    </script>\n"
865             + "  </head>\n"
866             + "  <body onload='test()'>\n"
867             + "    <textarea></textarea>\n"
868             + LOG_TEXTAREA
869             + "  </body>\n"
870             + "</html>";
871 
872         loadPageVerifyTextArea2(html);
873     }
874 
875     /**
876      * @throws Exception if an error occurs
877      */
878     @Test
879     @Alerts({"true", "false", "true", "false", "true"})
880     public void willValidate() throws Exception {
881         final String html = DOCTYPE_HTML
882                 + "<html><head>\n"
883                 + "  <script>\n"
884                 + LOG_TITLE_FUNCTION
885                 + "    function test() {\n"
886                 + "      log(document.getElementById('i1').willValidate);\n"
887                 + "      log(document.getElementById('i2').willValidate);\n"
888                 + "      log(document.getElementById('i3').willValidate);\n"
889                 + "      log(document.getElementById('i4').willValidate);\n"
890                 + "      log(document.getElementById('i5').willValidate);\n"
891                 + "    }\n"
892                 + "  </script>\n"
893                 + "</head>\n"
894                 + "<body onload='test()'>\n"
895                 + "  <form>\n"
896                 + "    <textarea id='i1'>button</textarea>"
897                 + "    <textarea id='i2' disabled></textarea>"
898                 + "    <textarea id='i3' hidden></textarea>"
899                 + "    <textarea id='i4' readonly></textarea>"
900                 + "    <textarea id='i5' style='display: none'></textarea>"
901                 + "  </form>\n"
902                 + "</body></html>";
903 
904         loadPageVerifyTitle2(html);
905     }
906 }