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