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;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.htmlunit.html.HtmlPage;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link HtmlUnitScriptable}.
24   *
25   * @author Mike Bowler
26   * @author Barnaby Court
27   * @author David K. Taylor
28   * @author Ben Curren
29   * @author Marc Guillemot
30   * @author Chris Erskine
31   * @author Ahmed Ashour
32   * @author Sudhan Moghe
33   * @author Mike Dirolf
34   * @author Frank Danek
35   */
36  public class HtmlUnitScriptableTest extends SimpleWebTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts("past focus")
43      public void callInheritedFunction() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head><title>foo</title><script>\n"
46              + "function doTest() {\n"
47              + "  document.form1.textfield1.focus();\n"
48              + "  alert('past focus');\n"
49              + "}\n"
50              + "</script></head><body onload='doTest()'>\n"
51              + "<p>hello world</p>\n"
52              + "<form name='form1'>\n"
53              + "  <input type='text' name='textfield1' id='textfield1' value='foo'/>\n"
54              + "</form>\n"
55              + "</body></html>";
56  
57          final HtmlPage page = loadPageWithAlerts(html);
58          assertEquals("foo", page.getTitleText());
59          assertSame("focus not changed to textfield1",
60                       page.getFormByName("form1").getInputByName("textfield1"),
61                       page.getFocusedElement());
62      }
63  }