1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
34
35
36
37
38
39 public class HTMLIFrameElementTest extends SimpleWebTestCase {
40
41
42
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
81
82
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
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
127 List<FrameWindow> frames = page.getFrames();
128 assertEquals(1, frames.size());
129 assertEquals("frame content", ((HtmlPage) page.getFrameByName("content").getEnclosedPage()).asNormalizedText());
130
131
132 page.getElementById("clickId").click();
133
134 assertEquals("new content", page.getElementById("content").asNormalizedText());
135
136
137 frames = page.getFrames();
138 assertTrue(frames.isEmpty());
139 }
140
141
142
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 }