Revision: 976
          http://sourceforge.net/p/jwebunit/code/976
Author:   jevonwright
Date:     2014-03-17 23:23:25 +0000 (Mon, 17 Mar 2014)
Log Message:
-----------
Adding methods hasElementById(), hasElementByXPath(), hasElementsByXPath() so 
one can test for elements without having to catch AssertionErrors as per 
getElementById() etc.

Modified Paths:
--------------
    
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/IElementTest.java
    
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java

Property Changed:
----------------
    trunk/

Index: trunk
===================================================================
--- trunk       2014-03-17 11:04:26 UTC (rev 975)
+++ trunk       2014-03-17 23:23:25 UTC (rev 976)

Property changes on: trunk
___________________________________________________________________
Modified: svn:ignore
## -1,3 +1,4 ##
 target
 .project
 .settings
+bin
Modified: 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/IElementTest.java
===================================================================
--- 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/IElementTest.java
 2014-03-17 11:04:26 UTC (rev 975)
+++ 
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/IElementTest.java
 2014-03-17 23:23:25 UTC (rev 976)
@@ -18,22 +18,8 @@
  */
 package net.sourceforge.jwebunit.tests;
 
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertCommentNotPresent;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertCommentPresent;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertElementPresent;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertFormElementPresent;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertMatch;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertNotMatch;
-import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextFieldEquals;
-import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
-import static net.sourceforge.jwebunit.junit.JWebUnit.getElementById;
-import static net.sourceforge.jwebunit.junit.JWebUnit.getElementByXPath;
-import static net.sourceforge.jwebunit.junit.JWebUnit.getElementsByXPath;
-import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
-import static net.sourceforge.jwebunit.junit.JWebUnit.setTextField;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static net.sourceforge.jwebunit.junit.JWebUnit.*;
+import static org.junit.Assert.*;
 
 import java.util.Date;
 import java.util.List;
@@ -65,8 +51,23 @@
        assertEquals(element.getAttribute("id"), "test");
        assertEquals(element.getAttribute("value"), "test3");
     }
+    
+    @Test
+    public void testSimpleHasID() {
+       assertTrue(hasElementById("test"));
+    }
 
     @Test
+    public void testSimpleHasXPath() {
+       assertTrue(hasElementByXPath("//input[@id='test']"));
+    }
+
+    @Test
+    public void testSimpleHasXPaths() {
+       assertTrue(hasElementsByXPath("//input[@id='test']"));
+    }
+
+    @Test
     public void testMissing() {
        // a missing element should throw an exception
        try {
@@ -77,6 +78,21 @@
        }
     }
     
+    @Test
+    public void testMissingHasID() {
+       assertFalse(hasElementById("test2"));
+    }    
+    
+    @Test
+    public void testMissingHasXPath() {
+       assertFalse(hasElementByXPath("//input[@id='test2']"));
+    }
+    
+    @Test
+    public void testMissingHasXPaths() {
+       assertFalse(hasElementsByXPath("//input[@id='test2']"));
+    }
+
     /**
      * Test parent, child methods
      */

Modified: 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
===================================================================
--- 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java 
    2014-03-17 11:04:26 UTC (rev 975)
+++ 
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java 
    2014-03-17 23:23:25 UTC (rev 976)
@@ -2737,6 +2737,16 @@
       return getTestingEngine().getElementByXPath(xpath);
     }
 
+    /**
+     * Return {@code true} if the given element by XPath exists.
+     *
+     * @param xpath XPath to search
+     * @return {@code true} if the the requested element
+     * @throws AssertionError if the element xpath is not found
+     */
+    public boolean hasElementByXPath(String xpath) {
+      return getTestingEngine().getElementByXPath(xpath) != null;
+    }
 
     /**
      * Get an element for a particular ID.
@@ -2749,6 +2759,16 @@
       assertElementPresent(id);
       return getTestingEngine().getElementByID(id);
     }
+    
+    /**
+     * Returns {@code true} if an element with the given ID exists.
+     * 
+     * @param id element ID to find
+     * @return {@code true} if the element ID exists, {@code false} otherwise
+     */
+    public boolean hasElementById(String id) {
+       return getTestingEngine().getElementByID(id) != null;
+    }
 
     /**
      * Get elements for a particular xpath.
@@ -2760,6 +2780,17 @@
       return getTestingEngine().getElementsByXPath(xpath);
     }
 
+    /**
+     * Return {@code true} if the given elements by XPath exists.
+     *
+     * @param xpath XPath to search
+     * @return {@code true} if the given elements by XPath exist
+     */
+    public boolean hasElementsByXPath(String xpath) {
+       List<IElement> list = getTestingEngine().getElementsByXPath(xpath);
+       return list != null && !list.isEmpty();
+    }
+
     // label methods
     /**
      * Assert a label for a given ID exists.

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


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
JWebUnit-development mailing list
JWebUnit-development@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to