Revision: 715
          http://jwebunit.svn.sourceforge.net/jwebunit/?rev=715&view=rev
Author:   henryju
Date:     2007-12-08 12:31:26 -0800 (Sat, 08 Dec 2007)

Log Message:
-----------
[ 1845893 ] No way to click input of type button. Applied patch from Mike with 
some modifications and test cases added.

Modified Paths:
--------------
    
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java
    
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
    
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
    trunk/src/changes/changes.xml

Modified: 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java
 2007-12-01 17:05:42 UTC (rev 714)
+++ 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java
 2007-12-08 20:31:26 UTC (rev 715)
@@ -39,5 +39,26 @@
         assertButtonPresent("button2");
         assertButtonPresent("buttonOutside");
     }
+    
+    public void testAssertButtonWithText() {
+        beginAt("/pageWithTwoForms.html");
+        assertButtonPresentWithText("Testbutton");
+        assertButtonPresentWithText("Testbutton2");
+        assertButtonPresentWithText("Outside");
+        setWorkingForm("form1");
+        assertButtonPresentWithText("Testbutton");
+        assertButtonPresentWithText("Testbutton2");
+        assertButtonPresentWithText("Outside");
+        assertButtonPresentWithText("the submit btn");
+        assertButtonPresentWithText("the reset btn");
+        assertButtonPresentWithText("the btn btn");
+        setWorkingForm("form2");
+        assertButtonPresentWithText("Testbutton");
+        assertButtonPresentWithText("Testbutton2");
+        assertButtonPresentWithText("Outside");
+        assertButtonPresentWithText("Testbutton");
+        assertButtonPresentWithText("Testbutton2");
+        assertButtonPresentWithText("Outside");
+    }
 
 }

Modified: 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
   2007-12-01 17:05:42 UTC (rev 714)
+++ 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
   2007-12-08 20:31:26 UTC (rev 715)
@@ -298,5 +298,17 @@
         submit();
         assertTextPresent("Page 3");
     }
+    
+    /**
+     * Submit input
+     */
+    public void testClickButtonWithText1() {
+        beginAt("/SingleNamedButtonForm.html");
+        setTextField("color", "blue");
+        clickButtonWithText("click me");
+        assertTextPresent("Submitted parameters");
+        assertTextPresent("color=blue");
+    }
 
+
 }

Modified: 
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
        2007-12-01 17:05:42 UTC (rev 714)
+++ 
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
        2007-12-08 20:31:26 UTC (rev 715)
@@ -5,10 +5,13 @@
 <html>
 <body>
 <form id="form1">
-<button id="button1">Testbutton</button>
+<button id="button1">Testbutton</button>
+<input type="submit" value="the submit btn"/>
+<input type="reset" value="the reset btn"/>
+<input type="button" value="the btn btn"/>
 </form>
 <form id="form2">
-<button id="button2">Testbutton</button>
+<button id="button2">Testbutton2</button>
 </form>
 <button id="buttonOutside">Outside</button>
 </body>

Modified: 
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
     2007-12-01 17:05:42 UTC (rev 714)
+++ 
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
     2007-12-08 20:31:26 UTC (rev 715)
@@ -5,7 +5,7 @@
                <input type="text" name="color" />
                <input type="hidden" name="checkBox"/>
                <input type="checkbox" name="checkBox" />
-               <input type="submit" name="button">
+               <input type="submit" name="button" value="click me">
                <input type="hidden" name="myReferer" 
value="FormSubmissionTest/SingleNamedButtonForm.html">
        </form>
 </body>

Modified: 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
===================================================================
--- 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
       2007-12-01 17:05:42 UTC (rev 714)
+++ 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
       2007-12-08 20:31:26 UTC (rev 715)
@@ -1204,25 +1204,48 @@
         return null;
     }
 
