View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.html;
16  
17  import org.htmlunit.HttpMethod;
18  import org.htmlunit.MockWebConnection;
19  import org.htmlunit.Page;
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.javascript.host.event.KeyboardEvent;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for {@link HtmlTextArea}.
27   *
28   * @author Mike Bowler
29   * @author Marc Guillemot
30   * @author Ahmed Ashour
31   * @author Sudhan Moghe
32   * @author Amit Khanna
33   * @author Ronald Brill
34   */
35  public class HtmlTextAreaTest extends SimpleWebTestCase {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void formSubmission() throws Exception {
42          formSubmission("foo", "foo");
43          formSubmission("\r\nfoo\r\n", "foo%0D%0A");
44          formSubmission("\nfoo\n", "foo%0D%0A");
45          formSubmission("\r\n\r\nfoo\r\n", "%0D%0Afoo%0D%0A");
46      }
47  
48      private void formSubmission(final String textAreaText, final String expectedValue) throws Exception {
49          final String content = DOCTYPE_HTML
50              + "<html><head><title>foo</title></head><body>\n"
51              + "<form id='form1'>\n"
52              + "<textarea name='textArea1'>" + textAreaText + "</textarea>\n"
53              + "<input type='submit' id='mysubmit'/>\n"
54              + "</form></body></html>";
55          final HtmlPage page = loadPage(content);
56          final MockWebConnection webConnection = getMockConnection(page);
57          final HtmlForm form = page.getHtmlElementById("form1");
58  
59          final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
60          assertNotNull(textArea);
61  
62          final Page secondPage = page.getHtmlElementById("mysubmit").click();
63  
64          assertEquals("url", URL_FIRST + "?textArea1=" + expectedValue, secondPage.getUrl());
65          assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      public void formSubmission_NewValue() throws Exception {
73          final String content = DOCTYPE_HTML
74              + "<html><head><title>foo</title></head><body>\n"
75              + "<form id='form1'>\n"
76              + "<textarea name='textArea1'>foo</textarea>\n"
77              + "<input type='submit' id='mysubmit'/>\n"
78              + "</form></body></html>";
79          final HtmlPage page = loadPage(content);
80          final MockWebConnection webConnection = getMockConnection(page);
81          final HtmlForm form = page.getHtmlElementById("form1");
82  
83          final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
84          textArea.setText("Flintstone");
85          final Page secondPage = page.getHtmlElementById("mysubmit").click();
86  
87          assertEquals("url", URL_FIRST + "?textArea1=Flintstone", secondPage.getUrl());
88          assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      public void getText() throws Exception {
96          final String content = DOCTYPE_HTML
97              + "<html><head><title>foo</title></head><body>\n"
98              + "<form id='form1'>\n"
99              + "<textarea name='textArea1'> foo \n bar </textarea>\n"
100             + "</form></body></html>";
101         final HtmlPage page = loadPage(content);
102         final HtmlForm form = page.getHtmlElementById("form1");
103 
104         final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
105         assertNotNull(textArea);
106         assertEquals("White space must be preserved!", " foo \n bar ", textArea.getText());
107         assertEquals(" foo \n bar ", textArea.getDefaultValue());
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts(" foo \n bar\n test\n a <p>html snippet</p>\n")
115     public void asNormalizedText() throws Exception {
116         final String html = DOCTYPE_HTML
117             + "<html><head>\n"
118             + "</head>\n"
119             + "<body>\n"
120             + "<textarea id='tester'> foo \n bar\r\n test\r a "
121             + "<p>html snippet</p>\n"
122             + "</textarea>\n"
123             + "</body></html>";
124 
125         final HtmlPage page = loadPage(html);
126         final HtmlTextArea textArea = page.getHtmlElementById("tester");
127 
128         assertEquals(getExpectedAlerts()[0], textArea.getText());
129 
130         assertEquals(getExpectedAlerts()[0], textArea.asNormalizedText());
131         assertEquals(getExpectedAlerts()[0], page.asNormalizedText());
132     }
133 
134     /**
135      * @throws Exception if the test fails
136      */
137     @Test
138     @Alerts("HtmlUnit")
139     public void asNormalizedTextTypedText() throws Exception {
140         final String html = DOCTYPE_HTML
141             + "<html><head>\n"
142             + "</head>\n"
143             + "<body>\n"
144             + "<textarea id='tester'> foo \n bar\r\n test\r a "
145             + "<p>html snippet</p>\n"
146             + "</textarea>\n"
147             + "</body></html>";
148 
149         final HtmlPage page = loadPage(html);
150         final HtmlTextArea textArea = page.getHtmlElementById("tester");
151         textArea.select();
152         textArea.type("HtmlUnit");
153 
154         assertEquals(getExpectedAlerts()[0], textArea.getText());
155 
156         assertEquals(getExpectedAlerts()[0], textArea.asNormalizedText());
157         assertEquals(getExpectedAlerts()[0], page.asNormalizedText());
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     public void asXml() throws Exception {
165         final String content = DOCTYPE_HTML
166             + "<html><head><title>foo</title></head><body>\n"
167             + "<form id='form1'>\n"
168             + "<textarea name='textArea1'> foo \n bar </textarea>\n"
169             + "<textarea name='textArea2'></textarea>\n"
170             + "<textarea name='textArea3'>1 &lt; 2 &amp; 3 &gt; 2</textarea>\n"
171             + "</form></body></html>";
172         final HtmlPage page = loadPage(content);
173         final HtmlForm form = page.getHtmlElementById("form1");
174 
175         final HtmlTextArea textArea1 = form.getTextAreaByName("textArea1");
176         final String expected1 = "<textarea name=\"textArea1\"> foo \n bar </textarea>";
177         assertEquals(expected1, textArea1.asXml());
178         assertTrue(form.asXml(), form.asXml().contains(expected1));
179 
180         final HtmlTextArea textArea2 = form.getTextAreaByName("textArea2");
181         assertEquals("<textarea name=\"textArea2\"></textarea>", textArea2.asXml());
182 
183         final HtmlTextArea textArea3 = form.getTextAreaByName("textArea3");
184         assertEquals("<textarea name=\"textArea3\">1 &lt; 2 &amp; 3 &gt; 2</textarea>", textArea3.asXml());
185     }
186 
187     /**
188      * @throws Exception if an error occurs
189      */
190     @Test
191     public void preventDefault() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html><head><script>\n"
194             + "  function handler(e) {\n"
195             + "    if (e && e.target.value.length > 2)\n"
196             + "      e.preventDefault();\n"
197             + "    else if (!e && window.event.srcElement.value.length > 2)\n"
198             + "      return false;\n"
199             + "  }\n"
200             + "  function init() {\n"
201             + "    document.getElementById('text1').onkeydown = handler;\n"
202             + "  }\n"
203             + "</script></head>\n"
204             + "<body onload='init()'>\n"
205             + "<textarea id='text1'></textarea>\n"
206             + "</body></html>";
207 
208         final HtmlPage page = loadPage(html);
209         final HtmlTextArea text1 = page.getHtmlElementById("text1");
210         text1.type("abcd");
211         assertEquals("abc", text1.getText());
212     }
213 
214     /**
215      * @throws Exception if the test fails
216      */
217     @Test
218     public void type() throws Exception {
219         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
220         final HtmlPage page = loadPage(html, null);
221         final HtmlTextArea t = page.getHtmlElementById("t");
222         t.type("abc");
223         assertEquals("abc", t.getText());
224         t.type('\b');
225         assertEquals("ab", t.getText());
226         t.type('\b');
227         assertEquals("a", t.getText());
228         t.type('\b');
229         assertEquals("", t.getText());
230         t.type('\b');
231         assertEquals("", t.getText());
232         t.type("ab\ncd");
233         assertEquals("ab\ncd", t.getText());
234         t.type("\r\nef");
235         assertEquals("ab\ncd\r\nef", t.getText());
236     }
237 
238     /**
239      * @throws Exception if the test fails
240      */
241     @Test
242     public void typeWithSelection() throws Exception {
243         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
244         final HtmlPage page = loadPage(html, null);
245         final HtmlTextArea t = page.getHtmlElementById("t");
246         t.type("abc");
247         assertEquals("abc", t.getText());
248 
249         t.setSelectionStart(1);
250         t.setSelectionEnd(3);
251         t.type('x');
252         assertEquals("ax", t.getText());
253 
254         t.setSelectionStart(0);
255         t.setSelectionEnd(0);
256         t.type('y');
257         assertEquals("yax", t.getText());
258     }
259 
260     /**
261      * @throws Exception if the test fails
262      */
263     @Test
264     public void typeWhileDisabled() throws Exception {
265         final String html = DOCTYPE_HTML + "<html><body><textarea id='t' disabled='disabled'></textarea></body></html>";
266         final HtmlPage page = loadPage(html);
267         final HtmlTextArea t = page.getHtmlElementById("t");
268         t.type("abc");
269         assertEquals("", t.getText());
270     }
271 
272     /**
273      * Style=visibility:hidden should not affect getText().
274      * @throws Exception if the test fails
275      */
276     @Test
277     public void getTextAndVisibility() throws Exception {
278         final String content = DOCTYPE_HTML
279             + "<html><head><title>foo</title></head><body>\n"
280             + "<form id='form1'>\n"
281             + "<textarea name='textArea1' style='visibility:hidden'> foo \n bar "
282             + "</textarea>\n"
283             + "</form></body></html>";
284         final HtmlPage page = loadPage(content);
285         final HtmlForm form = page.getHtmlElementById("form1");
286         final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
287         assertNotNull(textArea);
288         assertEquals(" foo \n bar ", textArea.getText());
289     }
290 
291     /**
292      * @throws Exception if the test fails
293      */
294     @Test
295     public void typeLeftArrow() throws Exception {
296         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
297         final HtmlPage page = loadPage(html);
298         final HtmlTextArea t = page.getHtmlElementById("t");
299         t.type('t');
300         t.type('e');
301         t.type('t');
302         assertEquals("tet", t.getText());
303         t.type(KeyboardEvent.DOM_VK_LEFT);
304         assertEquals("tet", t.getText());
305         t.type('s');
306         assertEquals("test", t.getText());
307         t.type(KeyboardEvent.DOM_VK_SPACE);
308         assertEquals("tes t", t.getText());
309     }
310 
311     /**
312      * @throws Exception if the test fails
313      */
314     @Test
315     public void typeDelKey() throws Exception {
316         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
317         final HtmlPage page = loadPage(html);
318         final HtmlTextArea t = page.getHtmlElementById("t");
319         t.type('t');
320         t.type('e');
321         t.type('t');
322         assertEquals("tet", t.getText());
323         t.type(KeyboardEvent.DOM_VK_LEFT);
324         t.type(KeyboardEvent.DOM_VK_LEFT);
325         assertEquals("tet", t.getText());
326         t.type(KeyboardEvent.DOM_VK_DELETE);
327         assertEquals("tt", t.getText());
328     }
329 
330     /**
331      * Make sure removeFocus does not throw.
332      *
333      * @throws Exception if the test fails
334      */
335     @Test
336     public void removeFocus() throws Exception {
337         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
338         final HtmlPage page = loadPage(html, null);
339         final HtmlTextArea t = page.getHtmlElementById("t");
340         t.removeFocus();
341     }
342 
343     /**
344      * @throws Exception if the test fails
345      */
346     @Test
347     public void typingAndClone() throws Exception {
348         final String htmlContent = DOCTYPE_HTML
349             + "<html>\n"
350             + "<head></head>\n"
351             + "<body>\n"
352             + "<form id='form1'>\n"
353             + "  <textarea id='foo'></textarea>\n"
354             + "</form>\n"
355             + "</body></html>";
356 
357         final HtmlPage page = loadPage(htmlContent);
358 
359         HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
360         input = (HtmlTextArea) input.cloneNode(true);
361         input.type("4711");
362         assertEquals("4711", input.getText());
363         assertEquals("", input.getTextContent());
364     }
365 
366     /**
367      * @throws Exception if the test fails
368      */
369     @Test
370     public void typingAndReset() throws Exception {
371         final String htmlContent = DOCTYPE_HTML
372             + "<html>\n"
373             + "<head></head>\n"
374             + "<body>\n"
375             + "<form id='form1'>\n"
376             + "  <textarea id='foo'></textarea>\n"
377             + "</form>\n"
378             + "</body></html>";
379 
380         final HtmlPage page = loadPage(htmlContent);
381 
382         final HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
383 
384         input.type("4711");
385         input.reset();
386         input.type("0815");
387 
388         assertEquals("0815", input.getText());
389         assertEquals("", input.getTextContent());
390     }
391 
392     /**
393      * @throws Exception if the test fails
394      */
395     @Test
396     public void typingAndSetTextContent() throws Exception {
397         final String htmlContent = DOCTYPE_HTML
398             + "<html>\n"
399             + "<head></head>\n"
400             + "<body>\n"
401             + "<form id='form1'>\n"
402             + "  <textarea id='foo'></textarea>\n"
403             + "</form>\n"
404             + "</body></html>";
405 
406         final HtmlPage page = loadPage(htmlContent);
407 
408         final HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
409 
410         input.type("4711");
411         input.setTextContent("");
412         input.type("0815");
413 
414         assertEquals("47110815", input.getText());
415         assertEquals("", input.getTextContent());
416     }
417 }