1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.net.URL;
18
19 import org.htmlunit.html.DomElement;
20 import org.htmlunit.html.DomNodeList;
21 import org.htmlunit.html.HtmlCheckBoxInput;
22 import org.htmlunit.html.HtmlInlineFrame;
23 import org.htmlunit.html.HtmlPage;
24 import org.htmlunit.html.HtmlRadioButtonInput;
25 import org.htmlunit.util.MimeType;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32
33
34 public class WebClient8Test extends SimpleWebTestCase {
35
36
37
38
39 @Test
40 public void asNormalizedText() throws Exception {
41 final String html = DOCTYPE_HTML
42 + "<html><head><title>foo</title></head>\n"
43 + "<body><div>Hello <b>HtmlUnit</b></div></body></html>";
44
45 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
46 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
47 assertEquals("foo\nHello HtmlUnit", page.asNormalizedText());
48 }
49 }
50
51
52
53
54 @Test
55 public void asXml() throws Exception {
56 final String html = DOCTYPE_HTML
57 + "<html><head><title>foo</title></head>\n"
58 + "<body><div>Hello <b>HtmlUnit</b></div></body></html>";
59
60 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
61 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
62 assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<html>\r\n"
63 + " <head>\r\n"
64 + " <title>foo</title>\r\n"
65 + " </head>\r\n"
66 + " <body>\r\n"
67 + " <div>Hello <b>HtmlUnit</b>\r\n"
68 + " </div>\r\n"
69 + " </body>\r\n"
70 + "</html>",
71 page.asXml());
72 }
73 }
74
75
76
77
78 @Test
79 public void cloneNode() throws Exception {
80 final String html = DOCTYPE_HTML
81 + "<html>\n"
82 + "<head><title>foo</title></head>\n"
83 + "<body>\n"
84 + "<p>hello world</p>\n"
85 + "</body></html>";
86
87 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
88 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
89
90 final String org = page.asXml();
91
92 final HtmlPage clonedPage = page.cloneNode(true);
93 final String clone = clonedPage.asXml();
94
95 assertEquals(org, clone);
96 }
97 }
98
99
100
101
102 @Test
103 public void appendChildMoved() throws Exception {
104 final String html = DOCTYPE_HTML
105 + "<html>\n"
106 + "<head><title>foo</title></head>\n"
107 + "<body>\n"
108 + "<p>hello</p>\n"
109 + "</body></html>";
110
111 final String html2 = DOCTYPE_HTML
112 + "<html>\n"
113 + "<head><title>foo</title></head>\n"
114 + "<body>\n"
115 + "<p id='tester'>world</p>\n"
116 + "</body></html>";
117
118 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
119 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
120 final HtmlPage page2 = loadPage(webClient, html2, null, URL_SECOND);
121
122 final DomNodeList<DomElement> elements = page.getElementsByTagName("*");
123 assertEquals(5, elements.getLength());
124
125 page.getBody().appendChild(page2.getElementById("tester"));
126 assertEquals(6, elements.getLength());
127 }
128 }
129
130
131
132
133 @Test
134 public void iFrame() throws Exception {
135 final String html = DOCTYPE_HTML
136 + "<html>\n"
137 + "<head><title>foo</title></head>\n"
138 + "<body>\n"
139 + " <iframe id='tester' src='second.html'></iframe>\n"
140 + "</body></html>";
141
142 final String html2 = DOCTYPE_HTML
143 + "<html>\n"
144 + "<head><title>frame</title></head>\n"
145 + "<body>\n"
146 + "</body></html>";
147
148 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
149 final MockWebConnection webConnection = getMockWebConnection();
150 webConnection.setResponse(URL_FIRST, html);
151 webConnection.setDefaultResponse(html2);
152 webClient.setWebConnection(webConnection);
153
154 final HtmlPage page = webClient.getPage(URL_FIRST);
155
156 final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
157 assertEquals("frame", ((HtmlPage) iFrame.getEnclosedWindow().getEnclosedPage()).getTitleText());
158 }
159 }
160
161
162
163
164 @Test
165 public void iFrameTextContent() throws Exception {
166 final String html = DOCTYPE_HTML
167 + "<html>\n"
168 + "<head><title>foo</title></head>\n"
169 + "<body>\n"
170 + " <iframe id='tester' src='second.html'></iframe>\n"
171 + "</body></html>";
172
173 final String plainContent = "plain frame content";
174
175 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
176 final MockWebConnection webConnection = getMockWebConnection();
177 webConnection.setResponse(URL_FIRST, html);
178 webConnection.setDefaultResponse(plainContent, 200, "OK", MimeType.TEXT_PLAIN);
179 webClient.setWebConnection(webConnection);
180
181 final HtmlPage page = webClient.getPage(URL_FIRST);
182
183 final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
184 assertEquals(plainContent, ((TextPage) iFrame.getEnclosedWindow().getEnclosedPage()).getContent());
185 }
186 }
187
188
189
190
191 @Test
192 public void iFrameInFragment() throws Exception {
193 final String html = DOCTYPE_HTML
194 + "<html>\n"
195 + "<head><title>foo</title></head>\n"
196 + "<body>\n"
197 + "<p id='para'>hello</p>\n"
198 + "</body></html>";
199
200 final String html2 = DOCTYPE_HTML
201 + "<html>\n"
202 + "<head><title>frame</title></head>\n"
203 + "<body>\n"
204 + "</body></html>";
205
206 final String fragment = "<iframe id='tester' src='second.html'></iframe>";
207
208 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
209 final MockWebConnection webConnection = getMockWebConnection();
210 webConnection.setResponse(URL_FIRST, html);
211 webConnection.setDefaultResponse(html2);
212 webClient.setWebConnection(webConnection);
213
214 final HtmlPage page = webClient.getPage(URL_FIRST);
215 final DomElement para = page.getElementById("para");
216 page.getWebClient().getPageCreator().getHtmlParser()
217 .parseFragment(para, para, fragment, false);
218
219 final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
220 assertEquals("frame", ((HtmlPage) iFrame.getEnclosedWindow().getEnclosedPage()).getTitleText());
221 }
222 }
223
224
225
226
227 @Test
228 public void script() throws Exception {
229 final String html = DOCTYPE_HTML
230 + "<html>\n"
231 + "<head>\n"
232 + " <title>foo</title>\n"
233 + " <script src='script.js'></script>\n"
234 + " <script src='script.js' async></script>\n"
235 + "</head>\n"
236 + "<body>\n"
237 + "</body></html>";
238
239 final String script = "alert('test');";
240
241 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
242 final MockWebConnection webConnection = getMockWebConnection();
243 webConnection.setResponse(URL_FIRST, html);
244 webConnection.setDefaultResponse(script);
245 webClient.setWebConnection(webConnection);
246
247 webClient.getPage(URL_FIRST);
248 }
249 }
250
251
252
253
254 @Test
255 public void link() throws Exception {
256 final String html = DOCTYPE_HTML
257 + "<html>\n"
258 + "<head>\n"
259 + " <title>foo</title>\n"
260 + " <link rel='stylesheet' href='simple.css'>\n"
261 + "</head>\n"
262 + "<body>\n"
263 + "</body></html>";
264
265 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
266 final MockWebConnection webConnection = getMockWebConnection();
267 webConnection.setResponse(URL_FIRST, html);
268 webConnection.setResponse(new URL(URL_FIRST, "simple.css"), "");
269
270 webClient.setWebConnection(webConnection);
271
272 webClient.getPage(URL_FIRST);
273 }
274 }
275
276
277
278
279 @Test
280 public void object() throws Exception {
281 final String html = DOCTYPE_HTML
282 + "<html>\n"
283 + "<head>\n"
284 + " <title>foo</title>\n"
285 + "</head>\n"
286 + "<body>\n"
287 + " <object type='application/pdf' classid='cls12345'></object>\n"
288 + "</body></html>";
289
290 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
291 final MockWebConnection webConnection = getMockWebConnection();
292 webConnection.setResponse(URL_FIRST, html);
293 webConnection.setResponse(new URL(URL_FIRST, "simple.css"), "");
294
295 webClient.setWebConnection(webConnection);
296
297 webClient.getPage(URL_FIRST);
298 }
299 }
300
301
302
303
304 @Test
305 public void svgScript() throws Exception {
306 final String html = DOCTYPE_HTML
307 + "<html>\n"
308 + "<head>\n"
309 + " <title>foo</title>\n"
310 + "</head>\n"
311 + "<body>\n"
312 + " <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
313 + " <script id='myId'></script>\n"
314 + " </svg>\n"
315 + "</body></html>";
316
317 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
318 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
319 assertEquals(page.getBody().getChildElementCount(), 1);
320 }
321 }
322
323
324
325
326 @Test
327 public void invalidElementEventWithNoJS() throws Exception {
328 final String html = DOCTYPE_HTML
329 + "<html>"
330 + "<head>"
331 + " <title>foo</title>"
332 + "</head>"
333 + " <body onLoad='ready()'>"
334 + " </body>"
335 + "</html>";
336
337 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
338 loadPage(webClient, html, null, URL_FIRST);
339 }
340 }
341
342
343
344
345 @Test
346 public void checkingWithNoJS() throws Exception {
347 final String html = DOCTYPE_HTML
348 + "<html>"
349 + "<head>"
350 + " <title>foo</title>"
351 + "</head>"
352 + " <body>"
353 + " <form id='form1'>\n"
354 + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
355 + " <input type='radio' name='radio' id='radio'>Check me</input>\n"
356 + " </form>"
357 + " </body>"
358 + "</html>";
359
360 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
361 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
362
363 final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
364 checkBox.setChecked(true);
365
366 final HtmlRadioButtonInput radioButton = page.getHtmlElementById("radio");
367 radioButton.setChecked(true);
368 }
369 }
370
371
372
373
374 @Test
375 public void frameSetWithNoJS() throws Exception {
376 final String html = DOCTYPE_HTML
377 + "<html>\n"
378 + "<head>\n"
379 + " <title>foo</title>\n"
380 + "</head>\n"
381 + "<frameset cols='200,*' frameborder='0' framespacing='0' border='0' >"
382 + " <frame src='menu.html' marginheight=0 marginwidth=0"
383 + " frameborder=0 scrolling='no' noresize name='leftarea'>"
384 + " <frame src='intro.html' marginheight=0 marginwidth=0"
385 + " frameborder=0 noresize name='mainarea'>"
386 + "</frameset>"
387 + "</html>";
388
389 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
390 loadPage(webClient, html, null, URL_FIRST);
391 }
392 }
393
394
395
396
397 @Test
398 public void imageEventHandlersWithNoJs() throws Exception {
399 final String html = DOCTYPE_HTML
400 + "<html>\n"
401 + "<head>\n"
402 + "</head>\n"
403 + " <body>\n"
404 + " <img onerror='doSomething(this)' />\n"
405 + " </body>\n"
406 + "</html>";
407
408 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
409 loadPage(webClient, html, null, URL_FIRST);
410 }
411 }
412
413
414
415
416 @Test
417 public void clickWithNoJs() throws Exception {
418 final String html = DOCTYPE_HTML
419 + "<html>\n"
420 + "<head>\n"
421 + "</head>\n"
422 + " <body>\n"
423 + " <img onerror='doSomething(this)' />\n"
424 + " </body>\n"
425 + "</html>";
426
427 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
428 loadPage(webClient, html, null, URL_FIRST);
429 }
430 }
431 }