Revision: 756
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=756&view=rev
Author: jevonwright
Date: 2008-10-29 02:08:52 +0000 (Wed, 29 Oct 2008)
Log Message:
-----------
issue 1519807: added assertLabeledFieldEquals(id, text) and assertLabel*()
methods
Modified Paths:
--------------
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
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/FormAssertionsTest.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java
2008-10-29 00:24:52 UTC (rev 755)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java
2008-10-29 02:08:52 UTC (rev 756)
@@ -257,5 +257,35 @@
assertRadioOptionSelected(option1, "1");
}
+
+ // tests for label elements
+ public void testLabels() {
+ beginAt("/testPage.html");
+
+ assertLabelPresent("label1");
+ assertLabeledFieldEquals("label1", "one");
+ assertLabelPresent("label2");
+ assertLabeledFieldEquals("label2", "two");
+
+ assertLabelPresent("label3");
+ assertLabeledFieldEquals("label3", "three");
+
+ assertLabelPresent("label4");
+ assertLabeledFieldEquals("label4", "2");
+
+ assertLabelPresent("label5");
+ assertLabeledFieldEquals("label5", "2");
+
+ assertLabelPresent("label6");
+ assertLabeledFieldEquals("label6", "ten");
+
+ assertLabelPresent("label7");
+ assertLabeledFieldEquals("label7", "10");
+
+ assertLabelPresent("label8");
+ assertLabeledFieldEquals("label8", "eight");
+
+ }
+
}
Modified:
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
===================================================================
---
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
2008-10-29 00:24:52 UTC (rev 755)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
2008-10-29 02:08:52 UTC (rev 756)
@@ -55,6 +55,50 @@
</form>
</td>
</tr>
+ <tr>
+ <td colspan="3">
+ <h1>Field with labels</h1>
+
+ <label id="label1">
+ Field 1:
+ <input type="text" name="label1_field1" value="one" />
+ </label>
+ <label id="label2">
+ Field 2:
+ <input name="label1_field2" value="two" />
+ </label>
+ <label id="label3">
+ <textarea>three</textarea>
+ </label>
+ <label id="label4">
+ <input type="checkbox" value="1"> Not selected<br />
+ <input type="checkbox" value="2" checked>Selected<br />
+ </label>
+ <label id="label5">
+ <input type="radio" value="1">Not selected<br />
+ <input type="radio" value="2" selected>Selected<br />
+ <input type="text" name="label5_field1" value="Ignore
this" />
+ </label>
+ <label id="label6">
+ <select name="seven" id="label6_seven">
+ <option>eight</option>
+ <option>nine</option>
+ <option selected>ten</option>
+ <option>eleven</option>
+ </select>
+ </label>
+ <label id="label7">
+ <select name="eight" id="label7_eight">
+ <option value="8">eight</option>
+ <option value="9">nine</option>
+ <option value="10" selected>ten</option>
+ <option value="11">eleven</option>
+ </select>
+ </label>
+ <label id="label8" for="input8">Label 8</label>
+ <input id="input8" type="text" value="eight" />
+ </td>
+ </tr>
</table>
</body>
</html>
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
2008-10-29 00:24:52 UTC (rev 755)
+++
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2008-10-29 02:08:52 UTC (rev 756)
@@ -2554,7 +2554,141 @@
public List<IElement> getElementsByXPath(String xpath) {
return getTestingEngine().getElementsByXPath(xpath);
}
+
+ // label methods
+ /**
+ * Assert a label for a given ID exists.
+ */
+ public void assertLabelPresent(String id) {
+ // get all labels
+ for (IElement e : getTestingEngine().getElementsByXPath("//label")) {
+ if (e.getName().equals("label") &&
id.equals(e.getAttribute("id")))
+ return; // label found
+ }
+ Assert.fail("No label found with id [" + id + "]");
+ }
+
+ /**
+ * Find a particular element with given text
+ *
+ * @param elementName the element type e.g. "input", "label"
+ * @param text the text to search for
+ * @return the found element, or null
+ */
+ private IElement getElementWithText(String elementName, String text) {
+ for (IElement e : getTestingEngine().getElementsByXPath("//" +
elementName)) {
+ if (elementName.equals(e.getName()) &&
text.equals(e.getTextContent())) {
+ return e;
+ }
+ }
+ return null;
+ }
+ /**
+ * Assert a label exists.
+ */
+ public void assertLabelMatches(String regexp) {
+ // get regexp
+ RE re = null;
+ try {
+ re = new RE(regexp, RE.MATCH_SINGLELINE);
+ } catch (RESyntaxException e) {
+ Assert.fail(e.toString());
+ }
+
+ // get all labels
+ for (IElement e : getTestingEngine().getElementsByXPath("//label")) {
+ if (e.getName().equals("label") && re.match( e.getTextContent()
))
+ return; // label found
+ }
+ Assert.fail("No label found with text matching [" + regexp + "]");
+ }
+
+ /**
+ * Private method to test the value of a field connected to a particular
IElement label.
+ *
+ * @param label
+ * @param fieldText
+ */
+ private void assertLabeledFieldEquals(String identifier, IElement label,
String fieldText) {
+ List<IElement> fields = new java.util.ArrayList<IElement>();
+ // a direct "for" attribute
+ if (label.getAttribute("for") != null) {
+ IElement e =
getTestingEngine().getElementByID(label.getAttribute("for"));
+ if (e != null)
+ fields.add(e);
+ }
+
+ // implicitly the elements inside the label
+ if (fields.isEmpty()) {
+ // get elements inside the label
+ for (IElement e : label.getChildren()) {
+ if (e.getName().equals("input") ||
e.getName().equals("textarea") || e.getName().equals("select")) {
+ fields.add(e);
+ }
+ }
+ }
+
+ Assert.assertFalse("No field found for label [" + identifier + "]",
fields.isEmpty());
+ String value = null;
+ // cycle through all fields trying to find value
+ for (IElement field : fields) {
+ if (value != null) // stop at first correct value found
+ break;
+ if (field == null)
+ throw new RuntimeException("unexpected null field " +
field);
+
+ if ("input".equals(field.getName())) {
+ if (field.getAttribute("type") != null) {
+ if
(field.getAttribute("type").toLowerCase().equals("checkbox")) {
+ if (field.getAttribute("checked") !=
null) {
+ value =
field.getAttribute("value");
+ }
+ } else if
(field.getAttribute("type").toLowerCase().equals("radio")) {
+ if (field.getAttribute("selected") !=
null) {
+ value =
field.getAttribute("value");
+ }
+ } else {
+ // any other input type
+ value = field.getAttribute("value");
+ }
+ } else {
+ // unspecified input type, default = text
+ value = field.getAttribute("value");
+ }
+ } else if ("textarea".equals(field.getName())) {
+ value = field.getTextContent();
+ } else if ("select".equals(field.getName())) {
+ // get the selected option
+ for (IElement children : field.getChildren()) {
+ if (children.getName().equals("option") &&
children.getAttribute("selected") != null) {
+ value = children.getAttribute("value")
== null ? children.getTextContent() : children.getAttribute("value");
+ break;
+ }
+ }
+ } else {
+ throw new RuntimeException("Unexpected field type " +
field.getName());
+ }
+ }
+
+ Assert.assertEquals("value of field for label [" + identifier + "]
should be [" + fieldText + "]", fieldText, value);
+ }
+
+ /**
+ * Assert that a labeled field exists (for the given id) and the
+ * field that it labels equals the given text
+ *
+ * @param id
+ * @param fieldText
+ */
+ public void assertLabeledFieldEquals(String id, String fieldText) {
+ IElement label = getTestingEngine().getElementByID(id);
+ Assert.assertNotNull("no label for id [" + id + "] found", label);
+ Assert.assertEquals("element with id [" + id + "] is not a label",
"label", label.getName());
+
+ assertLabeledFieldEquals(id, label, fieldText);
+ }
+
// Window and Frame Navigation Methods
/**
Modified:
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
===================================================================
---
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
2008-10-29 00:24:52 UTC (rev 755)
+++
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
2008-10-29 02:08:52 UTC (rev 756)
@@ -26,6 +26,8 @@
private HtmlElement element;
public HtmlUnitElementImpl(HtmlElement element) {
+ if (element == null)
+ throw new NullPointerException("Cannot create an
IElement for a null element.");
this.element = element;
}
@@ -34,6 +36,9 @@
* @see
net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
*/
public String getAttribute(String name) {
+ if (!element.hasAttribute(name))
+ return null;
+
return element.getAttribute(name);
}
@@ -51,8 +56,10 @@
*/
public List<IElement> getChildren() {
List<IElement> children = new ArrayList<IElement>();
- for (HtmlElement e : element.getChildElements())
- children.add(new HtmlUnitElementImpl(e));
+ for (HtmlElement e : element.getChildElements()) {
+ if (e != null)
+ children.add(new HtmlUnitElementImpl(e));
+ }
return children;
}
@@ -103,5 +110,9 @@
}
return elements;
}
+
+ public String toString() {
+ return "IElement[name=" + getName() + " wrapped=" + element +
"]";
+ }
}
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
2008-10-29 00:24:52 UTC (rev 755)
+++
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
2008-10-29 02:08:52 UTC (rev 756)
@@ -2149,14 +2149,20 @@
* @see
net.sourceforge.jwebunit.api.ITestingEngine#getElementByXPath(java.lang.String)
*/
public IElement getElementByXPath(String xpath) {
- return new
HtmlUnitElementImpl(this.getHtmlElementByXPath(xpath));
+ HtmlElement element = this.getHtmlElementByXPath(xpath);
+ if (element != null)
+ return new HtmlUnitElementImpl(element);
+ return null;
}
/* (non-Javadoc)
* @see
net.sourceforge.jwebunit.api.ITestingEngine#getElementByID(java.lang.String)
*/
public IElement getElementByID(String id) {
- return new HtmlUnitElementImpl(this.getHtmlElement(id));
+ HtmlElement element = this.getHtmlElement(id);
+ if (element != null)
+ return new HtmlUnitElementImpl(element);
+ return null;
}
/* (non-Javadoc)
Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml 2008-10-29 00:24:52 UTC (rev 755)
+++ trunk/src/changes/changes.xml 2008-10-29 02:08:52 UTC (rev 756)
@@ -17,6 +17,9 @@
<action type="add" dev="jevonwright" issue="1728676">
Added WebTestCase(WebTester) constructor
</action>
+ <action type="add" dev="jevonwright" issue="1519807">
+ Added assertLabeledFieldEquals(id, text) and
assertLabel*() methods
+ </action>
<action type="update" dev="jevonwright">
BC CHANGE: setFormElement(), assertFormElementEquals()
methods will no longer assert that a form already exists in the page (as
allowed by the HTML standard).
</action>
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development