1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26 public class XPathExpressionTest extends WebDriverTestCase {
27
28
29
30
31 @Test
32 @Alerts({"function", "[object XPathEvaluator]", "[object XPathExpression]", "first", "second", ""})
33 public void simple() throws Exception {
34 final String html = DOCTYPE_HTML
35 + "<html><body>\n"
36 + "<span id='first'>hello</span>\n"
37 + "<div><span id='second'>world</span></div>\n"
38 + "<script>\n"
39 + LOG_TITLE_FUNCTION
40 + "var res = '';\n"
41 + "try {\n"
42 + " res += typeof window.XPathEvaluator + '§';\n"
43 + " var xpe = new XPathEvaluator();\n"
44 + " res += xpe + '§';\n"
45 + " var expression = xpe.createExpression('//span');\n"
46 + " res += expression + '§';\n"
47 + " var result = expression.evaluate(document.body);\n"
48 + " var found = [];\n"
49 + " var next;\n"
50 + " while (next = result.iterateNext()) {\n"
51 + " res += next.id + '§';\n"
52 + " }\n"
53 + "} catch(e) { res += 'exception' + '§'; }\n"
54 + "log(res);\n"
55 + "</script></body></html>";
56
57 loadPageVerifyTitle2(html);
58 }
59
60
61
62
63 @Test
64 @Alerts({"function", "[object HTMLHtmlElement]", "[object XPathEvaluator]",
65 "[object XPathExpression]", "first", "second", ""})
66 public void withResolver() throws Exception {
67 final String html = DOCTYPE_HTML
68 + "<html><body>\n"
69 + "<span id='first'>hello</span>\n"
70 + "<div><span id='second'>world</span></div>\n"
71 + "<script>\n"
72 + LOG_TITLE_FUNCTION
73 + "var res = '';\n"
74 + "try {\n"
75 + " res += typeof window.XPathEvaluator + '§';\n"
76 + " var xpe = new XPathEvaluator();\n"
77 + " var nsResolver = xpe.createNSResolver(document.documentElement);\n"
78 + " res += nsResolver + '§';\n"
79 + " res += xpe + '§';\n"
80 + " var expression = xpe.createExpression('//span', nsResolver);\n"
81 + " res += expression + '§';\n"
82 + " var result = expression.evaluate(document.body);\n"
83 + " var found = [];\n"
84 + " var next;\n"
85 + " while (next = result.iterateNext()) {\n"
86 + " res += next.id + '§';\n"
87 + " }\n"
88 + "} catch(e) { res += 'exception' + '§'; }\n"
89 + "log(res);\n"
90 + "</script></body></html>";
91
92 loadPageVerifyTitle2(html);
93 }
94
95
96
97
98 @Test
99 @Alerts({"function", "[object HTMLHtmlElement]", "[object XPathEvaluator]",
100 "[object XPathExpression]", "exception", ""})
101 public void evaluateContextNode() throws Exception {
102 final String html = DOCTYPE_HTML
103 + "<html><body>\n"
104 + "<span id='first'>hello</span>\n"
105 + "<div><span id='second'>world</span></div>\n"
106 + "<script>\n"
107 + LOG_TITLE_FUNCTION
108 + "var res = '';\n"
109 + "try {\n"
110 + " res += typeof window.XPathEvaluator + '§';\n"
111 + " var xpe = new XPathEvaluator();\n"
112 + " var nsResolver = xpe.createNSResolver(document.documentElement);\n"
113 + " res += nsResolver + '§';\n"
114 + " res += xpe + '§';\n"
115 + " var expression = xpe.createExpression('//span', nsResolver);\n"
116 + " res += expression + '§';\n"
117 + " var result = expression.evaluate();\n"
118 + " var found = [];\n"
119 + " var next;\n"
120 + " while (next = result.iterateNext()) {\n"
121 + " res += next.id + '§';\n"
122 + " }\n"
123 + "} catch(e) { res += 'exception' + '§'; }\n"
124 + "log(res);\n"
125 + "</script></body></html>";
126
127 loadPageVerifyTitle2(html);
128 }
129 }