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.Arrays;
19  import java.util.List;
20  
21  import org.htmlunit.SimpleWebTestCase;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.htmlunit.util.NameValuePair;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for {@link HtmlButton}.
28   *
29   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
30   * @author David K. Taylor
31   * @author Brad Clarke
32   * @author Marc Guillemot
33   * @author Ahmed Ashour
34   * @author Ronald Brill
35   */
36  public class HtmlButtonTest extends SimpleWebTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      public void buttonClick_onClick() throws Exception {
43          final String htmlContent = DOCTYPE_HTML
44              + "<html><head><title>foo</title></head><body>\n"
45              + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
46              + "  <button type='button' name='button' id='button' "
47              + "onClick='alert(\"foo\")'>Push me</button>\n"
48              + "</form></body></html>";
49  
50          final List<String> collectedAlerts = new ArrayList<>();
51          final HtmlPage page = loadPage(htmlContent, collectedAlerts);
52          final HtmlButton button = page.getHtmlElementById("button");
53  
54          final HtmlPage secondPage = button.click();
55  
56          final String[] expectedAlerts = {"foo"};
57          assertEquals(expectedAlerts, collectedAlerts);
58  
59          assertSame(page, secondPage);
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      public void submitClick_onClick() throws Exception {
67          final String htmlContent = DOCTYPE_HTML
68              + "<html><head><title>foo</title></head><body>\n"
69              + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
70              + "  <button type='submit' name='button' id='button' "
71              + "onClick='alert(\"foo\")'>Push me</button>\n"
72              + "</form></body></html>";
73          final List<String> collectedAlerts = new ArrayList<>();
74          final HtmlPage page = loadPage(htmlContent, collectedAlerts);
75          final HtmlButton button = page.getHtmlElementById("button");
76  
77          final HtmlPage secondPage = button.click();
78  
79          final String[] expectedAlerts = {"foo", "bar"};
80          assertEquals(expectedAlerts, collectedAlerts);
81  
82          assertNotSame(page, secondPage);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      public void buttonTypeSubmit() throws Exception {
90          final String htmlContent = DOCTYPE_HTML
91              + "<html><head><title>foo</title></head><body>\n"
92              + "<form id='form1' method='post' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
93              + "  <button type='submit' name='button' id='button' value='foo'"
94              + "    >Push me</button>\n"
95              + "</form></body></html>";
96          final List<String> collectedAlerts = new ArrayList<>();
97          final HtmlPage page = loadPage(htmlContent, collectedAlerts);
98          final HtmlButton button = page.getHtmlElementById("button");
99  
100         final HtmlPage secondPage = button.click();
101 
102         final String[] expectedAlerts = {"bar"};
103         assertEquals(expectedAlerts, collectedAlerts);
104 
105         assertNotSame(page, secondPage);
106         final List<NameValuePair> expectedParameters = Arrays.asList(new NameValuePair[]{
107             new NameValuePair("button", "foo")
108         });
109         final List<NameValuePair> collectedParameters = getMockConnection(secondPage).getLastParameters();
110 
111         assertEquals(expectedParameters, collectedParameters);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts("")
119     public void defaultButtonTypeNotSet() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html><head><title>First</title></head><body>\n"
122             + "<form id='form1' action='" + URL_SECOND + "' method='post'>\n"
123             + "  <button name='button' id='button' value='pushme'>PushMe</button>\n"
124             + "</form></body></html>";
125 
126         final HtmlPage page = loadPage(html);
127         final HtmlButton button = page.getHtmlElementById("button");
128         assertEquals(getExpectedAlerts()[0], button.getTypeAttribute());
129         assertEquals(getExpectedAlerts()[0], button.getAttribute("type"));
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts("Submit")
137     public void defaultButtonTypeSubmit() throws Exception {
138         final String html = DOCTYPE_HTML
139             + "<html><head><title>First</title></head><body>\n"
140             + "<form id='form1' action='" + URL_SECOND + "' method='post'>\n"
141             + "  <button name='button' id='button' type='Submit' value='pushme'>PushMe</button>\n"
142             + "</form></body></html>";
143 
144         final HtmlPage page = loadPage(html);
145         final HtmlButton button = page.getHtmlElementById("button");
146         assertEquals(getExpectedAlerts()[0], button.getTypeAttribute());
147         assertEquals(getExpectedAlerts()[0], button.getAttribute("type"));
148     }
149 
150     /**
151      * @throws Exception if the test fails
152      */
153     @Test
154     @Alerts("invalid")
155     public void defaultButtonTypeInvalid() throws Exception {
156         final String html = DOCTYPE_HTML
157             + "<html><head><title>First</title></head><body>\n"
158             + "<form id='form1' action='" + URL_SECOND + "' method='post'>\n"
159             + "  <button name='button' id='button' type='invalid' value='pushme'>PushMe</button>\n"
160             + "</form></body></html>";
161 
162         final HtmlPage page = loadPage(html);
163         final HtmlButton button = page.getHtmlElementById("button");
164         assertEquals(getExpectedAlerts()[0], button.getTypeAttribute());
165         assertEquals(getExpectedAlerts()[0], button.getAttribute("type"));
166     }
167 
168     /**
169      * @throws Exception if the test fails
170      */
171     @Test
172     public void asXml() throws Exception {
173         final String html = DOCTYPE_HTML
174             + "<html><body><title>foo</title>\n"
175             + "<button></button>\n"
176             + "</body></html>";
177 
178         final HtmlPage page = loadPage(html);
179 
180         final String xml = page.asXml();
181         assertTrue("Node not expanded in: " + xml, xml.contains("</button>"));
182     }
183 }