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.SimpleWebTestCase;
18  import org.htmlunit.html.HtmlPage;
19  import org.htmlunit.html.HtmlTextArea;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link HTMLTextAreaElement}.
24   *
25   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
26   * @author Marc Guillemot
27   * @author Ahmed Ashour
28   * @author Daniel Gredler
29   */
30  public class HTMLTextAreaElement2Test extends SimpleWebTestCase {
31  
32      /**
33       * Method type(...) should not trigger onchange!
34       * @throws Exception if the test fails
35       */
36      @Test
37      public void type_onchange() throws Exception {
38          final String content = DOCTYPE_HTML
39              + "<html><head><title>foo</title>\n"
40              + "<script>\n"
41              + "  function changed(e) {\n"
42              + "    log('changed: ' + e.value);\n"
43              + "  }\n"
44              + "  function keypressed(e) {\n"
45              + "    log('keypressed: ' + e.value);\n"
46              + "  }\n"
47              + "  function log(msg) {\n"
48              + "    document.getElementById('log').value += msg + '; ';\n"
49              + "  }\n"
50              + "</script></head>\n"
51              + "<body>\n"
52              + "<form id='form1'>\n"
53              + "<textarea id='textArea1' onchange='changed(this)' onkeypress='keypressed(this)'></textarea>\n"
54              + "<textarea id='log'></textarea>\n"
55              + "</form>\n"
56              + "</body></html>";
57          final HtmlPage page = loadPage(content);
58          final HtmlTextArea textArea = page.getHtmlElementById("textArea1");
59          textArea.type("hello");
60          page.setFocusedElement(null); // remove focus on textarea to trigger onchange
61  
62          final HtmlTextArea log = page.getHtmlElementById("log");
63          final String expectation = "keypressed: ; "
64              + "keypressed: h; "
65              + "keypressed: he; "
66              + "keypressed: hel; "
67              + "keypressed: hell; "
68              + "changed: hello;";
69          assertEquals(expectation, log.asNormalizedText());
70      }
71  
72  }