+
     /**
-     * Checks if a button with <code>text</code> is present.
-     * 
-     * @param text the text of the button (between 
&lt;button&gt;&lt;/button&gt;).
+     * Checks whether a button containing the specified text as its label 
exists.
+     * For HTML input tags of type submit, reset, or button, this checks the
+     * value attribute.  For HTML button tags, this checks the element's
+     * content by converting it to text.  
+     * @param text the text of the button (between 
&lt;button&gt;&lt;/button&gt;)
+     * or the value of the "value" attribute.
      * @return <code>true</code> when the button with text could be found.
      */
     public boolean hasButtonWithText(String text) {
-        boolean bReturn = getButtonWithText(text) != null ? true : false;
-        return bReturn;
+        return getButtonWithText(text) != null ? true : false;
     }
 
-    public HtmlButton getButtonWithText(String buttonValueText) {
-        List l = ((HtmlPage) win.getEnclosedPage()).getDocumentElement()
+    /**
+     * Returns the first button that contains the specified text as its label.
+     * For HTML input tags of type submit, reset, or button, this checks the
+     * value attribute.  For HTML button tags, this checks the element's
+     * content by converting it to text.  
+     * @param buttonValueText the text of the button (between 
&lt;button&gt;&lt;/button&gt;)
+     * or the value of the "value" attribute.
+     * @return the ClickableElement with the specified text or null if 
+     * no such button is found. 
+     */
+    public ClickableElement getButtonWithText(String buttonValueText) {
+        List l = ((HtmlPage) win.getEnclosedPage()).getDocumentHtmlElement()
                 .getHtmlElementsByTagNames(
-                        Arrays.asList(new String[] { "button" }));
+                        Arrays.asList(new String[] { "button", "input" }));
         for (int i = 0; i < l.size(); i++) {
             HtmlElement e = (HtmlElement) l.get(i);
-            if (((HtmlButton) e).asText().equals(buttonValueText))
-                return (HtmlButton) e;
+            if ( e instanceof HtmlButton )
+            {
+               if (((HtmlButton) e).asText().equals(buttonValueText))
+                       return (ClickableElement) e;
+            }
+            else if ( e instanceof HtmlButtonInput ||
+                         e instanceof HtmlSubmitInput ||
+                         e instanceof HtmlResetInput )
+            {
+               if ( buttonValueText.equals(e.getAttributeValue("value")) )
+                       return (ClickableElement)e;
+            }
         }
         return null;
     }
@@ -1706,14 +1729,24 @@
         }
     }
 
+       /**
+     * Clicks the first button that contains the specified text as its label.
+     * For HTML input tags of type submit, reset, or button, this checks the
+     * value attribute.  For HTML button tags, this checks the element's
+     * content by converting it to text.  or an HTML &lt;button&gt; tag.
+     */
     public void clickButtonWithText(String buttonValueText) {
-        try {
-            if (hasButtonWithText(buttonValueText)) {
-                getButtonWithText(buttonValueText).click();
-            }
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+       ClickableElement b = getButtonWithText(buttonValueText);
+       if (b != null) {
+               try {
+                       b.click();
+               } catch (Exception e) {
+                       throw new RuntimeException(e);
+               }
+       }
+       else {
+               throw new RuntimeException("No button found with text: " + 
buttonValueText);
+       }
     }
 
     /**

Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml       2007-12-01 17:05:42 UTC (rev 714)
+++ trunk/src/changes/changes.xml       2007-12-08 20:31:26 UTC (rev 715)
@@ -8,6 +8,10 @@
     </properties>
     <body>
         <release version="1.5" date="UNKNOW">
+            <action type="fix" dev="Julien Henry" issue="1845893" 
due-to="mtc3b">
+                assertButtonPresentWithText and clickButtonWithText now work 
for input button
+                (submit, reset, button) matching the value attribute.
+            </action>
             <action type="update" dev="Julien Henry">
                 Update to HtmlUnit 1.13.
             </action>


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

-------------------------------------------------------------------------
SF.Net email is sponsored by: 
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to