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