Revision: 482
Author:   henryju
Date:     2006-06-16 07:52:15 -0700 (Fri, 16 Jun 2006)
ViewCVS:  http://svn.sourceforge.net/jwebunit/?rev=482&view=rev

Log Message:
-----------
Add XPath support to HttpUnit (only hasElementByXPath, but still not 
clickElementByXPath)
This allow to implement hasResetButton() :)

Modified Paths:
--------------
    trunk/jwebunit-httpunit-plugin/pom.xml
    
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
    
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
Modified: trunk/jwebunit-httpunit-plugin/pom.xml
===================================================================
--- trunk/jwebunit-httpunit-plugin/pom.xml      2006-06-15 14:31:40 UTC (rev 
481)
+++ trunk/jwebunit-httpunit-plugin/pom.xml      2006-06-16 14:52:15 UTC (rev 
482)
@@ -1,18 +1,28 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0";
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+<project>
     <parent>
+        <artifactId>jwebunit</artifactId>
         <groupId>net.sourceforge.jwebunit</groupId>
-        <artifactId>jwebunit</artifactId>
         <version>1.3-SNAPSHOT</version>
         <relativePath>../</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>jwebunit-httpunit-plugin</artifactId>
-    <packaging>jar</packaging>
-    <name>HttpUnit Plugin</name>
+    <name>HttpUnit Plugin</name>
     <description>HttpUnit plugin for jWebUnit.</description>
+    <build>
+        <plugins>
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <includes>
+                        <include>**/*Test.java</include>
+                    </includes>
+                    <testFailureIgnore>true</testFailureIgnore>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
     <repositories>
         <repository>
             <releases />
@@ -22,9 +32,6 @@
             <url>file:${basedir}/private-repository</url>
         </repository>
     </repositories>
-    <properties>
-        <topDirectoryLocation>..</topDirectoryLocation>
-    </properties>
     <dependencies>
         <dependency>
             <groupId>junit</groupId>
@@ -47,19 +54,13 @@
             <version>1.3-SNAPSHOT</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>jaxen</groupId>
+            <artifactId>jaxen</artifactId>
+            <version>1.1-beta-8</version>
+        </dependency>
     </dependencies>
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-surefire-plugin</artifactId>
-                <configuration>
-                    <includes>
-                        <include>**/*Test.java</include>
-                    </includes>
-                    <testFailureIgnore>true</testFailureIgnore>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
-</project>
+    <properties>
+        <topDirectoryLocation>..</topDirectoryLocation>
+    </properties>
+</project>
\ No newline at end of file

Modified: 
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
===================================================================
--- 
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
  2006-06-15 14:31:40 UTC (rev 481)
+++ 
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
  2006-06-16 14:52:15 UTC (rev 482)
@@ -12,6 +12,9 @@
 
 import org.apache.regexp.RE;
 import org.apache.regexp.RESyntaxException;
+import org.jaxen.JaxenException;
+import org.jaxen.XPath;
+import org.jaxen.dom.DOMXPath;
 
 import net.sourceforge.jwebunit.IJWebUnitDialog;
 import net.sourceforge.jwebunit.TestContext;
@@ -49,6 +52,7 @@
 import com.meterware.httpunit.WebTable;
 import com.meterware.httpunit.WebWindow;
 import com.meterware.httpunit.cookies.CookieJar;
+import com.meterware.httpunit.parsing.HTMLParserFactory;
 
 /**
  * Acts as the wrapper for HttpUnit access. A dialog is initialized with a 
given
@@ -107,6 +111,7 @@
     }
 
     private void initWebClient() {
+        HTMLParserFactory.useJTidyParser();
         wc = new WebConversation();
 
         wc.getClientProperties().setUserAgent(testContext.getUserAgent());
@@ -622,13 +627,11 @@
     }
 
     public boolean hasResetButton() {
-        // TODO Implement hasResetButton in HttpUnitDialog
-        throw new UnsupportedOperationException("hasResetButton");
+        return hasElementByXPath("//[EMAIL PROTECTED]'reset']");
     }
 
     public boolean hasResetButton(String buttonName) {
-        // TODO Implement hasResetButton(String) in HttpUnitDialog
-        throw new UnsupportedOperationException("hasResetButton");
+        return hasElementByXPath("//[EMAIL PROTECTED]'reset' and 
@name='"+buttonName+"']");
     }
 
     /**
@@ -1394,7 +1397,7 @@
      * @param anID
      *            id of the element.
      */
-    public Element getElement(String anID) {
+    private Element getElement(String anID) {
         try {
             return walkDOM(getResponse().getDOM().getDocumentElement(), anID);
         } catch (Exception e) {
@@ -1580,13 +1583,34 @@
     }
 
     public boolean hasElementByXPath(String xpath) {
-        // TODO Implement hasElementByXPath in HttpUnitDialog
-        throw new UnsupportedOperationException("hasElementByXPath");
+        return getElementByXPath(xpath)!=null;
     }
+    
+    private XPath makeXpath(String xpathString) throws JaxenException {
+        return new DOMXPath(xpathString);
+    }
+    
+    private Object getElementByXPath(String xpath) {
+        try {
+            Document rootNode = getResponse().getDOM();
+            XPath xpathObject = makeXpath(xpath);
+            return xpathObject.selectSingleNode(rootNode);
+        } catch (SAXException e) {
+            throw new RuntimeException(e);
+        } catch (JaxenException e) {
+            throw new RuntimeException(e);
+        }
+    }
 
     public void clickElementByXPath(String xpath) {
-        // TODO Implement clickElementByXPath in HttpUnitDialog
-        throw new UnsupportedOperationException("clickElementByXPath");
+        Object o = getElementByXPath(xpath);
+        if (o instanceof Node) {
+            //TODO fix clickElementByXPath in HttpUnitDialog
+            throw new UnsupportedOperationException("clickElementByXPath");
+        } 
+        else {
+            throw new RuntimeException("Don't know how to click on this 
element");
+        }
     }
 
     /*

Modified: 
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
===================================================================
--- 
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
    2006-06-15 14:31:40 UTC (rev 481)
+++ 
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
    2006-06-16 14:52:15 UTC (rev 482)
@@ -44,6 +44,7 @@
         suite.addTestSuite(FormAssertionsTest.class);
         suite.addTestSuite(FormAssertionsWithLabelTest.class);
         suite.addTestSuite(NavigationTest.class);
+        suite.addTestSuite(XPathTest.class);
         suite.addTestSuite(CharsetTest.class);
         //$JUnit-END$
         return new JettySetup(suite);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to