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