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.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   * Tests for {@link XPathExpression}.
23   *
24   * @author Ronald Brill
25   */
26  public class XPathExpressionTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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 }