Revision: 558
Author:   henryju
Date:     2006-08-08 10:15:39 -0700 (Tue, 08 Aug 2006)
ViewCVS:  http://svn.sourceforge.net/jwebunit/?rev=558&view=rev

Log Message:
-----------
Add assertJavascriptAlertPresent method to check presence of a Javascript alert.

Modified Paths:
--------------
    
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
    
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java
    trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java
    trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
    
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
    
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumDialog.java
    trunk/src/changes/changes.xml

Added Paths:
-----------
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/Alert.html
    
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/MultipleAlerts.html
    
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/ElementNotFoundException.java
    
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/JWebUnitException.java
Modified: 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
       2006-08-07 20:46:43 UTC (rev 557)
+++ 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
       2006-08-08 17:15:39 UTC (rev 558)
@@ -29,5 +29,21 @@
         //FIXME Fails with HtmlUnit
         //assertTextPresent("Hello World");
     }
+    
+    public void testAlertOnPageLoad() {
+        beginAt("Alert.html");
+        assertJavascriptAlertPresent("Foo Bar");
+    }
+    
+    public void testInvalidAlertOnPageLoad() {
+        beginAt("Alert.html");
+        assertFail("assertJavascriptAlertPresent", "invalid");
+    }
 
+    public void testMultipleAlerts() {
+        beginAt("MultipleAlerts.html");
+        assertJavascriptAlertPresent("Alert 1");
+        assertJavascriptAlertPresent("Alert 2");
+    }
+
 }

Added: 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/Alert.html
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/Alert.html
                         (rev 0)
+++ 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/Alert.html
 2006-08-08 17:15:39 UTC (rev 558)
@@ -0,0 +1,7 @@
+<HTML>
+       <BODY>
+               <SCRIPT LANGUAGE="javascript">
+                       alert("Foo Bar");
+               </SCRIPT>
+       </BODY>
+<HTML>
\ No newline at end of file

Added: 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/MultipleAlerts.html
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/MultipleAlerts.html
                                (rev 0)
+++ 
trunk/jwebunit-commons-tests/src/main/resources/testcases/JavaScriptTest/MultipleAlerts.html
        2006-08-08 17:15:39 UTC (rev 558)
@@ -0,0 +1,8 @@
+<HTML>
+       <BODY>
+               <SCRIPT LANGUAGE="javascript">
+                       alert("Alert 1");
+                       alert("Alert 2");
+               </SCRIPT>
+       </BODY>
+<HTML>
\ No newline at end of file

Modified: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java
===================================================================
--- 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java 
    2006-08-07 20:46:43 UTC (rev 557)
+++ 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java 
    2006-08-08 17:15:39 UTC (rev 558)
@@ -4,6 +4,7 @@
  
******************************************************************************/
 package net.sourceforge.jwebunit;
 
+import net.sourceforge.jwebunit.exception.ElementNotFoundException;
 import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
 import net.sourceforge.jwebunit.html.Table;
 
@@ -188,7 +189,7 @@
      * @deprecated
      */
     String getFormParameterValue(String paramName);
-    
+
     /**
      * Return the current value of a text field with name 
<code>paramName</code>.
      * Text fields are input text, input password and textarea
@@ -197,9 +198,10 @@
      *            name of the text field element.
      */
     String getTextFieldValue(String paramName);
-    
+
     /**
-     * Return the current value of a hidden input element with name 
<code>paramName</code>.
+     * Return the current value of a hidden input element with name
+     * <code>paramName</code>.
      * 
      * @param paramName
      *            name of the hidden input element.
@@ -222,18 +224,18 @@
      * Exemple: <br/>
      * 
      * <pre>
-     *         &lt;FORM action=&quot;http://my_host/doit&quot; 
method=&quot;post&quot;&gt;
-     *           &lt;P&gt;
-     *             &lt;SELECT multiple size=&quot;4&quot; 
name=&quot;component-select&quot;&gt;
-     *               &lt;OPTION selected 
value=&quot;Component_1_a&quot;&gt;Component_1&lt;/OPTION&gt;
-     *               &lt;OPTION selected 
value=&quot;Component_1_b&quot;&gt;Component_2&lt;/OPTION&gt;
-     *               &lt;OPTION&gt;Component_3&lt;/OPTION&gt;
-     *               &lt;OPTION&gt;Component_4&lt;/OPTION&gt;
-     *               &lt;OPTION&gt;Component_5&lt;/OPTION&gt;
-     *             &lt;/SELECT&gt;
-     *             &lt;INPUT type=&quot;submit&quot; 
value=&quot;Send&quot;&gt;&lt;INPUT type=&quot;reset&quot;&gt;
-     *           &lt;/P&gt;
-     *         &lt;/FORM&gt;
+     *          &lt;FORM action=&quot;http://my_host/doit&quot; 
method=&quot;post&quot;&gt;
+     *            &lt;P&gt;
+     *              &lt;SELECT multiple size=&quot;4&quot; 
name=&quot;component-select&quot;&gt;
+     *                &lt;OPTION selected 
value=&quot;Component_1_a&quot;&gt;Component_1&lt;/OPTION&gt;
+     *                &lt;OPTION selected 
value=&quot;Component_1_b&quot;&gt;Component_2&lt;/OPTION&gt;
+     *                &lt;OPTION&gt;Component_3&lt;/OPTION&gt;
+     *                &lt;OPTION&gt;Component_4&lt;/OPTION&gt;
+     *                &lt;OPTION&gt;Component_5&lt;/OPTION&gt;
+     *              &lt;/SELECT&gt;
+     *              &lt;INPUT type=&quot;submit&quot; 
value=&quot;Send&quot;&gt;&lt;INPUT type=&quot;reset&quot;&gt;
+     *            &lt;/P&gt;
+     *          &lt;/FORM&gt;
      * </pre>
      * 
      * Should return [Component_1_a, Component_1_b, Component_3, Component_4,
@@ -675,4 +677,16 @@
      *            regexp to match.
      */
     boolean isMatchInElement(String elementID, String regexp);
