Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit d617594

Browse files
committed
Adding methods hasElementById(), hasElementByXPath(), hasElementsByXPath() so one can test for elements without having to catch AssertionErrors as per getElementById() etc.
1 parent dc5f949 commit d617594

2 files changed

Lines changed: 63 additions & 16 deletions

File tree

  • jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests
  • jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit

jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/IElementTest.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,8 @@
1818
*/
1919
package net.sourceforge.jwebunit.tests;
2020

21-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertCommentNotPresent;
22-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertCommentPresent;
23-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertElementPresent;
24-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertFormElementPresent;
25-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertMatch;
26-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertNotMatch;
27-
import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextFieldEquals;
28-
import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
29-
import static net.sourceforge.jwebunit.junit.JWebUnit.getElementById;
30-
import static net.sourceforge.jwebunit.junit.JWebUnit.getElementByXPath;
31-
import static net.sourceforge.jwebunit.junit.JWebUnit.getElementsByXPath;
32-
import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
33-
import static net.sourceforge.jwebunit.junit.JWebUnit.setTextField;
34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertNotNull;
36-
import static org.junit.Assert.fail;
21+
import static net.sourceforge.jwebunit.junit.JWebUnit.*;
22+
import static org.junit.Assert.*;
3723

3824
import java.util.Date;
3925
import java.util.List;
@@ -65,6 +51,21 @@ public void testSimple() {
6551
assertEquals(element.getAttribute("id"), "test");
6652
assertEquals(element.getAttribute("value"), "test3");
6753
}
54+
55+
@Test
56+
public void testSimpleHasID() {
57+
assertTrue(hasElementById("test"));
58+
}
59+
60+
@Test
61+
public void testSimpleHasXPath() {
62+
assertTrue(hasElementByXPath("//input[@id='test']"));
63+
}
64+
65+
@Test
66+
public void testSimpleHasXPaths() {
67+
assertTrue(hasElementsByXPath("//input[@id='test']"));
68+
}
6869

6970
@Test
7071
public void testMissing() {
@@ -77,6 +78,21 @@ public void testMissing() {
7778
}
7879
}
7980

81+
@Test
82+
public void testMissingHasID() {
83+
assertFalse(hasElementById("test2"));
84+
}
85+
86+
@Test
87+
public void testMissingHasXPath() {
88+
assertFalse(hasElementByXPath("//input[@id='test2']"));
89+
}
90+
91+
@Test
92+
public void testMissingHasXPaths() {
93+
assertFalse(hasElementsByXPath("//input[@id='test2']"));
94+
}
95+
8096
/**
8197
* Test parent, child methods
8298
*/

jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,16 @@ public IElement getElementByXPath(String xpath) {
27372737
return getTestingEngine().getElementByXPath(xpath);
27382738
}
27392739

2740+
/**
2741+
* Return {@code true} if the given element by XPath exists.
2742+
*
2743+
* @param xpath XPath to search
2744+
* @return {@code true} if the the requested element
2745+
* @throws AssertionError if the element xpath is not found
2746+
*/
2747+
public boolean hasElementByXPath(String xpath) {
2748+
return getTestingEngine().getElementByXPath(xpath) != null;
2749+
}
27402750

27412751
/**
27422752
* Get an element for a particular ID.
@@ -2749,6 +2759,16 @@ public IElement getElementById(String id) {
27492759
assertElementPresent(id);
27502760
return getTestingEngine().getElementByID(id);
27512761
}
2762+
2763+
/**
2764+
* Returns {@code true} if an element with the given ID exists.
2765+
*
2766+
* @param id element ID to find
2767+
* @return {@code true} if the element ID exists, {@code false} otherwise
2768+
*/
2769+
public boolean hasElementById(String id) {
2770+
return getTestingEngine().getElementByID(id) != null;
2771+
}
27522772

27532773
/**
27542774
* Get elements for a particular xpath.
@@ -2760,6 +2780,17 @@ public List<IElement> getElementsByXPath(String xpath) {
27602780
return getTestingEngine().getElementsByXPath(xpath);
27612781
}
27622782

2783+
/**
2784+
* Return {@code true} if the given elements by XPath exists.
2785+
*
2786+
* @param xpath XPath to search
2787+
* @return {@code true} if the given elements by XPath exist
2788+
*/
2789+
public boolean hasElementsByXPath(String xpath) {
2790+
List<IElement> list = getTestingEngine().getElementsByXPath(xpath);
2791+
return list != null && !list.isEmpty();
2792+
}
2793+
27632794
// label methods
27642795
/**
27652796
* Assert a label for a given ID exists.

0 commit comments

Comments
 (0)