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