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