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.annotation.Alerts;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link HtmlButtonInput}.
26   *
27   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
28   * @author Ronald Brill
29   */
30  public class HtmlButtonInputTest extends SimpleWebTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("foo")
37      public void click_onClick() throws Exception {
38          final String htmlContent = DOCTYPE_HTML
39              + "<html>\n"
40              + "<head></head>\n"
41              + "<body>\n"
42              + "<form id='form1' onSubmit='alert(\"bar\")'>\n"
43              + "  <input type='button' name='button' id='button' onClick='alert(\"foo\")'>Push me</button>\n"
44              + "</form>\n"
45              + "</body></html>";
46  
47          final List<String> collectedAlerts = new ArrayList<>();
48          final HtmlPage page = loadPage(htmlContent, collectedAlerts);
49          final HtmlButtonInput button = page.getHtmlElementById("button");
50  
51          final HtmlPage secondPage = button.click();
52  
53          assertEquals(getExpectedAlerts(), collectedAlerts);
54  
55          assertSame(page, secondPage);
56      }
57  }