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