Revision: 758
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=758&view=rev
Author: jevonwright
Date: 2008-10-29 04:17:14 +0000 (Wed, 29 Oct 2008)
Log Message:
-----------
added setLabeledFormElementField(id, value) method
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/api/IElement.java
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/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 03:14:02 UTC (rev 757)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java
2008-10-29 04:17:14 UTC (rev 758)
@@ -4,6 +4,7 @@
******************************************************************************/
package net.sourceforge.jwebunit.tests;
+import net.sourceforge.jwebunit.api.IElement;
import net.sourceforge.jwebunit.tests.util.JettySetup;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -258,7 +259,9 @@
}
- // tests for label elements
+ /**
+ * tests for label elements
+ */
public void testLabels() {
beginAt("/testPage.html");
@@ -285,7 +288,42 @@
assertLabelPresent("label8");
assertLabeledFieldEquals("label8", "eight");
+ }
+
+ /**
+ * Test setting elements retrieved through labels
+ */
+ public void testSetLabels() {
+ beginAt("/testPage.html");
+ assertLabelPresent("label1");
+ assertLabeledFieldEquals("label1", "one");
+ assertTextFieldEquals("label1_field1", "one");
+ assertEquals(getElementById("field1_id").getAttribute("value"), "one");
+
+ // through setLabeledFormElementField
+ setLabeledFormElementField("label1", "two");
+
+ assertLabeledFieldEquals("label1", "two");
+ assertTextFieldEquals("label1_field1", "two");
+ assertEquals(getElementById("field1_id").getAttribute("value"), "two");
+
+ // through normal setValue
+ setTextField("label1_field1", "three");
+
+ assertLabeledFieldEquals("label1", "three");
+ assertTextFieldEquals("label1_field1", "three");
+ assertEquals(getElementById("field1_id").getAttribute("value"),
"three");
+
+ // through IElement
+ IElement element = getElementById("field1_id");
+ assertNotNull(element);
+ element.setAttribute("value", "four");
+
+ assertLabeledFieldEquals("label1", "four");
+ assertTextFieldEquals("label1_field1", "four");
+ assertEquals(getElementById("field1_id").getAttribute("value"), "four");
+
}
}
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 03:14:02 UTC (rev 757)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
2008-10-29 04:17:14 UTC (rev 758)
@@ -61,7 +61,7 @@
<label id="label1">
Field 1:
- <input type="text" name="label1_field1" value="one" />
+ <input type="text" name="label1_field1" id="field1_id"
value="one" />
</label>
<label id="label2">
Field 2:
Modified:
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/IElement.java
===================================================================
---
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/IElement.java
2008-10-29 03:14:02 UTC (rev 757)
+++
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/IElement.java
2008-10-29 04:17:14 UTC (rev 758)
@@ -66,5 +66,27 @@
* @return A list of all matching elements
*/
public List<IElement> getElements(String xpath);
+
+ /**
+ * Set an attribute on this element.
+ *
+ * @param string
+ */
+ public void setAttribute(String string);
+
+ /**
+ * Set an attribute on this element.
+ *
+ * @param string
+ * @param value
+ */
+ public void setAttribute(String string, String value);
+
+ /**
+ * Set the text content on this element.
+ *
+ * @param value
+ */
+ public void setTextContent(String value);
}
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 03:14:02 UTC (rev 757)
+++
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2008-10-29 04:17:14 UTC (rev 758)
@@ -2560,12 +2560,22 @@
* Assert a label for a given ID exists.
*/
public void assertLabelPresent(String id) {
+ Assert.assertNotNull("No label found with id [" + id + "]",
getLabel(id));
+ }
+
+ /**
+ * Get a label for a particular ID.
+ *
+ * @param id
+ * @return
+ */
+ private IElement getLabel(String id) {
// get all labels
for (IElement e : getTestingEngine().getElementsByXPath("//label")) {
if (e.getName().equals("label") &&
id.equals(e.getAttribute("id")))
- return; // label found
+ return e; // label found
}
- Assert.fail("No label found with id [" + id + "]");
+ return null;
}
/**
@@ -2605,12 +2615,12 @@
}
/**
- * Private method to test the value of a field connected to a particular
IElement label.
+ * Get all the fields that are referenced or contained in a particular
label.
*
- * @param label
- * @param fieldText
+ * @param label The label to consider
+ * @return A list of all fields contained or referenced in this label
*/
- private void assertLabeledFieldEquals(String identifier, IElement label,
String fieldText) {
+ private List<IElement> getFieldsForLabel(IElement label) {
List<IElement> fields = new java.util.ArrayList<IElement>();
// a direct "for" attribute
if (label.getAttribute("for") != null) {
@@ -2628,7 +2638,19 @@
}
}
}
+
+ return fields;
+ }
+ /**
+ * 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 = getFieldsForLabel(label);
+
Assert.assertFalse("No field found for label [" + identifier + "]",
fields.isEmpty());
String value = null;
// cycle through all fields trying to find value
@@ -2682,12 +2704,66 @@
* @param fieldText
*/
public void assertLabeledFieldEquals(String id, String fieldText) {
- IElement label = getTestingEngine().getElementByID(id);
+ IElement label = getLabel(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);
}
+
+ public void setLabeledFormElementField(String id, String value) {
+ IElement label = getLabel(id);
+ Assert.assertNotNull("no label for id [" + id + "] found", label);
+
+ List<IElement> fields = getFieldsForLabel(label);
+ Assert.assertNotSame("there should be at least one element referenced
for label [" + id + "]", 0, fields.size());
+
+ // find the first element that we can change
+ for (IElement field : fields) {
+ 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
(value.equals(field.getAttribute("value"))) {
+ field.setAttribute("checked");
+ return;
+ }
+ } else if
(field.getAttribute("type").toLowerCase().equals("radio")) {
+ if
(value.equals(field.getAttribute("value"))) {
+ field.setAttribute("checked");
+ return;
+ }
+ } else {
+ // any other input type
+ field.setAttribute("value", value);
+ return;
+ }
+ } else {
+ // unspecified input type, default = text
+ field.setAttribute("value", value);
+ return;
+ }
+ } else if ("textarea".equals(field.getName())) {
+ field.setTextContent(value);
+ return;
+ } else if ("select".equals(field.getName())) {
+ // get the selected option
+ for (IElement children : field.getChildren()) {
+ // find the option which matches the given
value (we can't specify random values)
+ if (children.getName().equals("option") &&
+ (children.getAttribute("value")
== null ? value.equals(children.getTextContent()) :
value.equals(children.getAttribute("value")))) {
+ children.setAttribute("selected");
+ return;
+ }
+ }
+ } else {
+ throw new RuntimeException("Unexpected field type " +
field.getName());
+ }
+ }
+
+ Assert.fail("could not find any fields for label [" + id + "] to set.");
+ }
// 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 03:14:02 UTC (rev 757)
+++
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
2008-10-29 04:17:14 UTC (rev 758)
@@ -114,4 +114,28 @@
return "IElement[name=" + getName() + " wrapped=" + element +
"]";
}
+
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
+ */
+ public void setAttribute(String string) {
+ element.setAttribute(string, "1");
+ }
+
+
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String,
java.lang.String)
+ */
+ public void setAttribute(String string, String value) {
+ element.setAttribute(string, value);
+ }
+
+
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
+ */
+ public void setTextContent(String value) {
+ element.setTextContent(value);
+ }
+
}
Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml 2008-10-29 03:14:02 UTC (rev 757)
+++ trunk/src/changes/changes.xml 2008-10-29 04:17:14 UTC (rev 758)
@@ -18,7 +18,7 @@
Added WebTestCase(WebTester) constructor
</action>
<action type="add" dev="jevonwright" issue="1519807">
- Added assertLabeledFieldEquals(id, text) and
assertLabel*() methods
+ Added assertLabeledFieldEquals(id, text),
assertLabel*(), and setLabeledFormElementField(id, value) 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).
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