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.doc;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  
20  import java.awt.image.BufferedImage;
21  import java.io.ByteArrayInputStream;
22  import java.net.URL;
23  import java.nio.charset.StandardCharsets;
24  
25  import javax.imageio.ImageIO;
26  
27  import org.htmlunit.BrowserVersion;
28  import org.htmlunit.ScriptPreProcessor;
29  import org.htmlunit.WebClient;
30  import org.htmlunit.html.HtmlElement;
31  import org.htmlunit.html.HtmlPage;
32  import org.htmlunit.html.XHtmlPage;
33  import org.junit.Test;
34  
35  /**
36   * Tests for the sample code from the documentation to make sure
37   * we adapt the documentation or do not break the samples.
38   *
39   * @author Ronald Brill
40   */
41  public class FaqTest {
42  
43      /**
44       * @throws Exception if an error occurs
45       */
46      @Test
47      public void xhtmlPageFromString() throws Exception {
48          final BrowserVersion browserVersion = BrowserVersion.FIREFOX;
49  
50          final String htmlCode = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\""
51                  + "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
52                  + "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
53                  + "  <head>"
54                  + "    <title>Title</title>"
55                  + "  </head>"
56                  + "  <body>"
57                  + "    content..."
58                  + "  </body>"
59                  + "</html> ";
60          try (WebClient webClient = new WebClient(browserVersion)) {
61              final XHtmlPage page = webClient.loadXHtmlCodeIntoCurrentWindow(htmlCode);
62  
63              // work with the xhtml page
64  
65              assertEquals("Title\ncontent...", page.asNormalizedText());
66          }
67      }
68  
69      /**
70       * @throws Exception if an error occurs
71       */
72      @Test
73      public void htmlPageFromString() throws Exception {
74          final BrowserVersion browserVersion = BrowserVersion.FIREFOX;
75  
76          final String htmlCode = "<html>"
77                  + "  <head>"
78                  + "    <title>Title</title>"
79                  + "  </head>"
80                  + "  <body>"
81                  + "    content..."
82                  + "  </body>"
83                  + "</html> ";
84          try (WebClient webClient = new WebClient(browserVersion)) {
85              final HtmlPage page = webClient.loadHtmlCodeIntoCurrentWindow(htmlCode);
86  
87              // work with the html page
88  
89              assertEquals("Title\ncontent...", page.asNormalizedText());
90          }
91      }
92  
93      /**
94       * @throws Exception if an error occurs
95       */
96      @Test
97      public void checkSvgSupport() throws Exception {
98          final String svg =  "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\">"
99                  + "<circle cx=\"5\" cy=\"5\" r=\"4\" stroke=\"black\" stroke-width=\"1\" fill=\"red\" />"
100                 + "</svg>";
101         final BufferedImage img = ImageIO.read(new ByteArrayInputStream(svg.getBytes(StandardCharsets.US_ASCII)));
102         assertNotNull(img);
103     }
104 
105     /**
106      * @throws Exception if an error occurs
107      */
108     @Test
109     public void scriptPreProcessor() throws Exception {
110         final URL url = new URL("https://www.htmlunit.org");
111 
112         // create a ScriptPreProcessor
113         final ScriptPreProcessor myScriptPreProcessor = new ScriptPreProcessor() {
114 
115             @Override
116             public String preProcess(final HtmlPage htmlPage, final String sourceCode, final String sourceName,
117                     final int lineNumber, final HtmlElement htmlElement) {
118 
119                 // modify the source code here
120 
121                 return sourceCode;
122             }
123         };
124 
125         try (WebClient webClient = new WebClient()) {
126             // activate the ScriptPreProcessor
127             webClient.setScriptPreProcessor(myScriptPreProcessor);
128 
129             // use the client as usual
130             final HtmlPage page = webClient.getPage(url);
131         }
132     }
133 }