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 java.net.URL;
18  
19  import org.htmlunit.MockWebConnection;
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.htmlunit.junit.annotation.HtmlUnitNYI;
23  import org.junit.jupiter.api.Test;
24  import org.openqa.selenium.By;
25  import org.openqa.selenium.WebDriver;
26  
27  /**
28   * Tests for {@link HtmlFrame}.
29   *
30   * @author Ahmed Ashour
31   * @author Marc Guillemot
32   * @author Frank Danek
33   * @author Ronald Brill
34   */
35  public class HtmlFrame2Test extends WebDriverTestCase {
36  
37      /**
38       * @throws Exception if an error occurs
39       */
40      @Test
41      @Alerts("2")
42      public void crossFrameJavascript() throws Exception {
43          final String firstHtml = DOCTYPE_HTML
44              + "<html><body>\n"
45              + "<script>function render() { window.bar.real_render(); }</script>\n"
46              + "<iframe src='" + URL_SECOND + "' onload='render();' name='bar'></iframe>\n"
47              + "</body></html>";
48  
49          final String secondHtml = DOCTYPE_HTML
50              + "<html><body>\n"
51              + "<script>"
52              + LOG_WINDOW_NAME_FUNCTION
53              + "function real_render() { log(2); }</script>\n"
54              + "</body></html>";
55  
56          getMockWebConnection().setResponse(URL_SECOND, secondHtml);
57          loadPage2(firstHtml);
58          verifyWindowName2(getWebDriver(), getExpectedAlerts());
59      }
60  
61      /**
62       * Regression test for for bug
63       * <a href="http://sf.net/support/tracker.php?aid=2819477">2819477</a>: onload handler should be called only one
64       * time!
65       * @throws Exception if an error occurs
66       */
67      @Test
68      @Alerts(DEFAULT = "1",
69              CHROME = {"2", "1", "1"},
70              EDGE = {"2", "1", "1"})
71      @HtmlUnitNYI(CHROME = "1",
72              EDGE = "1")
73      public void iframeOnloadCalledOnlyOnce() throws Exception {
74          final String firstHtml = DOCTYPE_HTML
75              + "<html><body>\n"
76              + "<iframe src='" + URL_SECOND + "' onload='window.top.name += \"1\\u00a7\"'></iframe>\n"
77              + "</body></html>";
78  
79          getMockWebConnection().setResponse(URL_SECOND, "");
80          loadPage2(firstHtml);
81          verifyWindowName2(getWebDriver(), getExpectedAlerts());
82      }
83  
84      /**
85       * URL {@code about:blank} has a special meaning as it is what is loaded in the frame
86       * before the real content is loaded.
87       * @throws Exception if an error occurs
88       */
89      @Test
90      @Alerts(DEFAULT = {"2", "1"},
91              FF = "1",
92              FF_ESR = "1")
93      @HtmlUnitNYI(CHROME = "1",
94              EDGE = "1")
95      public void iframeOnloadAboutBlank() throws Exception {
96          final String html = DOCTYPE_HTML
97              + "<html><body>\n"
98              + "<iframe src='about:blank' onload='window.top.name += \"1\\u00a7\"'></iframe>\n"
99              + "</body></html>";
100 
101         loadPage2(html);
102         verifyWindowName2(getWebDriver(), getExpectedAlerts());
103     }
104 
105     /**
106      * Regression test for for bug
107      * <a href="http://sourceforge.net/p/htmlunit/bugs/925/">925</a>.
108      * @throws Exception if an error occurs
109      */
110     @Test
111     @Alerts("second [object HTMLFormElement] third [object HTMLFormElement] "
112                         + "parent [object HTMLFormElement]")
113     // real FF/CHROME sometimes alerts 'third' before 'second'
114     public void postponeLoading() throws Exception {
115         final String html = "<FRAMESET rows='50%,*' "
116                 + "onload=\"document.title += ' parent ' + window.parent.frames['third'].document.frm\">\n"
117             + "  <FRAME name='second' frameborder='0' src='second.html'>\n"
118             + "  <FRAME name='third' frameborder='0' src='third.html'>\n"
119             + "</FRAMESET>";
120 
121         final String secondHtml = DOCTYPE_HTML
122             + "<html>\n"
123             + "<body onload=\"top.document.title += ' second ' + window.parent.frames['third'].document.frm\">\n"
124             + "  <h1>second</h1>\n"
125             + "</body></html>";
126 
127         final String thirdHtml = DOCTYPE_HTML
128             + "<html>\n"
129             + "<body onload=\"top.document.title += ' third ' + window.parent.frames['third'].document.frm\">\n"
130             + "  <form name='frm' id='frm'>\n"
131             + "    <input type='text' id='one' name='one' value='something'>\n"
132             + "  </form>\n"
133             + "</body></html>";
134 
135         getMockWebConnection().setResponse(new URL(URL_FIRST, "second.html"), secondHtml);
136         getMockWebConnection().setResponse(new URL(URL_FIRST, "third.html"), thirdHtml);
137 
138         final WebDriver driver = loadPage2(html);
139         assertEquals(3, getMockWebConnection().getRequestCount());
140         assertTitle(driver, getExpectedAlerts()[0]);
141     }
142 
143     /**
144      * @throws Exception if an error occurs
145      */
146     @Test
147     @Alerts("second third first")
148     public void frameOnload() throws Exception {
149         final String html = "<FRAMESET rows='50%,50%' onload=\"document.title += ' first'\">\n"
150             + "  <FRAME name='second' src='second.html'>\n"
151             + "  <FRAME name='third' src='third.html'>\n"
152             + "</FRAMESET>";
153 
154         final String secondHtml = DOCTYPE_HTML
155             + "<html>\n"
156             + "<body onload=\"top.document.title += ' second'\">\n"
157             + "  <h1>second</h1>\n"
158             + "</body></html>";
159 
160         final String thirdHtml = DOCTYPE_HTML
161                 + "<html>\n"
162                 + "<body onload=\"top.document.title += ' third'\">\n"
163                 + "  <h1>third</h1>\n"
164                 + "</body></html>";
165 
166         getMockWebConnection().setResponse(new URL(URL_FIRST, "second.html"), secondHtml);
167         getMockWebConnection().setResponse(new URL(URL_FIRST, "third.html"), thirdHtml);
168 
169         final WebDriver driver = loadPage2(html);
170         assertEquals(3, getMockWebConnection().getRequestCount());
171         assertTitle(driver, getExpectedAlerts()[0]);
172     }
173 
174     /**
175      * @throws Exception if an error occurs
176      */
177     @Test
178     @Alerts("second fourth third first")
179     @HtmlUnitNYI(CHROME = "fourth second third first",
180             EDGE = "fourth second third first",
181             FF = "fourth second third first",
182             FF_ESR = "fourth second third first")
183     public void frameOnloadFrameInFrame() throws Exception {
184         final String html = "<FRAMESET rows='50%,50%' onload=\"document.title += ' first'\">\n"
185             + "  <FRAME name='second' src='second.html'>\n"
186             + "  <FRAME name='third' src='third.html'>\n"
187             + "</FRAMESET>";
188 
189         final String secondHtml = DOCTYPE_HTML
190             + "<html>\n"
191             + "<body onload=\"top.document.title += ' second'\">\n"
192             + "  <h1>second</h1>\n"
193             + "</body></html>";
194 
195         final String thirdHtml = "<FRAMESET cols='100%' onload=\"top.document.title += ' third'\">\n"
196                 + "  <FRAME name='fourth' src='fourth.html'>\n"
197                 + "</FRAMESET>";
198 
199         final String fourthHtml = DOCTYPE_HTML
200             + "<html>\n"
201             + "<body onload=\"top.document.title += ' fourth'\">\n"
202             + "  <h1>fourth</h1>\n"
203             + "</body></html>";
204 
205         getMockWebConnection().setResponse(new URL(URL_FIRST, "second.html"), secondHtml);
206         getMockWebConnection().setResponse(new URL(URL_FIRST, "third.html"), thirdHtml);
207         getMockWebConnection().setResponse(new URL(URL_FIRST, "fourth.html"), fourthHtml);
208 
209         final WebDriver driver = loadPage2(html);
210         assertEquals(4, getMockWebConnection().getRequestCount());
211         assertTitle(driver, getExpectedAlerts()[0]);
212     }
213 
214     /**
215      * Another regression test for for bug
216      * <a href="http://sf.net/support/tracker.php?aid=2819477">2819477</a>: frame content
217      * sometimes not loaded.
218      * @throws Exception if an error occurs
219      */
220     @Test
221     @Alerts("myInputName")
222     public void iframeContentNotLoaded() throws Exception {
223         final String html = DOCTYPE_HTML
224             + "<html>\n"
225             + "<head><title>FooBar</title></head>\n"
226             + "<body>\n"
227             + "<iframe id='myFrame' name='myFrame'></iframe>\n"
228 
229             + "<script type='text/javascript'>\n"
230             + "function writeForm(frame) {\n"
231             + "  var div=frame.document.getElementById('div');\n"
232             + "  div.innerHTML = \"<form id='myForm'><input type='text' id='myInput' name='myInputName'/></form>\";\n"
233             + "}\n"
234 
235             + "function writeFrame(frame) {\n"
236             + "  var frameHtml=\"<html><head><title>Inner Frame</title></head>"
237             + "<body><div id='div'></div></body></html>\";\n"
238             + "  frame.document.write(frameHtml);\n"
239             + "  frame.document.close();\n"
240             + "}\n"
241 
242             + "function loadFrame() {\n"
243             + "  var frame=parent.frames['myFrame'];\n"
244             + "  writeFrame(frame);\n"
245             + "  writeForm(frame);\n"
246             + "  alert(frame.document.getElementById('myInput').name);\n"
247             + "}\n"
248 
249             + "loadFrame();\n"
250             + "</script>\n"
251 
252             + "</body>\n"
253             + "</html>";
254 
255         loadPageWithAlerts2(html);
256     }
257 
258     /**
259      * @see <a href="sourceforge.net/p/htmlunit/bugs/1443/">Bug #1443</a>
260      * @throws Exception if the test fails
261      */
262     @Test
263     @Alerts("foo")
264     public void onloadInNavigatedFrame() throws Exception {
265         final String html = DOCTYPE_HTML
266             + "<html><head><title>first</title></head>\n"
267             + "<frameset cols='20%,80%'>\n"
268             + "  <frame src='frame1.html' id='frame1'>\n"
269             + "</frameset></html>";
270 
271         final String firstHtml = DOCTYPE_HTML
272                 + "<html><body>\n"
273                 + "<a id='a1' href='frame2.html'>hello</a>\n"
274                 + "</body></html>";
275 
276         final String secondHtml = DOCTYPE_HTML + "<html><body onload='alert(\"foo\")'></body></html>";
277 
278         final MockWebConnection webConnection = getMockWebConnection();
279         webConnection.setResponse(new URL(URL_FIRST, "frame1.html"), firstHtml);
280         webConnection.setResponse(new URL(URL_FIRST, "frame2.html"), secondHtml);
281 
282         final WebDriver driver = loadPage2(html);
283         driver.switchTo().frame(0);
284 
285         driver.findElement(By.id("a1")).click();
286 
287         verifyAlerts(driver, getExpectedAlerts());
288     }
289 
290     /**
291      * @throws Exception if the test fails
292      */
293     @Test
294     @Alerts("loaded")
295     public void lineBreaksInUrl() throws Exception {
296         final String html = DOCTYPE_HTML
297             + "<html><head>\n"
298             + "</head>\n"
299             + "<body onload='alert(\"loaded\");'>Test</body>\n"
300             + "</html>";
301 
302         getMockWebConnection().setResponse(new URL(URL_SECOND, "abcd"), html);
303         expandExpectedAlertsVariables(URL_SECOND);
304 
305         final String frame = DOCTYPE_HTML
306             + "<html>\n"
307             + "  <frameset cols='100%'>\n"
308             + "    <frame src='" + URL_SECOND + "a\rb\nc\r\nd' id='myFrame'>\n"
309             + "  </frameset></html>";
310 
311         expandExpectedAlertsVariables(URL_SECOND);
312         loadPageWithAlerts2(frame);
313     }
314 
315     /**
316      * @throws Exception if the test fails
317      */
318     @Test
319     @Alerts("loaded")
320     public void lineBreaksInUrlIFrame() throws Exception {
321         final String html = DOCTYPE_HTML
322             + "<html><head>\n"
323             + "</head>\n"
324             + "<body onload='alert(\"loaded\");'>Test</body>\n"
325             + "</html>";
326 
327         getMockWebConnection().setResponse(new URL(URL_SECOND, "abcd"), html);
328         expandExpectedAlertsVariables(URL_SECOND);
329 
330         final String frame = DOCTYPE_HTML
331             + "<html>\n"
332             + "  <body>\n"
333             + "    <iframe src='" + URL_SECOND + "a\rb\nc\r\nd' id='myFrame'></iframe>\n"
334             + "  </body>\n"
335             + "</html>";
336 
337         expandExpectedAlertsVariables(URL_SECOND);
338         loadPageWithAlerts2(frame);
339     }
340 }