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 org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link HTMLBaseElement}.
25   *
26   * @author Daniel Gredler
27   * @author Ahmed Ashour
28   */
29  @RunWith(BrowserRunner.class)
30  public class HTMLBaseElementTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if an error occurs
34       */
35      @Test
36      @Alerts({"http://www.foo.com/images/", "§§URL§§", "", "_blank"})
37      public void hrefAndTarget() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html>\n"
40              + "  <head>\n"
41              + "    <base id='b1' href='http://www.foo.com/images/' />\n"
42              + "    <base id='b2' target='_blank' />\n"
43              + "    <script>\n"
44              + LOG_TITLE_FUNCTION
45              + "      function test() {\n"
46              + "        log(document.getElementById('b1').href);\n"
47              + "        log(document.getElementById('b2').href);\n"
48              + "        log(document.getElementById('b1').target);\n"
49              + "        log(document.getElementById('b2').target);\n"
50              + "      }\n"
51              + "    </script>\n"
52              + "  </head>\n"
53              + "  <body onload='test()'>foo</body>\n"
54              + "</html>";
55  
56          expandExpectedAlertsVariables(URL_FIRST);
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts({"[object HTMLBaseElement]", "function HTMLBaseElement() { [native code] }"})
65      public void type() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html><head>\n"
68              + "<script>\n"
69              + LOG_TITLE_FUNCTION
70              + "  function test() {\n"
71              + "  var elem = document.getElementById('b1');\n"
72              + "    try {\n"
73              + "      log(elem);\n"
74              + "      log(HTMLBaseElement);\n"
75              + "    } catch(e) { logEx(e); }\n"
76              + "  }\n"
77              + "</script>\n"
78              + "</head>\n"
79              + "<body onload='test()'>\n"
80              + "  <base id='b1' href='http://somehost/images/' />\n"
81              + "</body></html>";
82  
83          loadPageVerifyTitle2(html);
84      }
85  }