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().parseFragment(null, para, para, fragment, false);
217
218 final HtmlInlineFrame iFrame = (HtmlInlineFrame) page.getElementById("tester");
219 assertEquals("frame", ((HtmlPage) iFrame.getEnclosedWindow().getEnclosedPage()).getTitleText());
220 }
221 }
222
223
224
225
226 @Test
227 public void script() throws Exception {
228 final String html = DOCTYPE_HTML
229 + "<html>\n"
230 + "<head>\n"
231 + " <title>foo</title>\n"
232 + " <script src='script.js'></script>\n"
233 + " <script src='script.js' async></script>\n"
234 + "</head>\n"
235 + "<body>\n"
236 + "</body></html>";
237
238 final String script = "alert('test');";
239
240 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
241 final MockWebConnection webConnection = getMockWebConnection();
242 webConnection.setResponse(URL_FIRST, html);
243 webConnection.setDefaultResponse(script);
244 webClient.setWebConnection(webConnection);
245
246 webClient.getPage(URL_FIRST);
247 }
248 }
249
250
251
252
253 @Test
254 public void link() throws Exception {
255 final String html = DOCTYPE_HTML
256 + "<html>\n"
257 + "<head>\n"
258 + " <title>foo</title>\n"
259 + " <link rel='stylesheet' href='simple.css'>\n"
260 + "</head>\n"
261 + "<body>\n"
262 + "</body></html>";
263
264 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
265 final MockWebConnection webConnection = getMockWebConnection();
266 webConnection.setResponse(URL_FIRST, html);
267 webConnection.setResponse(new URL(URL_FIRST, "simple.css"), "");
268
269 webClient.setWebConnection(webConnection);
270
271 webClient.getPage(URL_FIRST);
272 }
273 }
274
275
276
277
278 @Test
279 public void object() throws Exception {
280 final String html = DOCTYPE_HTML
281 + "<html>\n"
282 + "<head>\n"
283 + " <title>foo</title>\n"
284 + "</head>\n"
285 + "<body>\n"
286 + " <object type='application/pdf' classid='cls12345'></object>\n"
287 + "</body></html>";
288
289 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
290 final MockWebConnection webConnection = getMockWebConnection();
291 webConnection.setResponse(URL_FIRST, html);
292 webConnection.setResponse(new URL(URL_FIRST, "simple.css"), "");
293
294 webClient.setWebConnection(webConnection);
295
296 webClient.getPage(URL_FIRST);
297 }
298 }
299
300
301
302
303 @Test
304 public void svgScript() throws Exception {
305 final String html = DOCTYPE_HTML
306 + "<html>\n"
307 + "<head>\n"
308 + " <title>foo</title>\n"
309 + "</head>\n"
310 + "<body>\n"
311 + " <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
312 + " <script id='myId'></script>\n"
313 + " </svg>\n"
314 + "</body></html>";
315
316 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
317 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
318 assertEquals(page.getBody().getChildElementCount(), 1);
319 }
320 }
321
322
323
324
325 @Test
326 public void invalidElementEventWithNoJS() throws Exception {
327 final String html = DOCTYPE_HTML
328 + "<html>"
329 + "<head>"
330 + " <title>foo</title>"
331 + "</head>"
332 + " <body onLoad='ready()'>"
333 + " </body>"
334 + "</html>";
335
336 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
337 loadPage(webClient, html, null, URL_FIRST);
338 }
339 }
340
341
342
343
344 @Test
345 public void checkingWithNoJS() throws Exception {
346 final String html = DOCTYPE_HTML
347 + "<html>"
348 + "<head>"
349 + " <title>foo</title>"
350 + "</head>"
351 + " <body>"
352 + " <form id='form1'>\n"
353 + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
354 + " <input type='radio' name='radio' id='radio'>Check me</input>\n"
355 + " </form>"
356 + " </body>"
357 + "</html>";
358
359 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
360 final HtmlPage page = loadPage(webClient, html, null, URL_FIRST);
361
362 final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
363 checkBox.setChecked(true);
364
365 final HtmlRadioButtonInput radioButton = page.getHtmlElementById("radio");
366 radioButton.setChecked(true);
367 }
368 }
369
370
371
372
373 @Test
374 public void frameSetWithNoJS() throws Exception {
375 final String html = DOCTYPE_HTML
376 + "<html>\n"
377 + "<head>\n"
378 + " <title>foo</title>\n"
379 + "</head>\n"
380 + "<frameset cols='200,*' frameborder='0' framespacing='0' border='0' >"
381 + " <frame src='menu.html' marginheight=0 marginwidth=0"
382 + " frameborder=0 scrolling='no' noresize name='leftarea'>"
383 + " <frame src='intro.html' marginheight=0 marginwidth=0"
384 + " frameborder=0 noresize name='mainarea'>"
385 + "</frameset>"
386 + "</html>";
387
388 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
389 loadPage(webClient, html, null, URL_FIRST);
390 }
391 }
392
393
394
395
396 @Test
397 public void imageEventHandlersWithNoJs() throws Exception {
398 final String html = DOCTYPE_HTML
399 + "<html>\n"
400 + "<head>\n"
401 + "</head>\n"
402 + " <body>\n"
403 + " <img onerror='doSomething(this)' />\n"
404 + " </body>\n"
405 + "</html>";
406
407 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
408 loadPage(webClient, html, null, URL_FIRST);
409 }
410 }
411
412
413
414
415 @Test
416 public void clickWithNoJs() throws Exception {
417 final String html = DOCTYPE_HTML
418 + "<html>\n"
419 + "<head>\n"
420 + "</head>\n"
421 + " <body>\n"
422 + " <img onerror='doSomething(this)' />\n"
423 + " </body>\n"
424 + "</html>";
425
426 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
427 loadPage(webClient, html, null, URL_FIRST);
428 }
429 }
430 }