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.BrowserRunner;
28 import org.htmlunit.junit.annotation.Alerts;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31
32
33
34
35
36
37
38
39
40
41 @RunWith(BrowserRunner.class)
42 public class HTMLIFrameElementTest extends SimpleWebTestCase {
43
44
45
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
84
85
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
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
130 List<FrameWindow> frames = page.getFrames();
131 assertEquals(1, frames.size());
132 assertEquals("frame content", ((HtmlPage) page.getFrameByName("content").getEnclosedPage()).asNormalizedText());
133
134
135 page.getElementById("clickId").click();
136
137 assertEquals("new content", page.getElementById("content").asNormalizedText());
138
139
140 frames = page.getFrames();
141 assertTrue(frames.isEmpty());
142 }
143
144
145
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 }