1 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 61 public class HtmlUnitXPathTest extends WebTestCase { 62 63 68 public HtmlUnitXPathTest( final String name ) { 69 super( name ); 70 } 71 72 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 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 114 public void testElementOrder() throws Exception { 115 if (true) { 116 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 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 |