+
+    /**
+     * When you perform an action, the dialog keep an history of each 
Javascript
+     * alert thrown. This method get the first Javascript alert in the list
+     * and remove it.
+     * With Selenium, you HAVE TO check presence of alert. If not, an 
exception is thrown on the next action.
+     * With HtmlUnit, no exception is thrown, but the list will never be 
cleared if you don't check alert.
+     * 
+     * @return Text of the alert.
+     * @throws ElementNotFoundException If there is no alert in the list.
+     */
+    String getJavascriptAlert() throws ElementNotFoundException;
 }

Modified: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java
===================================================================
--- trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java 
2006-08-07 20:46:43 UTC (rev 557)
+++ trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java 
2006-08-08 17:15:39 UTC (rev 558)
@@ -574,9 +574,6 @@
         getTester().assertFramePresent(frameName);
     }
 
-    /**
-     * Contributed by Vivek Venugopalan.
-     */
     public void assertCookiePresent(String cookieName) {
         getTester().assertCookiePresent(cookieName);
     }
@@ -588,6 +585,10 @@
     public void assertCookieValueMatch(String cookieName, String regexp) {
         getTester().assertCookieValueMatch(cookieName, regexp);
     }
+    
+    public void assertJavascriptAlertPresent(String msg) {
+        getTester().assertJavascriptAlertPresent(msg);
+    }
 
     // Form interaction methods
 

Modified: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
===================================================================
--- trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java   
2006-08-07 20:46:43 UTC (rev 557)
+++ trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java   
2006-08-08 17:15:39 UTC (rev 558)
@@ -10,6 +10,7 @@
 
 import junit.framework.Assert;
 import junit.framework.AssertionFailedError;
+import net.sourceforge.jwebunit.exception.ElementNotFoundException;
 import net.sourceforge.jwebunit.exception.TestingEngineRegistryException;
 import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
 import net.sourceforge.jwebunit.exception.UnableToSetFormException;
@@ -1563,8 +1564,7 @@
     }
 
     /**
-     * Checks to see if a cookie is present in the response. Contributed by
-     * Vivek Venugopalan.
+     * Checks to see if a cookie is present in the response.
      * 
      * @param cookieName
      *            The cookie name
@@ -1592,6 +1592,17 @@
                 + cookieName + "\"", re.match(getDialog().getCookieValue(
                 cookieName)));
     }
+    
+    public void assertJavascriptAlertPresent(String msg) {
+        String alert = null;
+        try {
+            alert = getDialog().getJavascriptAlert();
+        } catch (ElementNotFoundException e) {
+            Assert.fail(e.getMessage());
+        }
+        
+        Assert.assertTrue(alert.equals(msg));
+    }
 
     // Form interaction methods
 

Added: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/ElementNotFoundException.java
===================================================================
--- 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/ElementNotFoundException.java
                          (rev 0)
+++ 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/ElementNotFoundException.java
  2006-08-08 17:15:39 UTC (rev 558)
@@ -0,0 +1,23 @@
+/******************************************************************************
+ * jWebUnit project (http://jwebunit.sourceforge.net)                         *
+ * Distributed open-source, see full license under LICENCE.txt                *
+ 
******************************************************************************/
+package net.sourceforge.jwebunit.exception;
+
+/**
+ * This exception should be used when an expected element is not found.
+ * @author Julien Henry ([EMAIL PROTECTED])
+ * 
+ */
+public class ElementNotFoundException extends JWebUnitException {
+   
+    //TODO Perhaps add some informations about missing element (tag, name, 
...).
+    
+    public ElementNotFoundException(String msg) {
+        super(msg);
+    }
+
+    public ElementNotFoundException(String msg, Exception cause) {
+        super(msg, cause);
+    }
+}

