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 java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link HtmlResetInput}.
25   *
26   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
27   * @author Ahmed Ashour
28   */
29  public class HtmlResetInputTest extends SimpleWebTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      public void reset() throws Exception {
36          final String htmlContent = DOCTYPE_HTML
37              + "<html><head><title>foo</title></head><body>\n"
38              + "<form id='form1'>\n"
39              + "<input type='text' name='textfield1' id='textfield1' value='foo'/>\n"
40              + "<input type='password' name='password1' id='password1' value='foo'/>\n"
41              + "<input type='hidden' name='hidden1' id='hidden1' value='foo'/>\n"
42              + "<input type='radio' name='radioButton' value='foo' checked/>\n"
43              + "<input type='radio' name='radioButton' value='bar'/>\n"
44              + "<input type='checkbox' name='checkBox' value='check'/>\n"
45              + "<select id='select1'>\n"
46              + "  <option id='option1' selected value='1'>Option1</option>\n"
47              + "  <option id='option2' value='2'>Option2</option>\n"
48              + "</select>\n"
49              + "<textarea id='textarea1'>Foobar</textarea>\n"
50              + "<isindex prompt='Enter some text' id='isindex1'>\n"
51              + "<input type='reset' name='resetButton' value='pushme'/>\n"
52              + "</form></body></html>";
53          final HtmlPage page = loadPage(htmlContent);
54  
55          final HtmlForm form = page.getHtmlElementById("form1");
56          final HtmlResetInput resetInput = form.getInputByName("resetButton");
57  
58          // change all the values to something else
59          form.<HtmlRadioButtonInput>getFirstByXPath(
60                  "//input[@type='radio' and @name='radioButton' and @value='bar']").setChecked(true);
61          form.<HtmlCheckBoxInput>getInputByName("checkBox").setChecked(true);
62          page.<HtmlOption>getHtmlElementById("option1").setSelected(false);
63          page.<HtmlOption>getHtmlElementById("option2").setSelected(true);
64          page.<HtmlTextArea>getHtmlElementById("textarea1").setText("Flintstone");
65          page.<HtmlTextInput>getHtmlElementById("textfield1").setValue("Flintstone");
66          page.<HtmlHiddenInput>getHtmlElementById("hidden1").setValue("Flintstone");
67          page.<HtmlPasswordInput>getHtmlElementById("password1").setValue("Flintstone");
68  
69          // Check to make sure they did get changed
70          assertEquals("bar", form.getCheckedRadioButton("radioButton").getValueAttribute());
71          assertEquals("bar", form.getCheckedRadioButton("radioButton").getValue());
72          assertTrue(form.<HtmlCheckBoxInput>getInputByName("checkBox").isChecked());
73          assertFalse(page.<HtmlOption>getHtmlElementById("option1").isSelected());
74          assertTrue(page.<HtmlOption>getHtmlElementById("option2").isSelected());
75          assertEquals("Flintstone", page.<HtmlTextArea>getHtmlElementById("textarea1").getText());
76          assertEquals("foo", page.<HtmlTextInput>getHtmlElementById("textfield1").getValueAttribute());
77          assertEquals("Flintstone", page.<HtmlTextInput>getHtmlElementById("textfield1").getValue());
78          assertEquals("Flintstone", page.<HtmlHiddenInput>getHtmlElementById("hidden1").getValueAttribute());
79          assertEquals("Flintstone", page.<HtmlHiddenInput>getHtmlElementById("hidden1").getValue());
80  
81          final HtmlPage secondPage = resetInput.click();
82          assertSame(page, secondPage);
83  
84          // Check to make sure all the values have been set back to their original values.
85          assertEquals("foo", form.getCheckedRadioButton("radioButton").getValueAttribute());
86          assertEquals("foo", form.getCheckedRadioButton("radioButton").getValue());
87          assertFalse(form.<HtmlCheckBoxInput>getInputByName("checkBox").isChecked());
88          assertTrue(page.<HtmlOption>getHtmlElementById("option1").isSelected());
89          assertFalse(page.<HtmlOption>getHtmlElementById("option2").isSelected());
90          assertEquals("Foobar", page.<HtmlTextArea>getHtmlElementById("textarea1").getText());
91          assertEquals("foo", page.<HtmlTextInput>getHtmlElementById("textfield1").getValueAttribute());
92          assertEquals("foo", page.<HtmlTextInput>getHtmlElementById("textfield1").getValue());
93  
94          // this is strange but this is the way the browsers are working
95          // org.htmlunit.html.HtmlHiddenInputTest.reset()
96          assertEquals("Flintstone", page.<HtmlHiddenInput>getHtmlElementById("hidden1").getValueAttribute());
97          assertEquals("Flintstone", page.<HtmlHiddenInput>getHtmlElementById("hidden1").getValue());
98  
99          assertEquals("foo", page.<HtmlPasswordInput>getHtmlElementById("password1").getValueAttribute());
100         assertEquals("foo", page.<HtmlPasswordInput>getHtmlElementById("password1").getValue());
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     public void resetClick_onClick() throws Exception {
108         final String htmlContent = DOCTYPE_HTML
109             + "<html><head><title>foo</title></head><body>\n"
110             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
111             + "  <button type='reset' name='button' id='button' "
112             + "onClick='alert(\"foo\")'>Push me</button>\n"
113             + "</form></body></html>";
114         final List<String> collectedAlerts = new ArrayList<>();
115         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
116         final HtmlButton button = page.getHtmlElementById("button");
117 
118         final HtmlPage secondPage = button.click();
119 
120         final String[] expectedAlerts = {"foo", "reset"};
121         assertEquals(expectedAlerts, collectedAlerts);
122 
123         assertSame(page, secondPage);
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     public void outsideForm() throws Exception {
131         final String html = DOCTYPE_HTML
132             + "<html><head></head>\n"
133             + "<body>\n"
134             + "<input id='myInput' type='reset' onclick='alert(1)'>\n"
135             + "</body></html>";
136 
137         final String[] expectedAlerts = {"1"};
138         final List<String> collectedAlerts = new ArrayList<>();
139         final HtmlPage page = loadPage(html, collectedAlerts);
140         final HtmlResetInput input = page.getHtmlElementById("myInput");
141         input.click();
142 
143         assertEquals(expectedAlerts, collectedAlerts);
144     }
145 }