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