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  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.htmlunit.CollectingAlertHandler;
23  import org.htmlunit.HttpHeader;
24  import org.htmlunit.MockWebConnection;
25  import org.htmlunit.SimpleWebTestCase;
26  import org.htmlunit.WebClient;
27  import org.htmlunit.WebWindow;
28  import org.htmlunit.junit.BrowserRunner;
29  import org.htmlunit.junit.annotation.Alerts;
30  import org.htmlunit.junit.annotation.HtmlUnitNYI;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  
34  /**
35   * Tests for {@link HtmlFrameSet}.
36   *
37   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
38   * @author Marc Guillemot
39   * @author Hans Donner
40   * @author Ahmed Ashour
41   * @author Ronald Brill
42   */
43  @RunWith(BrowserRunner.class)
44  public class HtmlFrameSetTest extends SimpleWebTestCase {
45  
46      /**
47       * @throws Exception if the test fails
48       */
49      @Test
50      public void loadingFrameSet() throws Exception {
51          final String firstContent = DOCTYPE_HTML
52              + "<html><head><title>First</title></head>\n"
53              + "<frameset cols='130,*'>\n"
54              + "  <frame scrolling='no' name='left' src='" + URL_SECOND + "' frameborder='1' />\n"
55              + "  <frame scrolling='auto' name='right' src='" + URL_THIRD + "' frameborder='1' />\n"
56              + "  <noframes>\n"
57              + "    <body>Frames not supported</body>\n"
58              + "  </noframes>\n"
59              + "</frameset>\n"
60              + "</html>";
61          final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
62          final String thirdContent  = DOCTYPE_HTML + "<html><head><title>Third</title></head><body></body></html>";
63  
64          final WebClient webClient = getWebClientWithMockWebConnection();
65  
66          final MockWebConnection webConnection = getMockWebConnection();
67          webConnection.setResponse(URL_FIRST, firstContent);
68          webConnection.setResponse(URL_SECOND, secondContent);
69          webConnection.setResponse(URL_THIRD, thirdContent);
70  
71          final HtmlPage firstPage = webClient.getPage(URL_FIRST);
72          assertEquals("First", firstPage.getTitleText());
73  
74          final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
75          assertSame(firstPage, ((FrameWindow) secondWebWindow).getEnclosingPage());
76          assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
77  
78          final WebWindow thirdWebWindow = webClient.getWebWindowByName("right");
79          assertTrue(FrameWindow.class.isInstance(thirdWebWindow));
80          assertSame(firstPage, ((FrameWindow) thirdWebWindow).getEnclosingPage());
81          assertEquals("Third", ((HtmlPage) thirdWebWindow.getEnclosedPage()).getTitleText());
82      }
83  
84      /**
85       * @throws Exception if the test fails
86       */
87      @Test
88      public void loadingIFrames() throws Exception {
89          final String firstContent = DOCTYPE_HTML
90              + "<html><head><title>First</title></head>\n"
91              + "<body>\n"
92              + "  <iframe name='left' src='" + URL_SECOND + "' />\n"
93              + "  some stuff"
94              + "</html>";
95          final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
96  
97          final WebClient webClient = getWebClientWithMockWebConnection();
98  
99          final MockWebConnection webConnection = getMockWebConnection();
100         webConnection.setResponse(URL_FIRST, firstContent);
101         webConnection.setResponse(URL_SECOND, secondContent);
102 
103         final HtmlPage firstPage = webClient.getPage(URL_FIRST);
104         assertEquals("First", firstPage.getTitleText());
105 
106         final WebWindow secondWebWindow = webClient.getWebWindowByName("left");
107         assertTrue(FrameWindow.class.isInstance(secondWebWindow));
108         assertSame(firstPage, ((FrameWindow) secondWebWindow).getEnclosingPage());
109         assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
110     }
111 
112     /**
113      * Regression test for Bug #203.
114      * @throws Exception if the test fails
115      */
116     @Test
117     public void loadingFrameSetWithRelativePaths() throws Exception {
118         final String framesContent = DOCTYPE_HTML
119             + "<html><head><title>Frames</title></head>\n"
120             + "<frameset rows='110,*'>\n"
121             + "  <frame src='subdir1/menu.html' name='menu' scrolling='no' border='0' noresize>\n"
122             + "  <frame src='subdir2/first.html' name='test' border='0' auto>\n"
123             + "</frameset>\n"
124             + "<noframes>\n"
125             + "  <body>Frames not supported</body>\n"
126             + "</noframes>\n"
127             + "</html>";
128         final String menuContent = DOCTYPE_HTML
129             + "<html><head><title>Menu</title></head>\n"
130             + "<body>\n"
131             + "  <script language='javascript'>\n"
132             + "    function changeEditPage() {parent.test.location='../second.html';}\n"
133             + "  </script>\n"
134             + "  <a name ='changePage' onClick='javascript:changeEditPage();' href='#'>Click</a>."
135             + "</body>\n"
136             + "</html>";
137         final String firstContent = DOCTYPE_HTML
138             + "<html><head><title>First</title></head>\n"
139             + "<body>First/body>\n"
140             + "</html>";
141         final String secondContent = DOCTYPE_HTML
142             + "<html><head><title>Second</title></head>\n"
143             + "<body>Second</body>\n"
144             + "</html>";
145         final String baseUrl = "http://framestest";
146 
147         final URL framesURL = new URL(baseUrl + "/frames.html");
148         final URL menuURL = new URL(baseUrl + "/subdir1/menu.html");
149         final URL firstURL = new URL(baseUrl + "/subdir2/first.html");
150         final URL secondURL = new URL(baseUrl + "/second.html");
151 
152         final WebClient webClient = getWebClientWithMockWebConnection();
153 
154         final MockWebConnection webConnection = getMockWebConnection();
155         webConnection.setResponse(framesURL, framesContent);
156         webConnection.setResponse(menuURL, menuContent);
157         webConnection.setResponse(firstURL, firstContent);
158         webConnection.setResponse(secondURL, secondContent);
159 
160         final HtmlPage framesPage = webClient.getPage(framesURL);
161         assertEquals("Frames", framesPage.getTitleText());
162 
163         final WebWindow menuWebWindow = webClient.getWebWindowByName("menu");
164         final HtmlPage menuPage = (HtmlPage) menuWebWindow.getEnclosedPage();
165         assertEquals("Menu", menuPage.getTitleText());
166 
167         final WebWindow testWebWindow = webClient.getWebWindowByName("test");
168         assertEquals("First", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText());
169 
170         final HtmlAnchor changePage = menuPage.getAnchorByName("changePage");
171         changePage.click();
172         assertEquals("Second", ((HtmlPage) testWebWindow.getEnclosedPage()).getTitleText());
173     }
174 
175     /**
176      * Forward referencing issue in FrameSet.
177      * Test for bug #291
178      * @throws Exception if the test fails
179      */
180     @Test
181     public void frameOnloadAccessOtherFrame() throws Exception {
182         final String framesContent = DOCTYPE_HTML
183             + "<html><head><title>Main</title>\n"
184             + "</head>\n"
185             + "  <frameset cols='18%,*'>\n"
186             + "    <frame name='menu' src='" + URL_SECOND + "'>\n"
187             + "    <frame name='button_pallete' src='about:blank'>\n"
188             + "  </frameset>\n"
189             + "</html>";
190 
191         final String menuContent = DOCTYPE_HTML
192             + "<html><head><title>Menu</title>\n"
193             + "  <script>\n"
194             + "    function init() {\n"
195             + "      var oFrame = top.button_pallete;\n"
196             + "      alert((oFrame == null) ? 'Failure' : 'Success');\n"
197             + "    }\n"
198             + "  </script>\n"
199             + "</head>\n"
200             + "<body onload='init()'></body></html>";
201 
202         final WebClient webClient = getWebClientWithMockWebConnection();
203 
204         final MockWebConnection webConnection = getMockWebConnection();
205 
206         final List<String> collectedAlerts = new ArrayList<>();
207         final String[] expectedAlerts = {"Success"};
208         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
209 
210         webConnection.setResponse(URL_FIRST, framesContent);
211         webConnection.setResponse(URL_SECOND, menuContent);
212 
213         final HtmlPage framesPage = webClient.getPage(URL_FIRST);
214         assertEquals("Main", framesPage.getTitleText());
215 
216         assertEquals(expectedAlerts, collectedAlerts);
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     public void refererHeader() throws Exception {
224         final String firstContent = DOCTYPE_HTML
225             + "<html><head><title>First</title></head>\n"
226             + "<frameset cols='130,*'>\n"
227             + "  <frame scrolling='no' name='left' src='" + URL_SECOND + "' frameborder='1' />\n"
228             + "  <noframes>\n"
229             + "    <body>Frames not supported</body>\n"
230             + "  </noframes>\n"
231             + "</frameset>\n"
232             + "</html>";
233         final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
234 
235         final WebClient webClient = getWebClientWithMockWebConnection();
236 
237         final MockWebConnection webConnection = getMockWebConnection();
238         webConnection.setResponse(URL_FIRST, firstContent);
239         webConnection.setResponse(URL_SECOND, secondContent);
240 
241         final HtmlPage firstPage = webClient.getPage(URL_FIRST);
242         assertEquals("First", firstPage.getTitleText());
243 
244         final Map<String, String> lastAdditionalHeaders = webConnection.getLastAdditionalHeaders();
245         assertEquals(URL_FIRST.toString(), lastAdditionalHeaders.get(HttpHeader.REFERER));
246     }
247 
248     /**
249      * @throws Exception if the test fails
250      */
251     @Test
252     public void scriptUnderNoFrames() throws Exception {
253         final String firstContent = DOCTYPE_HTML
254             + "<html><head><title>first</title></head>\n"
255             + "<frameset cols='100%'>\n"
256             + "  <frame src='" + URL_SECOND + "'' id='frame1'/>\n"
257             + "  <noframes>\n"
258             + "    <div><script>alert(1);</script></div>\n"
259             + "    <script src='" + URL_THIRD + "'></script>\n"
260             + "   </noframes>\n"
261             + "</frameset></html>";
262         final String secondContent = DOCTYPE_HTML
263             + "<html><body><script>alert(2);</script></body></html>";
264         final String thirdContent
265             = "alert('3');\n";
266         final WebClient client = getWebClientWithMockWebConnection();
267 
268         final MockWebConnection webConnection = getMockWebConnection();
269         webConnection.setResponse(URL_FIRST, firstContent);
270         webConnection.setResponse(URL_SECOND, secondContent);
271         webConnection.setResponse(URL_THIRD, thirdContent, "text/javascript");
272 
273         final String[] expectedAlerts = {"2"};
274         final ArrayList<String> collectedAlerts = new ArrayList<>();
275         client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
276 
277         client.getPage(URL_FIRST);
278         assertEquals(expectedAlerts, collectedAlerts);
279     }
280 
281     /**
282      * Regression test for Bug #521.
283      * @throws Exception if the test fails
284      */
285     @Test
286     public void frameReloadsAnother() throws Exception {
287         final URL framesetURL = new URL("http://domain/frameset.html");
288         final URL leftURL = new URL("http://domain/left.html");
289         final URL rightURL = new URL("http://domain/right.html");
290         final URL right2URL = new URL("http://domain/right2.html");
291 
292         final String framesetHtml = DOCTYPE_HTML
293             + "<html><head><title>Test Frameset</title><script>\n"
294             + "function writeLeftFrame() {\n"
295             + "  var leftDoc = leftFrame.document;\n"
296             + "  leftDoc.open();\n"
297             + "  leftDoc.writeln('<HTML><BODY>This is the left frame.<br><br>'\n"
298             + "    + '<A HREF=\"javaScript:parent.showNextPage()\" id=\"node_0\">Show version 2 '\n"
299             + "    + 'of right frame</A></BODY></HTML>');\n"
300             + "  leftDoc.close();\n"
301             + "}\n"
302             + "\n"
303             + "function showNextPage() {\n"
304             + "  rightFrame.location = 'right2.html';\n"
305             + "}\n"
306             + "</script></head>\n"
307             + "<frameset cols='300,*' border=1>\n"
308             + "  <frame name='leftFrame' src='left.html'>\n"
309             + "  <frame name='rightFrame' src='right.html'>\n"
310             + "</frameset>\n"
311             + "</html>";
312 
313         final String leftHtml = DOCTYPE_HTML
314             + "<html>\n"
315             + "<body onLoad=\"parent.writeLeftFrame()\">\n"
316             + "  This is the initial left frame, to be overwritten immediately (onLoad).\n"
317             + "</body></html>";
318 
319         final String rightHtml = DOCTYPE_HTML
320             + "<html>\n"
321             + "<body>\n"
322             + "  This is the right frame, version 1.\n"
323             + "</body></html>";
324 
325         final String right2Html = DOCTYPE_HTML
326             + "<html>\n"
327             + "<body>\n"
328             + "  This is the right frame, version 2.\n"
329             + "</body></html>";
330 
331         final WebClient client = getWebClientWithMockWebConnection();
332         final List<String> collectedAlerts = new ArrayList<>();
333         client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
334 
335         final MockWebConnection webConnection = getMockWebConnection();
336         webConnection.setResponse(framesetURL, framesetHtml);
337         webConnection.setResponse(leftURL, leftHtml);
338         webConnection.setResponse(rightURL, rightHtml);
339         webConnection.setResponse(right2URL, right2Html);
340 
341         final HtmlPage page = client.getPage(framesetURL);
342         final HtmlPage leftPage = (HtmlPage) page.getFrames().get(0).getEnclosedPage();
343         final WebWindow rightWindow = page.getFrames().get(1);
344         assertTrue(((HtmlPage) rightWindow.getEnclosedPage()).asXml().contains("version 1"));
345         leftPage.getAnchors().get(0).click();
346         assertTrue(((HtmlPage) rightWindow.getEnclosedPage()).asXml().contains("version 2"));
347     }
348 
349     /**
350      * @throws Exception if the test fails
351      */
352     @Test
353     @Alerts("3")
354     @HtmlUnitNYI(CHROME = "2",
355             EDGE = "2",
356             FF = "2",
357             FF_ESR = "2")
358     public void onunload() throws Exception {
359         final String mainHtml =
360             "<frameset onunload=\"document.location.href='3.html'\">\n"
361             + "<frame name='f1' src='1.html'/>\n"
362             + "</frameset>";
363 
364         final String frame1 = DOCTYPE_HTML
365             + "<html><head><title>1</title></head>\n"
366             + "<body><button id='myButton' onclick=\"top.location.href='2.html'\"/></body>\n"
367             + "</html>";
368 
369         final String html2 = DOCTYPE_HTML
370             + "<html><head><title>2</title></head>\n"
371             + "<body>hello</body>\n"
372             + "</html>";
373 
374         final String html3 = DOCTYPE_HTML
375             + "<html><head><title>3</title></head>\n"
376             + "<body>hello</body>\n"
377             + "</html>";
378 
379         final WebClient webClient = getWebClientWithMockWebConnection();
380         final List<String> collectedAlerts = new ArrayList<>();
381         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
382 
383         final MockWebConnection conn = getMockWebConnection();
384         conn.setResponse(URL_FIRST, mainHtml);
385         conn.setResponse(new URL(URL_FIRST, "1.html"), frame1);
386         conn.setResponse(new URL(URL_FIRST, "2.html"), html2);
387         conn.setResponse(new URL(URL_FIRST, "3.html"), html3);
388 
389         final HtmlPage mainPage = webClient.getPage(URL_FIRST);
390         final HtmlPage framePage = (HtmlPage) mainPage.getFrameByName("f1").getEnclosedPage();
391         final HtmlPage page = framePage.getHtmlElementById("myButton").click();
392         assertEquals(getExpectedAlerts()[0], page.getTitleText());
393     }
394 
395     /**
396      * @throws Exception if the test fails
397      */
398     @Test
399     public void closeShouldRemoveFramesetWindows() throws Exception {
400         final String firstContent = DOCTYPE_HTML
401             + "<html><head><title>First</title></head>\n"
402             + "<frameset cols='130,*'>\n"
403             + "  <frame scrolling='no' name='left' src='" + URL_SECOND + "' frameborder='1' />\n"
404             + "  <frame scrolling='auto' name='right' src='" + URL_THIRD + "' frameborder='1' />\n"
405             + "  <noframes>\n"
406             + "    <body>Frames not supported</body>\n"
407             + "  </noframes>\n"
408             + "</frameset>\n"
409             + "</html>";
410         final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
411         final String thirdContent  = DOCTYPE_HTML + "<html><head><title>Third</title></head><body></body></html>";
412 
413         @SuppressWarnings("resource")
414         final WebClient webClient = getWebClientWithMockWebConnection();
415 
416         final MockWebConnection webConnection = getMockWebConnection();
417         webConnection.setResponse(URL_FIRST, firstContent);
418         webConnection.setResponse(URL_SECOND, secondContent);
419         webConnection.setResponse(URL_THIRD, thirdContent);
420 
421         final HtmlPage firstPage = webClient.getPage(URL_FIRST);
422         assertEquals("First", firstPage.getTitleText());
423 
424         assertEquals(3, webClient.getWebWindows().size());
425 
426         webClient.close();
427 
428         assertEquals(0, webClient.getWebWindows().size());
429         assertEquals(0, webClient.getTopLevelWindows().size());
430         assertNull(webClient.getCurrentWindow());
431     }
432 
433     /**
434      * @throws Exception if the test fails
435      */
436     @Test
437     public void navigateShouldRemoveFramesetWindows() throws Exception {
438         final String firstContent = DOCTYPE_HTML
439             + "<html><head><title>First</title></head>\n"
440             + "<frameset cols='130,*'>\n"
441             + "  <frame scrolling='no' name='left' src='" + URL_SECOND + "' frameborder='1' />\n"
442             + "  <frame scrolling='auto' name='right' src='" + URL_THIRD + "' frameborder='1' />\n"
443             + "  <noframes>\n"
444             + "    <body>Frames not supported</body>\n"
445             + "  </noframes>\n"
446             + "</frameset>\n"
447             + "</html>";
448         final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
449         final String thirdContent  = DOCTYPE_HTML + "<html><head><title>Third</title></head><body></body></html>";
450 
451         final WebClient webClient = getWebClientWithMockWebConnection();
452 
453         final MockWebConnection webConnection = getMockWebConnection();
454         webConnection.setResponse(URL_FIRST, firstContent);
455         webConnection.setResponse(URL_SECOND, secondContent);
456         webConnection.setResponse(URL_THIRD, thirdContent);
457 
458         HtmlPage page = webClient.getPage(URL_FIRST);
459         assertEquals("First", page.getTitleText());
460 
461         assertEquals(3, webClient.getWebWindows().size());
462 
463         page = webClient.getPage(URL_SECOND);
464         assertEquals("Second", page.getTitleText());
465 
466         assertEquals(1, webClient.getWebWindows().size());
467     }
468 
469 }