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.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.htmlunit.javascript.host.event.KeyboardEvent;
19  import org.htmlunit.junit.BrowserRunner;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link HtmlPasswordInput}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   * @author Anton Demydenko
30   */
31  @RunWith(BrowserRunner.class)
32  public class HtmlPasswordInput2Test extends SimpleWebTestCase {
33  
34      /**
35       * Verifies that asNormalizedText() returns the value string.
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts("bla")
40      public void asNormalizedText() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html>\n"
43              + "<head></head>\n"
44              + "<body>\n"
45              + "<form id='form1'>\n"
46              + "  <input type='password' name='tester' id='tester' value='bla'>\n"
47              + "</form>\n"
48              + "</body></html>";
49  
50          final HtmlPage page = loadPage(html);
51          assertEquals(getExpectedAlerts()[0], page.getBody().asNormalizedText());
52      }
53  
54      /**
55       * How could this test be migrated to WebDriver? How to select the field's content?
56       * @throws Exception if an error occurs
57       */
58      @Test
59      public void typeWhileSelected() throws Exception {
60          final String html = DOCTYPE_HTML
61              + "<html><head></head><body>\n"
62              + "<input type='password' id='myInput' value='Hello world'><br>\n"
63              + "</body></html>";
64          final HtmlPage page = loadPage(html);
65          final HtmlPasswordInput input = page.getHtmlElementById("myInput");
66          input.select();
67          input.type("Bye World");
68          assertEquals("Hello world", input.getValueAttribute());
69          assertEquals("Bye World", input.getValue());
70      }
71  
72      /**
73       * @throws Exception if the test fails
74       */
75      @Test
76      public void typeLeftArrow() throws Exception {
77          final String html = DOCTYPE_HTML
78                  + "<html><head></head><body><input type='password' id='t'/></body></html>";
79          final HtmlPage page = loadPage(html);
80          final HtmlPasswordInput t = page.getHtmlElementById("t");
81          t.type('t');
82          t.type('e');
83          t.type('t');
84          assertEquals("", t.getValueAttribute());
85          assertEquals("tet", t.getValue());
86          t.type(KeyboardEvent.DOM_VK_LEFT);
87          assertEquals("", t.getValueAttribute());
88          assertEquals("tet", t.getValue());
89          t.type('s');
90          assertEquals("", t.getValueAttribute());
91          assertEquals("test", t.getValue());
92          t.type(KeyboardEvent.DOM_VK_SPACE);
93          assertEquals("", t.getValueAttribute());
94          assertEquals("tes t", t.getValue());
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     public void typeDelKey() throws Exception {
102         final String html = DOCTYPE_HTML
103                 + "<html><head></head><body><input type='password' id='t'/></body></html>";
104         final HtmlPage page = loadPage(html);
105         final HtmlPasswordInput t = page.getHtmlElementById("t");
106         t.type('t');
107         t.type('e');
108         t.type('t');
109         assertEquals("", t.getValueAttribute());
110         assertEquals("tet", t.getValue());
111         t.type(KeyboardEvent.DOM_VK_LEFT);
112         t.type(KeyboardEvent.DOM_VK_LEFT);
113         assertEquals("", t.getValueAttribute());
114         assertEquals("tet", t.getValue());
115         t.type(KeyboardEvent.DOM_VK_DELETE);
116         assertEquals("", t.getValueAttribute());
117         assertEquals("tt", t.getValue());
118     }
119 
120     /**
121      * @throws Exception if the test fails
122      */
123     @Test
124     public void typingAndClone() throws Exception {
125         final String htmlContent = DOCTYPE_HTML
126             + "<html>\n"
127             + "<head></head>\n"
128             + "<body>\n"
129             + "<form id='form1'>\n"
130             + "  <input type='password' id='foo'>\n"
131             + "</form>\n"
132             + "</body></html>";
133 
134         final HtmlPage page = loadPage(htmlContent);
135 
136         HtmlPasswordInput input = (HtmlPasswordInput) page.getElementById("foo");
137         input = (HtmlPasswordInput) input.cloneNode(true);
138         input.type("4711");
139         assertEquals("", input.getValueAttribute());
140         assertEquals("4711", input.getValue());
141     }
142 
143     /**
144      * @throws Exception if the test fails
145      */
146     @Test
147     public void typingAndReset() throws Exception {
148         final String htmlContent = DOCTYPE_HTML
149             + "<html>\n"
150             + "<head></head>\n"
151             + "<body>\n"
152             + "<form id='form1'>\n"
153             + "  <input type='password' id='foo'>\n"
154             + "</form>\n"
155             + "</body></html>";
156 
157         final HtmlPage page = loadPage(htmlContent);
158 
159         final HtmlPasswordInput input = (HtmlPasswordInput) page.getElementById("foo");
160 
161         input.type("4711");
162         input.reset();
163         input.type("0815");
164 
165         assertEquals("", input.getValueAttribute());
166         assertEquals("0815", input.getValue());
167     }
168 
169     /**
170      * @throws Exception if the test fails
171      */
172     @Test
173     public void typingAndSetValueAttribute() throws Exception {
174         final String htmlContent = DOCTYPE_HTML
175             + "<html>\n"
176             + "<head></head>\n"
177             + "<body>\n"
178             + "<form id='form1'>\n"
179             + "  <input type='password' id='foo'>\n"
180             + "</form>\n"
181             + "</body></html>";
182 
183         final HtmlPage page = loadPage(htmlContent);
184 
185         final HtmlPasswordInput input = (HtmlPasswordInput) page.getElementById("foo");
186 
187         input.type("4711");
188         input.setValueAttribute("");
189         input.type("0815");
190 
191         assertEquals("", input.getValueAttribute());
192         assertEquals("47110815", input.getValue());
193     }
194 
195     /**
196      * @throws Exception if the test fails
197      */
198     @Test
199     public void typingAndSetValue() throws Exception {
200         final String htmlContent = DOCTYPE_HTML
201             + "<html>\n"
202             + "<head></head>\n"
203             + "<body>\n"
204             + "<form id='form1'>\n"
205             + "  <input type='password' id='foo'>\n"
206             + "</form>\n"
207             + "</body></html>";
208 
209         final HtmlPage page = loadPage(htmlContent);
210 
211         final HtmlPasswordInput input = (HtmlPasswordInput) page.getElementById("foo");
212 
213         input.type("4711");
214         input.setValue("");
215         input.type("0815");
216 
217         assertEquals("", input.getValueAttribute());
218         assertEquals("0815", input.getValue());
219     }
220 
221     /**
222      * @throws Exception if the test fails
223      */
224     @Test
225     public void patternValidation() throws Exception {
226         final String htmlContent = DOCTYPE_HTML
227             + "<html>\n"
228             + "<head></head>\n"
229             + "<body>\n"
230             + "<form id='form1'>\n"
231             + "  <input type='password' pattern='[0-9a-zA-Z]{10,40}' id='foo'>\n"
232             + "</form>\n"
233             + "</body></html>";
234 
235         final HtmlPage page = loadPage(htmlContent);
236 
237         final HtmlPasswordInput input = (HtmlPasswordInput) page.getElementById("foo");
238 
239         // empty
240         assertTrue(input.isValid());
241         // invalid
242         input.setValue("0987654321!");
243         assertFalse(input.isValid());
244         // valid
245         input.setValue("68746d6c756e69742072756c657a21");
246         assertTrue(input.isValid());
247     }
248 
249     /**
250      * @throws Exception
251      *         if the test fails
252      */
253     @Test
254     @Alerts({"true", "true", "true", "", "foo"})
255     public void maxLengthValidation() throws Exception {
256         final String htmlContent = DOCTYPE_HTML
257             + "<html>\n"
258             + "<head></head>\n"
259             + "<body>\n"
260             + "<form id='form1'>\n"
261             + "  <input type='password' id='foo' maxLength='3'>\n"
262             + "</form>\n"
263             + "</body></html>";
264 
265         final HtmlPage page = loadPage(htmlContent);
266 
267         final HtmlInput input = (HtmlInput) page.getElementById("foo");
268         assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
269         input.type("foo");
270         assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
271         input.type("bar");
272         assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
273         assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
274         assertEquals(getExpectedAlerts()[4], input.getValue());
275     }
276 
277     /**
278      * @throws Exception
279      *         if the test fails
280      */
281     @Test
282     @Alerts({"true", "false", "true", "", "foobar"})
283     public void minLengthValidation() throws Exception {
284         final String htmlContent = DOCTYPE_HTML
285             + "<html>\n"
286             + "<head></head>\n"
287             + "<body>\n"
288             + "<form id='form1'>\n"
289             + "  <input type='password' id='foo' minLength='4'>\n"
290             + "</form>\n"
291             + "</body></html>";
292 
293         final HtmlPage page = loadPage(htmlContent);
294 
295         final HtmlInput input = (HtmlInput) page.getElementById("foo");
296         assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
297         input.type("foo");
298         assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
299         input.type("bar");
300         assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
301         assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
302         assertEquals(getExpectedAlerts()[4], input.getValue());
303     }
304 }