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