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 java.net.URL;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang3.ArrayUtils;
22  import org.htmlunit.WebDriverTestCase;
23  import org.htmlunit.junit.annotation.Alerts;
24  import org.htmlunit.junit.annotation.HtmlUnitNYI;
25  import org.htmlunit.util.MimeType;
26  import org.htmlunit.util.NameValuePair;
27  import org.junit.jupiter.api.Test;
28  import org.openqa.selenium.By;
29  import org.openqa.selenium.NoSuchElementException;
30  import org.openqa.selenium.WebDriver;
31  
32  /**
33   * Tests for {@link HTMLFrameElement} when used for {@link org.htmlunit.html.HtmlFrame}.
34   *
35   * @author Chris Erskine
36   * @author Marc Guillemot
37   * @author Thomas Robbs
38   * @author David K. Taylor
39   * @author Ahmed Ashour
40   * @author Ronald Brill
41   * @author Frank Danek
42   */
43  public class HTMLFrameElement2Test extends WebDriverTestCase {
44  
45      /**
46       * @throws Exception if the test fails
47       */
48      @Test
49      @Alerts("Frame2")
50      public void frameName() throws Exception {
51          final String html = DOCTYPE_HTML
52              + "<html><head>\n"
53              + "<script>\n"
54              + LOG_TITLE_FUNCTION
55              + "</script>\n"
56              + "</head>\n"
57              + "<frameset cols='20%,80%'>\n"
58              + "  <frame id='frame1'>\n"
59              + "  <frame name='Frame2' onload='log(this.name)' id='frame2'>\n"
60              + "</frameset></html>";
61  
62          loadPageVerifyTitle2(html);
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts({"[object HTMLDocument]", "true"})
70      public void contentDocument() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html>\n"
73              + "<head>\n"
74              + "  <script>\n"
75              + LOG_TITLE_FUNCTION
76              + "    function test() {\n"
77              + "      log(document.getElementById('myFrame').contentDocument);\n"
78              + "      log(document.getElementById('myFrame').contentDocument == frames.foo.document);\n"
79              + "    }\n"
80              + "  </script>\n"
81              + "</head>\n"
82              + "<frameset rows='*' onload='test()'>\n"
83              + "  <frame name='foo' id='myFrame' src='about:blank'/>\n"
84              + "</frameset>\n"
85              + "</html>";
86  
87          loadPageVerifyTitle2(html);
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts("true")
95      public void contentWindow() throws Exception {
96          final String html = DOCTYPE_HTML
97                  + "<html><head>\n"
98                  + "<script>\n"
99                  + LOG_TITLE_FUNCTION
100                 + "function test() {\n"
101                 + "  log(document.getElementById('myFrame').contentWindow == frames.foo);\n"
102                 + "}\n"
103                 + "</script>\n"
104                 + "</head>\n"
105                 + "<frameset rows='*' onload='test()'>\n"
106                 + "<frame name='foo' id='myFrame' src='about:blank'/>\n"
107                 + "</frameset>\n"
108                 + "</html>";
109 
110         loadPageVerifyTitle2(html);
111     }
112 
113     /**
114      * Regression test for Bug #259.
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"frame=OK", "frames.length=2", "frame=OK", "frames.length=0", "frame=OK", "frames.length=0"})
119     public void frameTag1192854() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html>\n"
122             + "<script>\n"
123             + LOG_TITLE_FUNCTION
124             + "var root=this;\n"
125             + "function listframes(frame) {\n"
126             + "  if (frame == null) {\n"
127             + "    log('frame=null');\n"
128             + "  } else {\n"
129             + "    log('frame=OK');\n"
130             + "    var len = frame.frames.length;\n"
131             + "    log('frames.length=' + len);\n"
132             + "    for (var i = 0; i < len; i++) {\n"
133             + "      listframes(frame.frames[i]);\n"
134             + "    }\n"
135             + "  }\n"
136             + "}\n"
137             + "document.write('<frameset id=\"frameset1\" "
138             + "rows=\"50,50\"><frame id=\"frame1-1\" "
139             + "src=\"about:blank\"><frame id=\"frame1-2\" "
140             + "src=\"about:blank\"></frameset>');\n"
141             + "listframes(root);\n"
142             + "</script>\n"
143             + "</html>";
144 
145         loadPageVerifyTitle2(html);
146     }
147 
148     /**
149      * @throws Exception if the test fails
150      */
151     @Test
152     @Alerts({"function handler() {}", "null", "null"})
153     public void onloadNull() throws Exception {
154         final String html = DOCTYPE_HTML
155             + "<html><head>\n"
156             + "<script>\n"
157             + LOG_TITLE_FUNCTION
158             + "  function handler() {}\n"
159             + "  function test() {\n"
160             + "    var iframe = document.getElementById('myFrame');\n"
161             + "    iframe.onload = handler;\n"
162             + "    log(iframe.onload);\n"
163             + "    iframe.onload = null;\n"
164             + "    log(iframe.onload);\n"
165             + "    try {\n"
166             + "      iframe.onload = undefined;\n"
167             + "      log(iframe.onload);\n"
168             + "    } catch(e) { logEx(e); }\n"
169             + "  }\n"
170             + "</script>\n"
171             + "<body onload=test()>\n"
172             + "  <iframe id='myFrame'></iframe>\n"
173             + "</body></html>";
174 
175         loadPageVerifyTitle2(html);
176     }
177 
178     /**
179      * Regression test for Bug #203.
180      * @throws Exception if the test fails
181      */
182     @Test
183     @Alerts({"§§URL§§subdir/frame.html", "§§URL§§frame.html"})
184     public void location() throws Exception {
185         location("Frame1.location = \"frame.html\"");
186         // location("Frame1.location.replace(\"frame.html\")");
187     }
188 
189     private void location(final String jsExpr) throws Exception {
190         final String firstContent = DOCTYPE_HTML
191             + "<html><head>\n"
192             + "<title>first</title>\n"
193             + "</head>\n"
194             + "<frameset cols='*' onload='" + jsExpr + "'>\n"
195             + "  <frame name='Frame1' src='subdir/frame.html'>\n"
196             + "</frameset></html>";
197 
198         final String defaultContent = DOCTYPE_HTML
199             + "<html><head>\n"
200             + "<script>\n"
201             + "function log(msg) { window.top.name += msg + '\\u00a7'; }\n"
202             + "log(location);\n"
203             + "</script>\n"
204             + "</head></html>";
205 
206         getMockWebConnection().setDefaultResponse(defaultContent);
207 
208         final WebDriver driver = loadPage2(firstContent);
209 
210         expandExpectedAlertsVariables(URL_FIRST);
211         verifyWindowName2(driver, getExpectedAlerts());
212 
213         assertTitle(driver, "first");
214     }
215 
216     /**
217      * Regression test for Bug #288.
218      * @throws Exception if the test fails
219      */
220     @Test
221     @Alerts("2")
222     public void writeFrameset() throws Exception {
223         final String content1 = DOCTYPE_HTML
224             + "<html><head>\n"
225             + "<script>\n"
226             + LOG_TITLE_FUNCTION
227             + "    document.write('<frameset>');\n"
228             + "    document.write('<frame src=\"page2.html\" name=\"leftFrame\">');\n"
229             + "    document.write('</frameset>');\n"
230             + "</script>\n"
231             + "</head></html>";
232         final String content2 = DOCTYPE_HTML + "<html><head><script>parent.log(2)</script></head></html>";
233 
234         getMockWebConnection().setDefaultResponse(content2);
235 
236         loadPageVerifyTitle2(content1);
237     }
238 
239     /**
240      * Regression test for Bug #307.
241      * @throws Exception if the test fails
242      */
243     @Test
244     @Alerts("DIV")
245     public void frameLoadedAfterParent() throws Exception {
246         final String html = DOCTYPE_HTML
247             + "<html><head>\n"
248             + "<script>\n"
249             + LOG_TITLE_FUNCTION
250             + "</script>\n"
251             + "</head><body>\n"
252             + "<iframe name='testFrame' src='testFrame.html'></iframe>\n"
253             + "<div id='aButton'>test text</div>\n"
254             + "</body></html>";
255         final String frameContent = DOCTYPE_HTML
256             + "<html><head></head><body>\n"
257             + "<script>\n"
258             + "parent.log(top.document.getElementById('aButton').tagName);\n"
259             + "</script>\n"
260             + "</body></html>";
261 
262         getMockWebConnection().setResponse(new URL(URL_FIRST, "testFrame.html"), frameContent);
263         loadPageVerifyTitle2(html);
264     }
265 
266     /**
267      * Illustrates problem of issue #729.
268      * See <a href="https://sourceforge.net/tracker/?func=detail&atid=448266&aid=2314485&group_id=47038">issue 729</a>
269      * @throws Exception if the test fails
270      */
271     @Test
272     @Alerts({"about:blank", "oFrame.foo: undefined", "/frame1.html", "oFrame.foo: foo of frame 1",
273              "/frame2.html", "oFrame.foo: foo of frame 2"})
274     public void changingFrameDocumentLocation() throws Exception {
275         final String firstHtml = DOCTYPE_HTML
276             + "<html><head>\n"
277             + "<script>\n"
278             + LOG_TITLE_FUNCTION
279             + "var oFrame;\n"
280             + "function init() {\n"
281             + "  oFrame = self.frames['theFrame'];\n"
282             + "}\n"
283             + "function test(fileName) {\n"
284             + "  if (oFrame.document.location == 'about:blank')\n" // to avoid different expectations for IE and FF
285             + "    log('about:blank');\n"
286             + "  else\n"
287             + "    log(oFrame.document.location.pathname);\n"
288             + "  log('oFrame.foo: ' + oFrame.foo);\n"
289             + "  oFrame.document.location.href = fileName;\n"
290             + "}\n"
291             + "</script>\n"
292             + "</head>\n"
293             + "<body onload='init()'>\n"
294             + "<iframe name='theFrame'></iframe>\n"
295             + "<button id='btn1' onclick='test(\"frame1.html\")'>load frame1</button>\n"
296             + "<button id='btn2' onclick='test(\"frame2.html\")'>load frame2</button>\n"
297             + "<button id='btn3' onclick='test(\"about:blank\")'>load about:blank</button>\n"
298             + "</body></html>";
299 
300         final String frame1Html = DOCTYPE_HTML
301             + "<html><head><title>frame 1</title>\n"
302             + "<script>var foo = 'foo of frame 1'</script></head>\n"
303             + "<body>frame 1</body></html>";
304         final String frame2Html = frame1Html.replaceAll("frame 1", "frame 2");
305 
306         getMockWebConnection().setResponse(new URL(URL_FIRST, "frame1.html"), frame1Html);
307         getMockWebConnection().setResponse(new URL(URL_FIRST, "frame2.html"), frame2Html);
308 
309         final String[] alerts = getExpectedAlerts();
310 
311         final WebDriver driver = loadPage2(firstHtml);
312         driver.findElement(By.id("btn1")).click();
313         verifyTitle2(driver, ArrayUtils.subarray(alerts, 0, 2));
314         driver.findElement(By.id("btn2")).click();
315         verifyTitle2(driver, ArrayUtils.subarray(alerts, 0, 4));
316         driver.findElement(By.id("btn3")).click();
317         verifyTitle2(driver, ArrayUtils.subarray(alerts, 0, 6));
318     }
319 
320     /**
321      * @throws Exception if the test fails
322      */
323     @Test
324     @Alerts("[object HTMLFrameElement]")
325     public void frames_framesetOnLoad() throws Exception {
326         final String mainHtml = DOCTYPE_HTML
327             + "<html><head>\n"
328             + "<script>\n"
329             + LOG_TITLE_FUNCTION
330             + "</script>"
331             + "</head>\n"
332             + "<frameset onload=\"log(window.frames['f1'])\">\n"
333             + "<frame id='f1' src='1.html'/>\n"
334             + "<frame id='f2' src='1.html'/>\n"
335             + "</frameset>\n"
336             + "</html>";
337 
338         final String frame1 = DOCTYPE_HTML
339             + "<html><head><title>1</title></head>\n"
340             + "<body></body>\n"
341             + "</html>";
342 
343         getMockWebConnection().setResponse(new URL(URL_FIRST, "1.html"), frame1);
344 
345         loadPageVerifyTitle2(mainHtml);
346     }
347 
348     /**
349      * @throws Exception if the test fails
350      */
351     @Test
352     @Alerts("[object HTMLFrameElement]")
353     public void frames_bodyOnLoad() throws Exception {
354         final String mainHtml = DOCTYPE_HTML
355             + "<html><head>\n"
356             + "<script>\n"
357             + LOG_TITLE_FUNCTION
358             + "</script>\n"
359             + "</head>\n"
360             + "<frameset>\n"
361             + "<frame id='f1' src='1.html'/>\n"
362             + "</frameset>\n"
363             + "</html>";
364 
365         final String frame1 = DOCTYPE_HTML
366             + "<html><head><title>1</title></head>\n"
367             + "<body onload=\"parent.log(parent.frames['f1'])\"></body>\n"
368             + "</html>";
369 
370         getMockWebConnection().setResponse(new URL(URL_FIRST, "1.html"), frame1);
371 
372         loadPageVerifyTitle2(mainHtml);
373     }
374 
375     /**
376      * @throws Exception if the test fails
377      */
378     @Test
379     @Alerts("[object HTMLFrameElement]")
380     public void parent_frames() throws Exception {
381         final String mainHtml = DOCTYPE_HTML
382             + "<html><head>\n"
383             + "<script>\n"
384             + LOG_TITLE_FUNCTION
385             + "</script>\n"
386             + "</head>\n"
387             + "<frameset>\n"
388             + "<frame id='f1' src='1.html'/>\n"
389             + "</frameset>\n"
390             + "</html>";
391 
392         final String frame1 = DOCTYPE_HTML
393             + "<html><head><title>f1</title>\n"
394             + "<script type='text/javascript'>\n"
395             + "  function test() {\n"
396             + "    parent.log(parent.frames['f1']);\n"
397             + "  }\n"
398             + "</script>\n"
399             + "</head>\n"
400             + "<body onload='test();'></body>\n"
401             + "</html>";
402 
403         getMockWebConnection().setResponse(new URL(URL_FIRST, "1.html"), frame1);
404 
405         loadPageVerifyTitle2(mainHtml);
406     }
407 
408     /**
409      * @throws Exception if the test fails
410      */
411     @Test
412     @Alerts("OnloadTest head bottom frameset")
413     public void onloadOrderRows() throws Exception {
414         final String html = DOCTYPE_HTML
415                 + "<html><head><title>OnloadTest</title></head>\n"
416                 + "<frameset rows='50%,*' onLoad='document.title += \" frameset\"'>\n"
417                 + "  <frame name='head' src='head.html'>\n"
418                 + "  <frame name='bottom' src='bottom.html'>\n"
419                 + "</frameset>\n"
420                 + "</html>";
421 
422         final String top = DOCTYPE_HTML
423                 + "<html><head><title>Head</title></head>\n"
424                 + "<body onload='top.document.title += \" head\"'>head</body>\n"
425                 + "</html>";
426         final String bottom = DOCTYPE_HTML
427                 + "<html><head><title>Bottom</title></head>\n"
428                 + "<body onload='top.document.title += \" bottom\"'>bottom</body>\n"
429                 + "</html>";
430 
431         getMockWebConnection().setResponse(new URL(URL_FIRST, "head.html"), top);
432         getMockWebConnection().setResponse(new URL(URL_FIRST, "bottom.html"), bottom);
433 
434         final WebDriver driver = loadPage2(html);
435         assertEquals(3, getMockWebConnection().getRequestCount());
436         assertTitle(driver, getExpectedAlerts()[0]);
437     }
438 
439     /**
440      * @throws Exception if the test fails
441      */
442     @Test
443     @Alerts("OnloadTest left right frameset")
444     public void onloadOrderCols() throws Exception {
445         final String html = DOCTYPE_HTML
446                 + "<html><head><title>OnloadTest</title></head>\n"
447                 + "<frameset cols='50%,*' onLoad='document.title += \" frameset\"'>\n"
448                 + "  <frame name='left' src='left.html'>\n"
449                 + "  <frame name='right' src='right.html'>\n"
450                 + "</frameset>\n"
451                 + "</html>";
452 
453         final String left = DOCTYPE_HTML
454                 + "<html><head><title>Left</title></head>\n"
455                 + "<body onload='top.document.title += \" left\"'>left</body>\n"
456                 + "</html>";
457         final String right = DOCTYPE_HTML
458                 + "<html><head><title>Right</title></head>\n"
459                 + "<body onload='top.document.title += \" right\"'>right</body>\n"
460                 + "</html>";
461 
462         getMockWebConnection().setResponse(new URL(URL_FIRST, "left.html"), left);
463         getMockWebConnection().setResponse(new URL(URL_FIRST, "right.html"), right);
464 
465         final WebDriver driver = loadPage2(html);
466         assertEquals(3, getMockWebConnection().getRequestCount());
467         assertTitle(driver, getExpectedAlerts()[0]);
468     }
469 
470     /**
471      * @throws Exception if the test fails
472      */
473     @Test
474     @Alerts({"OnloadTest", "header -> content -> frameSet",
475              "content\nClick for new frame content with onload",
476              "header -> content -> frameSet -> onloadFrame",
477              "onloadFrame\nNew content loaded..."})
478     public void windowLocationReplaceOnload() throws Exception {
479         final String html = DOCTYPE_HTML
480                 + "<html><head><title>OnloadTest</title></head>\n"
481                 + "<frameset rows='50,*' onLoad=\"top.header.addToFrameOrder('frameSet');\">\n"
482                 + "  <frame name='header' src='header.html'>\n"
483                 + "  <frame name='content' id='content' "
484                         + "src=\"javascript:window.location.replace('content.html')\">\n"
485                 + "</frameset>\n"
486                 + "</html>";
487 
488         final String headerFrame = DOCTYPE_HTML
489                 + "<html><head><title>headerFrame</title></head>\n"
490                 + "<script type='text/javascript'>\n"
491                 + "  function addToFrameOrder(frame) {\n"
492                 + "    var spacer = ' -> ';\n"
493                 + "    var frameOrder = document.getElementById('frameOrder').innerHTML;\n"
494                 + "    if (frameOrder == '') {spacer = '';}\n"
495                 + "    document.getElementById('frameOrder').innerHTML = frameOrder + spacer + frame;\n"
496                 + "  }\n"
497                 + "</script>\n"
498                 + "<body onload=\"addToFrameOrder('header');\">\n"
499                 + "  <div id=\"frameOrder\"></div>\n"
500                 + "</body></html>";
501 
502         final String contentFrame = DOCTYPE_HTML
503                 + "<html><head><title>contentFrame</title></head>\n"
504                 + "<body onload=\"top.header.addToFrameOrder('content');\">\n"
505                 + "  <h3>content</h3>\n"
506                 + "  <a name='onloadFrameAnchor' href='onload.html' "
507                         + "target='content'>Click for new frame content with onload</a>\n"
508                 + "</body></html>";
509 
510         final String onloadFrame = DOCTYPE_HTML
511                 + "<html><head>\n"
512                 + "<title>onloadFrame</title>\n"
513                 + "<script>\n"
514                 + "  function log(msg) { window.top.name += msg + '\\u00a7'; }\n"
515                 + "</script>\n"
516                 + "</head>\n"
517                 + "<body onload=\"log('Onload alert.');top.header.addToFrameOrder('onloadFrame');\">\n"
518                 + "  <script type='text/javascript'>\n"
519                 + "    log('Body alert.');\n"
520                 + "  </script>\n"
521                 + "  <h3>onloadFrame</h3>\n"
522                 + "  <p id='newContent'>New content loaded...</p>\n"
523                 + "</body></html>";
524 
525         getMockWebConnection().setResponse(new URL(URL_FIRST, "header.html"), headerFrame);
526         getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), contentFrame);
527         getMockWebConnection().setResponse(new URL(URL_FIRST, "onload.html"), onloadFrame);
528 
529         final WebDriver driver = loadPage2(html);
530         // top frame
531         assertTitle(driver, getExpectedAlerts()[0]);
532 
533         // header frame
534         driver.switchTo().frame("header");
535         assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("frameOrder")).getText());
536 
537         // content frame
538         driver.switchTo().defaultContent();
539         driver.switchTo().frame("content");
540         assertEquals(getExpectedAlerts()[2], driver.findElement(By.tagName("body")).getText());
541 
542         driver.findElement(By.name("onloadFrameAnchor")).click();
543         verifyWindowName2(driver, "Body alert.", "Onload alert.");
544 
545         driver.switchTo().defaultContent();
546         Thread.sleep(1000);
547 
548         driver.switchTo().frame("header");
549         assertEquals(getExpectedAlerts()[3], driver.findElement(By.id("frameOrder")).getText());
550 
551         driver.switchTo().defaultContent();
552         driver.switchTo().frame("content");
553         assertEquals(getExpectedAlerts()[4], driver.findElement(By.tagName("body")).getText());
554     }
555 
556     /**
557      * @throws Exception if the test fails
558      */
559     @Test
560     @Alerts({"OnloadTest", "header -> content -> frameSet",
561              "content\nClick for new frame content with onload",
562              "header -> content -> frameSet -> onloadFrame",
563              "onloadFrame\nNew content loaded..."})
564     public void windowLocationAssignOnload() throws Exception {
565         final String html = DOCTYPE_HTML
566                 + "<html><head><title>OnloadTest</title></head>\n"
567                 + "<frameset rows='50,*' onLoad=\"top.header.addToFrameOrder('frameSet');\">\n"
568                 + "  <frame name='header' src='header.html'>\n"
569                 + "  <frame name='content' id='content' "
570                         + "src=\"javascript:window.location.assign('content.html')\">\n"
571                 + "</frameset>\n"
572                 + "</html>";
573 
574         final String headerFrame = DOCTYPE_HTML
575                 + "<html><head><title>headerFrame</title></head>\n"
576                 + "<script type='text/javascript'>\n"
577                 + "  function addToFrameOrder(frame) {\n"
578                 + "    var spacer = ' -> ';\n"
579                 + "    var frameOrder = document.getElementById('frameOrder').innerHTML;\n"
580                 + "    if (frameOrder == '') {spacer = '';}\n"
581                 + "    document.getElementById('frameOrder').innerHTML = frameOrder + spacer + frame;\n"
582                 + "  }\n"
583                 + "</script>\n"
584                 + "<body onload=\"addToFrameOrder('header');\">\n"
585                 + "  <div id='frameOrder'></div>\n"
586                 + "</body></html>";
587 
588         final String contentFrame = DOCTYPE_HTML
589                 + "<html><head><title>contentFrame</title></head>\n"
590                 + "<body onload=\"top.header.addToFrameOrder('content');\">\n"
591                 + "  <h3>content</h3>\n"
592                 + "  <a name='onloadFrameAnchor' href='onload.html' "
593                         + "target='content'>Click for new frame content with onload</a>\n"
594                 + "</body></html>";
595 
596         final String onloadFrame = DOCTYPE_HTML
597                 + "<html><head>\n"
598                 + "<title>onloadFrame</title>\n"
599                 + "<script>\n"
600                 + "  function log(msg) { window.top.name += msg + '\\u00a7'; }\n"
601                 + "</script>\n"
602                 + "</head>\n"
603                 + "<body onload=\"log('Onload alert.');top.header.addToFrameOrder('onloadFrame');\">\n"
604                 + "  <script type='text/javascript'>\n"
605                 + "    log('Body alert.');\n"
606                 + "  </script>\n"
607                 + "  <h3>onloadFrame</h3>\n"
608                 + "  <p id='newContent'>New content loaded...</p>\n"
609                 + "</body></html>";
610 
611         getMockWebConnection().setResponse(new URL(URL_FIRST, "header.html"), headerFrame);
612         getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), contentFrame);
613         getMockWebConnection().setResponse(new URL(URL_FIRST, "onload.html"), onloadFrame);
614 
615         final WebDriver driver = loadPage2(html);
616         // top frame
617         assertTitle(driver, getExpectedAlerts()[0]);
618 
619         // header frame
620         driver.switchTo().frame("header");
621         assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("frameOrder")).getText());
622 
623         // content frame
624         driver.switchTo().defaultContent();
625         driver.switchTo().frame("content");
626         assertEquals(getExpectedAlerts()[2], driver.findElement(By.tagName("body")).getText());
627 
628         driver.findElement(By.name("onloadFrameAnchor")).click();
629         verifyWindowName2(driver, "Body alert.", "Onload alert.");
630 
631         driver.switchTo().defaultContent();
632         Thread.sleep(1000);
633 
634         driver.switchTo().frame("header");
635         assertEquals(getExpectedAlerts()[3], driver.findElement(By.id("frameOrder")).getText());
636 
637         driver.switchTo().defaultContent();
638         driver.switchTo().frame("content");
639         assertEquals(getExpectedAlerts()[4], driver.findElement(By.tagName("body")).getText());
640     }
641 
642     /**
643      * @throws Exception if the test fails
644      */
645     @Test
646     @Alerts({"OnloadTest", "header -> content -> frameSet",
647              "content\nClick for new frame content with onload",
648              "header -> content -> frameSet -> onloadFrame",
649              "onloadFrame\nNew content loaded..."})
650     @HtmlUnitNYI(CHROME = {"OnloadTest", "header -> frameSet", "content.html",
651                            "Element named 'onloadFrameAnchor' not found."},
652             EDGE = {"OnloadTest", "header -> frameSet", "content.html",
653                     "Element named 'onloadFrameAnchor' not found."},
654             FF = {"OnloadTest", "header -> frameSet", "content.html",
655                   "Element named 'onloadFrameAnchor' not found."},
656             FF_ESR = {"OnloadTest", "header -> frameSet", "content.html",
657                       "Element named 'onloadFrameAnchor' not found."})
658     public void windowLocationSetOnload() throws Exception {
659         final String html = DOCTYPE_HTML
660                 + "<html><head><title>OnloadTest</title></head>\n"
661                 + "<frameset rows='50,*' onLoad=\"top.header.addToFrameOrder('frameSet');\">\n"
662                 + "  <frame name='header' src='header.html'>\n"
663                 + "  <frame name='content' id='content' "
664                         + "src=\"javascript:window.location='content.html'\">\n"
665                 + "</frameset>\n"
666                 + "</html>";
667 
668         final String headerFrame = DOCTYPE_HTML
669                 + "<html><head><title>headerFrame</title></head>\n"
670                 + "<script type='text/javascript'>\n"
671                 + "  function addToFrameOrder(frame) {\n"
672                 + "    var spacer = ' -> ';\n"
673                 + "    var frameOrder = document.getElementById('frameOrder').innerHTML;\n"
674                 + "    if (frameOrder == '') {spacer = '';}\n"
675                 + "    document.getElementById('frameOrder').innerHTML = frameOrder + spacer + frame;\n"
676                 + "  }\n"
677                 + "</script>\n"
678                 + "<body onload=\"addToFrameOrder('header');\">\n"
679                 + "  <div id='frameOrder'></div>\n"
680                 + "</body></html>";
681 
682         final String contentFrame = DOCTYPE_HTML
683                 + "<html><head><title>contentFrame</title></head>\n"
684                 + "<body onload=\"top.header.addToFrameOrder('content');\">\n"
685                 + "  <h3>content</h3>\n"
686                 + "  <a name='onloadFrameAnchor' href='onload.html' "
687                         + "target='content'>Click for new frame content with onload</a>\n"
688                 + "</body></html>";
689 
690         final String onloadFrame = DOCTYPE_HTML
691                 + "<html><head><title>onloadFrame</title></head>\n"
692                 + "<body onload=\"top.header.addToFrameOrder('onloadFrame');\">\n"
693                 + "  <h3>onloadFrame</h3>\n"
694                 + "  <p id='newContent'>New content loaded...</p>\n"
695                 + "</body></html>";
696 
697         getMockWebConnection().setResponse(new URL(URL_FIRST, "header.html"), headerFrame);
698         getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), contentFrame);
699         getMockWebConnection().setResponse(new URL(URL_FIRST, "onload.html"), onloadFrame);
700 
701         final WebDriver driver = loadPage2(html);
702         // top frame
703         assertTitle(driver, getExpectedAlerts()[0]);
704 
705         // header frame
706         driver.switchTo().frame("header");
707         assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("frameOrder")).getText());
708 
709         // content frame
710         driver.switchTo().defaultContent();
711         driver.switchTo().frame("content");
712         assertEquals(getExpectedAlerts()[2], driver.findElement(By.tagName("body")).getText());
713 
714         try {
715             driver.findElement(By.name("onloadFrameAnchor")).click();
716             driver.switchTo().defaultContent();
717             driver.switchTo().frame("header");
718             assertEquals(getExpectedAlerts()[3], driver.findElement(By.id("frameOrder")).getText());
719 
720             driver.switchTo().defaultContent();
721             driver.switchTo().frame("content");
722             assertEquals(getExpectedAlerts()[4], driver.findElement(By.tagName("body")).getText());
723         }
724         catch (final NoSuchElementException e) {
725             assertEquals(getExpectedAlerts()[3], "Element named 'onloadFrameAnchor' not found.");
726         }
727     }
728 
729     /**
730      * @throws Exception if the test fails
731      */
732     @Test
733     @Alerts({"localhost", "localhost", "localhost", "localhost"})
734     public void domain() throws Exception {
735         final String html = DOCTYPE_HTML
736                 + "<html>\n"
737                 + "<head>\n"
738                 + "  <script>\n"
739                 + LOG_TITLE_FUNCTION
740                 + "    function doTest() {\n"
741                 + "      log(document.domain);\n"
742                 + "      log(document.getElementById('left').contentWindow.document.domain);\n"
743                 + "      log(document.getElementById('center').contentWindow.document.domain);\n"
744                 + "      log(document.getElementById('right').contentWindow.document.domain);\n"
745                 + "    }\n"
746                 + "  </script>\n"
747                 + "</head>\n"
748                 + "<frameset cols='33%,33%,*' onLoad='doTest()'>\n"
749                 + "  <frame name='left' id='left' >\n"
750                 + "  <frame name='center' id='center' src='about:blank'>\n"
751                 + "  <frame name='right' id='right' src='left.html'>\n"
752                 + "</frameset>\n"
753                 + "</html>";
754 
755         final String left = DOCTYPE_HTML
756                 + "<html><head><title>Left</title></head>\n"
757                 + "<body>left</body>\n"
758                 + "</html>";
759 
760         getMockWebConnection().setResponse(new URL(URL_FIRST, "left.html"), left);
761 
762         loadPageVerifyTitle2(html);
763         assertEquals(2, getMockWebConnection().getRequestCount());
764     }
765 
766     /**
767      * @throws Exception if the test fails
768      */
769     @Test
770     @Alerts({"loaded", "null"})
771     public void deny() throws Exception {
772         final String html = DOCTYPE_HTML
773             + "<html>\n"
774             + "<head>\n"
775             + "  <script>\n"
776             + LOG_TITLE_FUNCTION
777             + "    function check() {\n"
778             + "      try {\n"
779             + "        log(document.getElementById(\"frame1\").contentDocument);\n"
780             + "      } catch(e) { logEx(e); }\n"
781             + "    }\n"
782             + "  </script>\n"
783             + "</head>\n"
784             + "<frameset cols='42%' >\n"
785             + "  <frame name='frame1' id='frame1' src='content.html' "
786                       + "onLoad='log(\"loaded\");check()' onError='log(\"error\")'>\n"
787             + "</frameset>\n"
788             + "</html>";
789 
790         final String left = DOCTYPE_HTML
791                 + "<html><head><title>Frame Title</title></head>\n"
792                 + "<body>Frame Content</body>\n"
793                 + "</html>";
794 
795         final List<NameValuePair> headers = new ArrayList<>();
796         headers.add(new NameValuePair("X-Frame-Options", "DENY"));
797 
798         getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), left,
799                 200, "OK", MimeType.TEXT_HTML, headers);
800 
801         loadPageVerifyTitle2(html);
802         assertEquals(2, getMockWebConnection().getRequestCount());
803     }
804 }