/*
* Copyright (c) 2002-2008 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.html.xpath;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebTestCase;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomText;
import com.gargoylesoftware.htmlunit.html.HtmlBody;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
/**
* Tests for XPath evaluation on HtmlUnit DOM.
*
* @version $Revision$
* @author Marc Guillemot
* @author Ahmed Ashour
*/
public class HtmlUnitXPathTest extends WebTestCase {
/**
* Test evaluation of some simple paths.
* @throws Exception if test fails
*/
@Test
public void simplePath() throws Exception {
final String content = "
Test page\n"
+ "foo\n"
+ "";
final HtmlPage page = loadPage(content);
assertEquals(page.getDocumentElement(), page.getFirstByXPath("/html"));
assertEquals(page.getDocumentElement().getFirstChild(), page.getFirstByXPath("/html/head"));
assertEquals(page.getHtmlElementById("myLink"), page.getFirstByXPath("/html/body/a"));
assertEquals("Test page", ((DomText) page.getFirstByXPath("/html/head/title/text()")).getNodeValue());
}
/**
* Test evaluation relative from elements other than the whole page.
* @throws Exception if test fails
*/
@Test
public void xpathFromElement() throws Exception {
final String content = "Test page\n"
+ "foo\n"
+ "";
final HtmlPage page = loadPage(content);
final HtmlBody body = (HtmlBody) page.getFirstByXPath("/html/body");
assertEquals(page.getHtmlElementById("myLink"), body.getFirstByXPath("./a"));
}
/**
* Test that the elements are in the right order.
* @throws Exception if test fails
*/
@Test
@SuppressWarnings("unchecked")
public void elementOrder() throws Exception {
final String content
= "First\n"
+ "";
final HtmlPage page = loadPage(content);
final List< ? > list = page.getByXPath("//*");
final String[] expected = {"html", "head", "title", "script", "body"};
final List actualNames = new ArrayList();
for (final DomNode node : (List) list) {
actualNames.add(node.getNodeName());
}
assertEquals(expected, actualNames);
}
/**
* Test evaluation of paths after they're changed through JavaScript.
* @throws Exception if test fails
*/
@Test
public void whenJSChangesPage() throws Exception {
final String content
= "foo\n"
+ "\n"
+ "\n"
+ "
hello world
\n"
+ "\n"
+ "add option\n"
+ "";
final HtmlPage page = loadPage(content);
assertEquals("foo", page.getTitleText());
assertEquals(3, ((Double) page.getFirstByXPath("count(//select[@name='select1']/option)")).intValue());
page.getAnchors().get(0).click();
assertEquals(4, ((Double) page.getFirstByXPath("count(//select[@name='select1']/option)")).intValue());
}
/**
* Tests XPath where results are attributes.
* @throws Exception if test fails
*/
@Test
@SuppressWarnings("unchecked")
public void listAttributesResult() throws Exception {
final String content
= "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
final List< ? > nameList = page.getByXPath("//img/@src");
final List< ? > valueList = new ArrayList