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.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  import org.openqa.selenium.WebDriver;
23  
24  /**
25   * Tests for {@link BaseFrameElement}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class BaseFrameElement2Test extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("posted called")
38      public void windowEventListenersContainer() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html>\n"
41              + "<head>\n"
42              + "<script>\n"
43              + "  function test() {\n"
44              + "    var iframe = document.createElement('iframe');\n"
45              + "    iframe.src = '';\n"
46              + "    document.documentElement.appendChild(iframe);\n"
47              + "    var win = iframe.contentWindow;\n"
48              + "    win.addEventListener('message', handler);\n"
49              + "    win.postMessage('hello', '*');\n"
50              + "    document.title += ' posted';\n"
51              + "  }\n"
52              + "  function handler() {\n"
53              + "    document.title += ' called';\n"
54              + "  }\n"
55              + "</script>\n"
56              + "</head>\n"
57              + "<body onload='test()'>\n"
58              + "</body></html>";
59  
60          final WebDriver driver = loadPage2(html);
61          assertTitle(driver, getExpectedAlerts()[0]);
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      public void doNotLoopEndless() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html>\n"
71              + "<head>\n"
72              + "</head>\n"
73              + "<body>\n"
74              + "  <iframe src='&#8221'></iframe>\n"
75              + "</body></html>";
76  
77          getMockWebConnection().setDefaultResponse(html);
78  
79          loadPage2(html);
80      }
81  }