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.javascript.host.html;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.html.HtmlPage;
22  import org.htmlunit.html.HtmlTextInput;
23  import org.htmlunit.junit.annotation.Alerts;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for {@link HTMLInputElement} and buttons.
28   *
29   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
30   * @author Marc Guillemot
31   * @author Chris Erskine
32   * @author Ahmed Ashour
33   */
34  public class HTMLInputElement2Test extends SimpleWebTestCase {
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts({"hello", "me te"})
41      public void selectionRange() throws Exception {
42          final String html = DOCTYPE_HTML
43              + "<html><head><title>foo</title><script>\n"
44              + "function test() {\n"
45              + "  var input = document.getElementById('myInput');\n"
46              + "  input.setSelectionRange(2, 7);\n"
47              + "  alert('hello');\n"
48              + "}\n"
49              + "</script></head>\n"
50              + "<body onload='test()'>\n"
51              + "  <input id='myInput' value='some test'>\n"
52              + "</body></html>";
53  
54          final String[] expected = getExpectedAlerts();
55          setExpectedAlerts(new String[] {expected[0]});
56          final HtmlPage page = loadPageWithAlerts(html);
57          final HtmlTextInput input = page.getHtmlElementById("myInput");
58  
59          assertEquals(expected[1], input.getSelectedText());
60      }
61  
62      /**
63       * @throws Exception if an error occurs
64       */
65      @Test
66      @Alerts("initial")
67      public void focus() throws Exception {
68          final String html = DOCTYPE_HTML
69              + "<html><body>\n"
70              + "<iframe name='theFrame' src='" + URL_SECOND + "'></iframe>\n"
71              + "</body></html>";
72          final String frame = DOCTYPE_HTML
73              + "<html><body>\n"
74              + "<input id='input' value='initial' onfocus='alert(this.value)'>\n"
75              + "<div id='div'>click me</div>\n"
76              + "</body></html>";
77  
78          getMockWebConnection().setResponse(URL_SECOND, frame);
79  
80          final List<String> collectedAlerts = new ArrayList<>();
81          final HtmlPage page = loadPage(html, collectedAlerts);
82          final HtmlPage framePage = (HtmlPage) page.getFrames().get(0).getEnclosedPage();
83  
84          framePage.getHtmlElementById("input").type("foo");
85  
86          framePage.getHtmlElementById("div").click();
87          assertEquals(getExpectedAlerts(), collectedAlerts);
88      }
89  }