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.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  
25  /**
26   * Tests for {@link HtmlButtonInput}.
27   *
28   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
29   * @author Ronald Brill
30   * @author Frank Danek
31   */
32  @RunWith(BrowserRunner.class)
33  public class HtmlButtonInput2Test extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      @Alerts({"-", "-", "-"})
40      public void defaultValues() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html><head>\n"
43              + "<script>\n"
44              + LOG_TITLE_FUNCTION
45              + "  function test() {\n"
46              + "    var input = document.getElementById('button1');\n"
47              + "    log(input.value + '-' + input.defaultValue);\n"
48  
49              + "    input = document.createElement('input');\n"
50              + "    input.type = 'button';\n"
51              + "    log(input.value + '-' + input.defaultValue);\n"
52  
53              + "    var builder = document.createElement('div');\n"
54              + "    builder.innerHTML = '<input type=\"button\">';\n"
55              + "    input = builder.firstChild;\n"
56              + "    log(input.value + '-' + input.defaultValue);\n"
57              + "  }\n"
58              + "</script>\n"
59              + "</head><body onload='test()'>\n"
60              + "<form>\n"
61              + "  <input type='button' id='button1'>\n"
62              + "</form>\n"
63              + "</body></html>";
64  
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"-", "-", "-"})
73      public void defaultValuesAfterClone() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html><head>\n"
76              + "<script>\n"
77              + LOG_TITLE_FUNCTION
78              + "  function test() {\n"
79              + "    var input = document.getElementById('button1');\n"
80              + "    input = input.cloneNode(false);\n"
81              + "    log(input.value + '-' + input.defaultValue);\n"
82  
83              + "    input = document.createElement('input');\n"
84              + "    input.type = 'button';\n"
85              + "    input = input.cloneNode(false);\n"
86              + "    log(input.value + '-' + input.defaultValue);\n"
87  
88              + "    var builder = document.createElement('div');\n"
89              + "    builder.innerHTML = '<input type=\"button\">';\n"
90              + "    input = builder.firstChild;\n"
91              + "    input = input.cloneNode(false);\n"
92              + "    log(input.value + '-' + input.defaultValue);\n"
93              + "  }\n"
94              + "</script>\n"
95              + "</head><body onload='test()'>\n"
96              + "<form>\n"
97              + "  <input type='button' id='button1'>\n"
98              + "</form>\n"
99              + "</body></html>";
100 
101         loadPageVerifyTitle2(html);
102     }
103 
104     /**
105      * @throws Exception if the test fails
106      */
107     @Test
108     @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
109                 "newDefault-newDefault", "newDefault-newDefault"})
110     public void resetByClick() throws Exception {
111         final String html = DOCTYPE_HTML
112             + "<html><head>\n"
113             + "<script>\n"
114             + LOG_TITLE_FUNCTION
115             + "  function test() {\n"
116             + "    var button = document.getElementById('testId');\n"
117             + "    log(button.value + '-' + button.defaultValue);\n"
118 
119             + "    document.getElementById('testReset').click;\n"
120             + "    log(button.value + '-' + button.defaultValue);\n"
121 
122             + "    button.value = 'newValue';\n"
123             + "    log(button.value + '-' + button.defaultValue);\n"
124 
125             + "    document.getElementById('testReset').click;\n"
126             + "    log(button.value + '-' + button.defaultValue);\n"
127 
128             + "    button.defaultValue = 'newDefault';\n"
129             + "    log(button.value + '-' + button.defaultValue);\n"
130 
131             + "    document.forms[0].reset;\n"
132             + "    log(button.value + '-' + button.defaultValue);\n"
133             + "  }\n"
134             + "</script>\n"
135             + "</head><body onload='test()'>\n"
136             + "<form>\n"
137             + "  <input type='button' id='testId' value='initial'>\n"
138             + "  <input type='reset' id='testReset'>\n"
139             + "</form>\n"
140             + "</body></html>";
141 
142         loadPageVerifyTitle2(html);
143     }
144 
145     /**
146      * @throws Exception if the test fails
147      */
148     @Test
149     @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
150                 "newDefault-newDefault", "newDefault-newDefault"})
151     public void resetByJS() throws Exception {
152         final String html = DOCTYPE_HTML
153             + "<html><head>\n"
154             + "<script>\n"
155             + LOG_TITLE_FUNCTION
156             + "  function test() {\n"
157             + "    var button = document.getElementById('testId');\n"
158             + "    log(button.value + '-' + button.defaultValue);\n"
159 
160             + "    document.forms[0].reset;\n"
161             + "    log(button.value + '-' + button.defaultValue);\n"
162 
163             + "    button.value = 'newValue';\n"
164             + "    log(button.value + '-' + button.defaultValue);\n"
165 
166             + "    document.forms[0].reset;\n"
167             + "    log(button.value + '-' + button.defaultValue);\n"
168 
169             + "    button.defaultValue = 'newDefault';\n"
170             + "    log(button.value + '-' + button.defaultValue);\n"
171 
172             + "    document.forms[0].reset;\n"
173             + "    log(button.value + '-' + button.defaultValue);\n"
174             + "  }\n"
175             + "</script>\n"
176             + "</head><body onload='test()'>\n"
177             + "<form>\n"
178             + "  <input type='button' id='testId' value='initial'>\n"
179             + "</form>\n"
180             + "</body></html>";
181 
182         loadPageVerifyTitle2(html);
183     }
184 
185     /**
186      * @throws Exception if the test fails
187      */
188     @Test
189     @Alerts({"initial-initial", "default-default", "newValue-newValue", "newDefault-newDefault"})
190     public void defaultValue() throws Exception {
191         final String html = DOCTYPE_HTML
192             + "<html><head>\n"
193             + "<script>\n"
194             + LOG_TITLE_FUNCTION
195             + "  function test() {\n"
196             + "    var button = document.getElementById('testId');\n"
197             + "    log(button.value + '-' + button.defaultValue);\n"
198 
199             + "    button.defaultValue = 'default';\n"
200             + "    log(button.value + '-' + button.defaultValue);\n"
201 
202             + "    button.value = 'newValue';\n"
203             + "    log(button.value + '-' + button.defaultValue);\n"
204             + "    button.defaultValue = 'newDefault';\n"
205             + "    log(button.value + '-' + button.defaultValue);\n"
206             + "  }\n"
207             + "</script>\n"
208             + "</head><body onload='test()'>\n"
209             + "<form>\n"
210             + "  <input type='button' id='testId' value='initial'>\n"
211             + "</form>\n"
212             + "</body></html>";
213 
214         loadPageVerifyTitle2(html);
215     }
216 
217     /**
218      * @throws Exception if the test fails
219      */
220     @Test
221     @Alerts("foo")
222     public void click_onClick() throws Exception {
223         final String html = DOCTYPE_HTML
224                 + "<html>\n"
225                 + "<head>"
226                 + "<script>\n"
227                 + LOG_TITLE_FUNCTION
228                 + "</script>\n"
229                 + "</head>\n"
230                 + "<body>\n"
231                 + "<form id='form1' onSubmit='log(\"bar\")'>\n"
232                 + "  <input type='button' name='button' id='button' onClick='log(\"foo\")'>Push me</button>\n"
233                 + "</form>\n"
234                 + "</body></html>";
235 
236         final WebDriver driver = loadPage2(html);
237         driver.findElement(By.id("button")).click();
238 
239         verifyTitle2(driver, getExpectedAlerts());
240     }
241 
242     /**
243      * @throws Exception if the test fails
244      */
245     @Test
246     @Alerts("foo")
247     public void click_onClickIgnoreCase() throws Exception {
248         final String html = DOCTYPE_HTML
249                 + "<html>\n"
250                 + "<head>\n"
251                 + "<script>\n"
252                 + LOG_TITLE_FUNCTION
253                 + "</script>\n"
254                 + "</head>\n"
255                 + "<body>\n"
256                 + "<form id='form1'>\n"
257                 + "  <input type='button' name='button' id='button' oNclICK='log(\"foo\")'>Push me</button>\n"
258                 + "</form>\n"
259                 + "</body></html>";
260 
261         final WebDriver driver = loadPage2(html);
262         driver.findElement(By.id("button")).click();
263 
264         verifyTitle2(driver, getExpectedAlerts());
265     }
266 
267     /**
268      * @throws Exception if the test fails
269      */
270     @Test
271     @Alerts("--")
272     public void minMaxStep() throws Exception {
273         final String html = DOCTYPE_HTML
274             + "<html>\n"
275             + "<head>\n"
276             + "<script>\n"
277             + LOG_TITLE_FUNCTION
278             + "  function test() {\n"
279             + "    var input = document.getElementById('tester');\n"
280             + "    log(input.min + '-' + input.max + '-' + input.step);\n"
281             + "  }\n"
282             + "</script>\n"
283             + "</head>\n"
284             + "<body onload='test()'>\n"
285             + "<form>\n"
286             + "  <input type='button' id='tester'>\n"
287             + "</form>\n"
288             + "</body>\n"
289             + "</html>";
290 
291         loadPageVerifyTitle2(html);
292     }
293 
294     /**
295      * @throws Exception if an error occurs
296      */
297     @Test
298     @Alerts({"false", "false", "false", "false", "false"})
299     public void willValidate() throws Exception {
300         final String html = DOCTYPE_HTML
301                 + "<html><head>\n"
302                 + "  <script>\n"
303                 + LOG_TITLE_FUNCTION
304                 + "    function test() {\n"
305                 + "      log(document.getElementById('o1').willValidate);\n"
306                 + "      log(document.getElementById('o2').willValidate);\n"
307                 + "      log(document.getElementById('o3').willValidate);\n"
308                 + "      log(document.getElementById('o4').willValidate);\n"
309                 + "      log(document.getElementById('o5').willValidate);\n"
310                 + "    }\n"
311                 + "  </script>\n"
312                 + "</head>\n"
313                 + "<body onload='test()'>\n"
314                 + "  <form>\n"
315                 + "    <input type='button' id='o1'>\n"
316                 + "    <input type='button' id='o2' disabled>\n"
317                 + "    <input type='button' id='o3' hidden>\n"
318                 + "    <input type='button' id='o4' readonly>\n"
319                 + "    <input type='button' id='o5' style='display: none'>\n"
320                 + "  </form>\n"
321                 + "</body></html>";
322 
323         loadPageVerifyTitle2(html);
324     }
325 
326     /**
327      * @throws Exception if an error occurs
328      */
329     @Test
330     @Alerts({"true",
331              "false-false-false-false-false-false-false-false-false-true-false",
332              "false"})
333     public void validationEmpty() throws Exception {
334         validation("<input type='button' id='e1'>\n", "");
335     }
336 
337     /**
338      * @throws Exception if an error occurs
339      */
340     @Test
341     @Alerts({"true",
342              "false-true-false-false-false-false-false-false-false-false-false",
343              "false"})
344     public void validationCustomValidity() throws Exception {
345         validation("<input type='button' id='e1'>\n", "elem.setCustomValidity('Invalid');");
346     }
347 
348     /**
349      * @throws Exception if an error occurs
350      */
351     @Test
352     @Alerts({"true",
353              "false-true-false-false-false-false-false-false-false-false-false",
354              "false"})
355     public void validationBlankCustomValidity() throws Exception {
356         validation("<input type='button' id='e1'>\n", "elem.setCustomValidity(' ');\n");
357     }
358 
359     /**
360      * @throws Exception if an error occurs
361      */
362     @Test
363     @Alerts({"true",
364              "false-false-false-false-false-false-false-false-false-true-false",
365              "false"})
366     public void validationResetCustomValidity() throws Exception {
367         validation("<input type='button' id='e1'>\n",
368                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
369     }
370 
371     /**
372      * @throws Exception if an error occurs
373      */
374     @Test
375     @Alerts({"true",
376              "false-false-false-false-false-false-false-false-false-true-false",
377              "false"})
378     public void validationRequired() throws Exception {
379         validation("<input type='button' id='e1' required>\n", "");
380     }
381 
382     private void validation(final String htmlPart, final String jsPart) throws Exception {
383         final String html = DOCTYPE_HTML
384                 + "<html><head>\n"
385                 + "  <script>\n"
386                 + LOG_TITLE_FUNCTION
387                 + "    function logValidityState(s) {\n"
388                 + "      log(s.badInput"
389                         + "+ '-' + s.customError"
390                         + "+ '-' + s.patternMismatch"
391                         + "+ '-' + s.rangeOverflow"
392                         + "+ '-' + s.rangeUnderflow"
393                         + "+ '-' + s.stepMismatch"
394                         + "+ '-' + s.tooLong"
395                         + "+ '-' + s.tooShort"
396                         + " + '-' + s.typeMismatch"
397                         + " + '-' + s.valid"
398                         + " + '-' + s.valueMissing);\n"
399                 + "    }\n"
400                 + "    function test() {\n"
401                 + "      var elem = document.getElementById('e1');\n"
402                 + jsPart
403                 + "      log(elem.checkValidity());\n"
404                 + "      logValidityState(elem.validity);\n"
405                 + "      log(elem.willValidate);\n"
406                 + "    }\n"
407                 + "  </script>\n"
408                 + "</head>\n"
409                 + "<body onload='test()'>\n"
410                 + "  <form>\n"
411                 + htmlPart
412                 + "  </form>\n"
413                 + "</body></html>";
414 
415         loadPageVerifyTitle2(html);
416     }
417 }