1
2
3
4
5
6
7
8
9
10
11
12
13
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
37
38
39
40
41 public class FaqTest {
42
43
44
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
64
65 assertEquals("Title\ncontent...", page.asNormalizedText());
66 }
67 }
68
69
70
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
88
89 assertEquals("Title\ncontent...", page.asNormalizedText());
90 }
91 }
92
93
94
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
107
108 @Test
109 public void scriptPreProcessor() throws Exception {
110 final URL url = new URL("https://www.htmlunit.org");
111
112
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
120
121 return sourceCode;
122 }
123 };
124
125 try (WebClient webClient = new WebClient()) {
126
127 webClient.setScriptPreProcessor(myScriptPreProcessor);
128
129
130 final HtmlPage page = webClient.getPage(url);
131 }
132 }
133 }