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.util.List;
18  
19  import javax.net.ssl.SSLHandshakeException;
20  
21  import org.htmlunit.MockWebConnection;
22  import org.htmlunit.SimpleWebTestCase;
23  import org.htmlunit.WebClient;
24  import org.htmlunit.html.FrameWindow;
25  import org.htmlunit.html.HtmlInlineFrame;
26  import org.htmlunit.html.HtmlPage;
27  import org.htmlunit.junit.BrowserRunner;
28  import org.htmlunit.junit.annotation.Alerts;
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  
32  /**
33   * Tests for {@link HTMLIFrameElement}.
34   *
35   * @author Marc Guillemot
36   * @author Ahmed Ashour
37   * @author Daniel Gredler
38   * @author Ronald Brill
39   * @author Frank Danek
40   */
41  @RunWith(BrowserRunner.class)
42  public class HTMLIFrameElementTest extends SimpleWebTestCase {
43  
44      /**
45       * @throws Exception if the test fails
46       */
47      @Test
48      public void setSrcAttribute() throws Exception {
49          final String firstContent = DOCTYPE_HTML
50              + "<html><head><title>First</title><script>\n"
51              + "  function test() {\n"
52              + "    document.getElementById('iframe1').setAttribute('src', '" + URL_SECOND + "');\n"
53              + "  }\n"
54              + "</script></head>\n"
55              + "<body onload='test()'>\n"
56              + "<iframe id='iframe1'>\n"
57              + "</body></html>";
58  
59          final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
60          final String thirdContent = DOCTYPE_HTML + "<html><head><title>Third</title></head><body></body></html>";
61          final WebClient client = getWebClient();
62  
63          final MockWebConnection webConnection = getMockWebConnection();
64          webConnection.setResponse(URL_FIRST, firstContent);
65          webConnection.setResponse(URL_SECOND, secondContent);
66          webConnection.setResponse(URL_THIRD, thirdContent);
67  
68          client.setWebConnection(webConnection);
69  
70          final HtmlPage page = client.getPage(URL_FIRST);
71          assertEquals("First", page.getTitleText());
72  
73          final HtmlInlineFrame iframe = page.getHtmlElementById("iframe1");
74          assertEquals(URL_SECOND.toExternalForm(), iframe.getSrcAttribute());
75          assertEquals("Second", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
76  
77          iframe.setSrcAttribute(URL_THIRD.toExternalForm());
78          assertEquals(URL_THIRD.toExternalForm(), iframe.getSrcAttribute());
79          assertEquals("Third", ((HtmlPage) iframe.getEnclosedPage()).getTitleText());
80      }
81  
82      /**
83       * Verify that an iframe.src with a "javascript:..." URL loaded by a client with JavaScript
84       * disabled does not cause a NullPointerException (reported on the mailing list).
85       * @throws Exception if an error occurs
86       */
87      @Test
88      public void srcJavaScriptUrl_JavaScriptDisabled() throws Exception {
89          final String html = DOCTYPE_HTML + "<html><body><iframe src='javascript:false;'></iframe></body></html>";
90  
91          final WebClient client = getWebClient();
92          client.getOptions().setJavaScriptEnabled(false);
93  
94          final MockWebConnection conn = getMockWebConnection();
95          conn.setDefaultResponse(html);
96          client.setWebConnection(conn);
97  
98          client.getPage(URL_FIRST);
99      }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     public void removeFrameWindow() throws Exception {
106         final String index = DOCTYPE_HTML
107                 + "<html><head></head><body>\n"
108                 + "<div id='content'>\n"
109                 + "  <iframe name='content' src='second/'></iframe>\n"
110                 + "</div>\n"
111                 + "<button id='clickId' "
112                 +     "onClick=\"document.getElementById('content').innerHTML = 'new content';\">Item</button>\n"
113                 + "</body></html>";
114 
115         final String frame1 = DOCTYPE_HTML
116                 + "<html><head></head><body>\n"
117                 + "<p>frame content</p>\n"
118                 + "</body></html>";
119 
120         final WebClient webClient = getWebClient();
121         final MockWebConnection webConnection = getMockWebConnection();
122 
123         webConnection.setResponse(URL_SECOND, frame1);
124         webClient.setWebConnection(webConnection);
125 
126         final HtmlPage page = loadPage(index);
127 
128         assertEquals("frame content", page.getElementById("content").asNormalizedText());
129         // check frame on page
130         List<FrameWindow> frames = page.getFrames();
131         assertEquals(1, frames.size());
132         assertEquals("frame content", ((HtmlPage) page.getFrameByName("content").getEnclosedPage()).asNormalizedText());
133 
134         // replace frame tag with javascript
135         page.getElementById("clickId").click();
136 
137         assertEquals("new content", page.getElementById("content").asNormalizedText());
138 
139         // frame has to be gone
140         frames = page.getFrames();
141         assertTrue(frames.isEmpty());
142     }
143 
144     /**
145      * @throws Exception if the test fails
146      */
147     @Test
148     @Alerts({"", "2"})
149     public void iframeUrlInvalid() throws Exception {
150         final String html = DOCTYPE_HTML
151             + "<html>\n"
152             + "<head>\n"
153             + "  <script>\n"
154             + "    function log(msg) { window.document.title += msg + '\\u00a7'; }\n"
155             + "  </script>\n"
156             + "</head>\n"
157             + "<body>\n"
158             + "  <iframe id='frame1' src='" + URL_SECOND + "' "
159                         + "onLoad='log(\"loaded\")' onError='log(\"error\")'></iframe>\n"
160             + "</body>\n"
161             + "</html>";
162 
163         getMockWebConnection().setDefaultResponse(html);
164         getMockWebConnection().setThrowable(URL_SECOND, new SSLHandshakeException("Test"));
165 
166         final String[] expectedAlerts = getExpectedAlerts();
167         final HtmlPage page = loadPage(html);
168         assertEquals(expectedAlerts[0], page.getTitleText());
169 
170         assertEquals(Integer.parseInt(expectedAlerts[1]), getMockWebConnection().getRequestCount());
171     }
172 }