KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > xpath > HtmlUnitXPathTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html.xpath;
39
40 import java.util.Arrays;
41 import java.util.List;
42
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.collections.Transformer;
45 import org.jaxen.BaseXPath;
46 import org.jaxen.Navigator;
47 import org.jaxen.XPath;
48
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50 import com.gargoylesoftware.htmlunit.html.DomNode;
51 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
52 import com.gargoylesoftware.htmlunit.html.HtmlBody;
53 import com.gargoylesoftware.htmlunit.html.HtmlPage;
54
55 /**
56  * <p>Tests for XPath evaluation on HtmlUnit DOM.
57  *
58  * @version $Revision: 1.6 $
59  * @author Marc Guillemot
60  */

61 public class HtmlUnitXPathTest extends WebTestCase {
62
63     /**
64      * Create an instance
65      *
66      * @param name The name of the test
67      */

68     public HtmlUnitXPathTest( final String name ) {
69         super( name );
70     }
71
72     /**
73      * Test evaluation of some simple paths
74      * @throws Exception if test fails
75      */

76     public void testSimplePath() throws Exception {
77         final String content = "<html><head><title>Test page</title></head>\n"
78             + "<body><a HREF='foo.html' id='myLink'>foo</a></body>\n"
79             + "</html>";
80
81         final HtmlPage page = loadPage(content);
82         HtmlUnitXPath xpath = new HtmlUnitXPath("/html");
83         assertEquals(page.getDocumentElement(), xpath.selectSingleNode(page));
84         xpath = new HtmlUnitXPath("/html/head");
85         assertEquals(page.getDocumentElement().getFirstChild(), xpath.selectSingleNode(page));
86         xpath = new HtmlUnitXPath("/html/body/a");
87         assertEquals(page.getHtmlElementById("myLink"), xpath.selectSingleNode(page));
88         xpath = new HtmlUnitXPath("/html/head/title/text()");
89         assertEquals("Test page", xpath.stringValueOf(page));
90     }
91
92     /**
93      * Test evaluation relative from elements other than the whole page
94      * @throws Exception if test fails
95      */

96     public void testXPathFromElement() throws Exception {
97         final String content = "<html><head><title>Test page</title></head>\n"
98             + "<body><a HREF='foo.html' id='myLink'>foo</a></body>\n"
99             + "</html>";
100
101         final HtmlPage page = loadPage(content);
102         XPath xpath = new HtmlUnitXPath("/html/body");
103         final HtmlBody body = (HtmlBody) xpath.selectSingleNode(page);
104
105         final Navigator relativeNavigator = HtmlUnitXPath.buildSubtreeNavigator(body);
106         xpath = new BaseXPath("/a", relativeNavigator);
107         assertEquals(page.getHtmlElementById("myLink"), xpath.selectSingleNode(body));
108     }
109
110     /**
111      * Test that the elements are in the right order
112      * @throws Exception if test fails
113      */

114     public void testElementOrder() throws Exception {
115         if (true) {
116             // due to bug http://jira.codehaus.org/browse/JAXEN-55
117
notImplemented();
118             return;
119         }
120
121         final String content
122             = "<html><head><title>First</title><script>"
123             + "</script></head><body>"
124             + "</body></html>";
125
126         final HtmlPage page = loadPage(content);
127         final XPath xpath = new HtmlUnitXPath("//*");
128         final List list = xpath.selectNodes(page);
129
130         final List expected = Arrays.asList(new String[] {"html", "head", "title", "script", "body"});
131         final Transformer tagReader = new Transformer() {
132             public Object transform(final Object obj) {
133                 return ((DomNode) obj).getNodeName();
134             }
135         };
136         CollectionUtils.transform(list, tagReader);
137         assertEquals(expected, list);
138     }
139
140     /**
141      * Test evaluation of paths after changed through javascript
142      * @throws Exception if test fails
143      */

144     public void testWhenJSChangesPage() throws Exception {
145         final String content
146             = "<html><head><title>foo</title><script>"
147             + "function addOption() {\n"
148             + " var options = document.form1.select1.options;\n"
149             + " var index = options.length;\n"
150             + " options[index] = new Option('Four','value4');\n"
151             + "}</script>\n"
152             + "</head>\n"
153             + "<body>"
154             + "<p>hello world</p>"
155             + "<form name='form1'>"
156             + " <select name='select1'>"
157             + " <option name='option1' value='value1'>One</option>"
158             + " <option name='option2' value='value2' selected>Two</option>"
159             + " <option name='option3' value='value3'>Three</option>"
160             + " </select>"
161             + "</form>"
162             + "<a HREF='javascript:addOption()'>add option</a>"
163             + "</body></html>";
164
165         final HtmlPage page = loadPage(content);
166         assertEquals("foo", page.getTitleText());
167         
168         final HtmlUnitXPath xpath = new HtmlUnitXPath("count(//select[@name='select1']/option)");
169         assertEquals(3, ((Double) xpath.evaluate(page)).intValue());
170         
171         final HtmlAnchor link = (HtmlAnchor) page.getAnchors().get(0);
172         link.click();
173         assertEquals(4, ((Double) xpath.evaluate(page)).intValue());
174
175     }
176 }
177
Popular Tags