Added: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/JWebUnitException.java
===================================================================
--- 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/JWebUnitException.java
                         (rev 0)
+++ 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/exception/JWebUnitException.java
 2006-08-08 17:15:39 UTC (rev 558)
@@ -0,0 +1,22 @@
+/******************************************************************************
+ * jWebUnit project (http://jwebunit.sourceforge.net)                         *
+ * Distributed open-source, see full license under LICENCE.txt                *
+ 
******************************************************************************/
+package net.sourceforge.jwebunit.exception;
+
+
+/**
+ * Global ancestor for all exception used by jWebUnit.
+ * @author Julien Henry ([EMAIL PROTECTED])
+ *
+ */
+public class JWebUnitException extends Exception {
+    
+    public JWebUnitException(String msg) {
+        super(msg);
+    }
+
+    public JWebUnitException(String msg, Exception cause) {
+        super(msg, cause);
+    }
+}

Modified: 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
===================================================================
--- 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
  2006-08-07 20:46:43 UTC (rev 557)
+++ 
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
  2006-08-08 17:15:39 UTC (rev 558)
@@ -15,6 +15,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.regexp.RE;
@@ -30,6 +31,7 @@
 import net.sourceforge.jwebunit.IJWebUnitDialog;
 import net.sourceforge.jwebunit.TestContext;
 
+import com.gargoylesoftware.htmlunit.AlertHandler;
 import com.gargoylesoftware.htmlunit.BrowserVersion;
 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
 import com.gargoylesoftware.htmlunit.Page;
@@ -100,6 +102,11 @@
      * Is Javascript enabled.
      */
     private boolean jsEnabled = true;
+    
+    /**
+     * Javascript alerts
+     */
+    private LinkedList<String> javascriptAlerts = new LinkedList<String>();
 
     // Implementation of IJWebUnitDialog
 
@@ -524,6 +531,12 @@
                 }
             }
         });
+        //Add Javascript Alert Handler
+        wc.setAlertHandler(new AlertHandler() {
+            public void handleAlert(Page page, String msg) {
+                javascriptAlerts.add(msg);
+            }
+        });
     }
 
     /**
@@ -1525,7 +1538,16 @@
         wc.setCurrentWindow(win);
         this.win = win;
     }
+    
 
+    public String getJavascriptAlert() throws 
net.sourceforge.jwebunit.exception.ElementNotFoundException {
+        if (!javascriptAlerts.isEmpty()) {
+            return javascriptAlerts.removeFirst();
+        } else {
+            throw new 
net.sourceforge.jwebunit.exception.ElementNotFoundException("There is no 
pending alert.");
+        }
+    }
+
     /**
      * Return the response for the given frame in the current conversation.
      * 
@@ -1534,6 +1556,7 @@
     private WebWindow getFrame(String frameName) {
         return ((HtmlPage) win.getEnclosedPage()).getFrameByName(frameName);
     }
+    
 
     /**
      * @param testContext

Modified: 
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumDialog.java
===================================================================
--- 
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumDialog.java
  2006-08-07 20:46:43 UTC (rev 557)
+++ 
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumDialog.java
  2006-08-08 17:15:39 UTC (rev 558)
@@ -1,3 +1,7 @@
+/******************************************************************************
+ * jWebUnit project (http://jwebunit.sourceforge.net)                         *
+ * Distributed open-source, see full license under LICENCE.txt                *
+ 
******************************************************************************/
 package net.sourceforge.jwebunit.selenium;
 
 import java.io.PrintStream;
@@ -2,2 +6,3 @@
 
+import net.sourceforge.jwebunit.exception.ElementNotFoundException;
 import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
@@ -469,4 +474,13 @@
         //TODO implement getHiddenFieldValue in SeleniumDialog
         throw new UnsupportedOperationException("getHiddenFieldValue");
     }
+    
+    public String getJavascriptAlert() throws ElementNotFoundException {
+        if (selenium.isAlertPresent()) {
+            return selenium.getAlert(); 
+        }
+        else {
+            throw new 
net.sourceforge.jwebunit.exception.ElementNotFoundException("There is no 
pending alert.");
+        }
+    }
 }
\ No newline at end of file

Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml       2006-08-07 20:46:43 UTC (rev 557)
+++ trunk/src/changes/changes.xml       2006-08-08 17:15:39 UTC (rev 558)
@@ -6,6 +6,9 @@
        </properties>
        <body>
        <release version="2.0" date="unknow">
+               <action type="add" dev="Julien Henry">
+                       Add assertJavascriptAlertPresent method to check 
presence of a Javascript alert.
+               </action>
                <action type="update" dev="Julien Henry">
                        Refactor TestingEngineRegistry: now fully static and 
use JDK 1.5.
                        It is now easier to add a custom plugin in registry.


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


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to