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