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