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