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