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