Here is the patch I propose.
Any remarks, suggestions are welcome :o)
Fred.
On Wednesday 26 July 2006 13:39, Julien HENRY wrote:
> You're right, assertHiddenFieldPresent is much better, and
> assertTextFieldEquals should work for the same objects as setTextField.
> i.e. <input type=text>, <input type=password> and <textarea>
>
> And welcome to the dev mailing list ;)
>
> Julien
>
> ----- Message d'origine ----
> De : Fred <[EMAIL PROTECTED]>
> À : Julien HENRY <[EMAIL PROTECTED]>;
> [email protected] Envoyé le : Mercredi, 26 Juillet
> 2006, 1h06mn 04s
> Objet : Re: New patch
>
>
> Ok... First, I subscribed to dev mailing list and put it in the reception
> field of this mail... and I say hello to all jwebunit developers :o)
>
> A bit of summary what Julien and I were discussing about:
> assertFormElementEquals does not support textarea. Julien suggested me
> another solution. (see discussion below this mail).
>
> Now I have several comments/ideas about Julien's feedback:
> * I assume assertTextFieldEquals should cover textareas and not only input
> text fields for consistency reasons.
> * I don't see what assertHiddenFieldEquals could check ? We could add
> assertHiddenFieldPresent(String inputName, String inputValue) ?
>
> I will start implementing assertTextFieldEquals... let me know your point
> of view about assertHiddenFieldEquals :o)
>
> Regards,
>
> Fred.
>
> On Wednesday 26 July 2006 12:28, Julien HENRY wrote:
> > Hum, I don't like assertFormElementEquals. I only leave it to preserve
> > backward compatibility. I think we should provide more precise methods,
> > like : - assertCheckBoxSelected
> > - assertRadioOptionSelected
> > - assertSelectedOptionEquals
> > We could add :
> > - assertTextFieldEquals
> > - assertHiddenFieldEquals
> >
> > With all this methods, we could deprecated assertFormElementEquals in
> > future releases.
> >
> > So, if you agree this point of view, could you please create the method
> > assertTextFieldEquals?
> >
> > You can continue to send patch, but use dev mailing list, as I am not the
> > only developper, and it allows the other to follow the evolutions of
> > jWebUnit. Moreover, if you continue to work for jwebunit, you will be
> > promoted committer very soon ;)
> >
> > ++
> >
> > Julien
> >
> > ----- Message d'origine ----
> > De : Fred <[EMAIL PROTECTED]>
> > À : Julien HENRY <[EMAIL PROTECTED]>
> > Envoyé le : Mercredi, 26 Juillet 2006, 11h36mn 30s
> > Objet : New patch
> >
> >
> > Hi Julien,
> >
> > I'm still working with textareas and seen that something was missing in
> > order to check the text of a textarea.
> > I was using the method assertFormElementEquals which ended up by the
> > RuntimeException. That's the reason why I sent you the first patch...
> > At first glance, I though my test was wrong but after a bit of
> > investigation, I didn't find a way to check the text of a textarea and
> > that the method I was using (assertFormElementEquals) was the right one
> > but incomplete.
> > So I created a new patch and made a test case for that... You will find
> > it out in attachment.
> >
> > Let me know if it is ok for you.
> >
> > Er... also... let me know if directly sending patches to you without
> > discussing about the topic on the mailing list is ok for you :o) Or if
> > you want me to process another way when I submit patches :o)
> >
> > Regards,
> >
> > Fred.
> >
> >
> > *DISCLAIMER*
> > This e-mail (including any attachments) may contain information which is
> > privileged or confidential or constitute non-public information.It is to
> > be conveyed only to the intended recipient(s).If you received this e-mail
> > in error, please notify the sender immediately by e-mail or telephone and
> > delete the e-mail from your system without reading, copying or disclosing
> > its contents to any other person.
> >
> > Index:
> > jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormA
> >ss ertionsTest.java
> > =================================================================== ---
> > jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormA
> >ss ertionsTest.java (revision 535) +++
> > jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormA
> >ss ertionsTest.java (working copy) @@ -59,6 +59,8 @@
> > beginAt("/testPage.html");
> > assertPass("assertFormElementEquals", new
> > Object[]{"testInputElement", "testValue"});
> > assertPass("assertFormElementEquals", new Object[]{"submitButton",
> > "buttonLabel"}); + assertPass("assertFormElementEquals", new
> > Object[]{"textarea", "sometexthere"}); +
> > assertPass("assertFormElementEquals", new Object[]{"cool", "dog"});
> > assertFail("assertFormElementEquals", new Object[]{"testInputElement",
> > "noSuchValue"}); assertFail("assertFormElementEquals", new
> > Object[]{"noSuchElement", "testValue"}); }
> > Index:
> > jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/te
> >st Page.html
> > =================================================================== ---
> > jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/te
> >st Page.html (revision 535) +++
> > jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/te
> >st Page.html (working copy) @@ -36,6 +36,7 @@
> > <option value="3">three</option>
> > <option value="4">four</option>
> > </select> <input type="reset" name="resetButton" /></form>
> > + <form id="form5"><textarea
> > name="textarea">sometexthere</textarea></form> </td>
> > </tr>
> > </table>
> > Index:
> > jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/
> >Ht mlUnitDialog.java
> > =================================================================== ---
> > jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/
> >Ht mlUnitDialog.java (revision 535) +++
> > jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/
> >Ht mlUnitDialog.java (working copy) @@ -314,18 +314,23 @@
> > HtmlRadioButtonInput rbtn =
> > getForm().getCheckedRadioButton(paramName); if (rbtn != null)
> > return rbtn.getValueAttribute();
> > - try {
> > - // TODO What should I return when it is a multi-select
> > - return ((HtmlOption) getForm().getSelectByName(paramName)
> > - .getSelectedOptions().get(0)).getValueAttribute();
> > - } catch (ElementNotFoundException e) {
> > -
> > + List formElements = getForm().getSelectsByName(paramName);
> > + // TODO What should I return when it is a multi-select
> > + if (!formElements.isEmpty()) {
> > + HtmlSelect selectElement = (HtmlSelect) formElements.get(0);
> > + HtmlOption option = (HtmlOption)
> > selectElement.getSelectedOptions().get(0); + return
> > option.getValueAttribute();
> > }
> > - try {
> > - return
> > getForm().getInputByName(paramName).getValueAttribute(); - } catch
> > (ElementNotFoundException e) {
> > -
> > + formElements = getForm().getHtmlElementsByAttribute("input",
> > "name", paramName); + if (!formElements.isEmpty()) {
> > + HtmlInput inputElement = (HtmlInput) formElements.get(0);
> > + return inputElement.getValueAttribute();
> > }
> > + formElements = getForm().getTextAreasByName(paramName);
> > + if (!formElements.isEmpty()) {
> > + HtmlTextArea textAreaElement = (HtmlTextArea)
> > formElements.get(0); + return textAreaElement.getText();
> > + }
> > throw new RuntimeException("getFormParameterValue failed");
> > }
>
> *DISCLAIMER*
> This e-mail (including any attachments) may contain information which is
> privileged or confidential or constitute non-public information.It is to be
> conveyed only to the intended recipient(s).If you received this e-mail in
> error, please notify the sender immediately by e-mail or telephone and
> delete the e-mail from your system without reading, copying or disclosing
> its contents to any other person.
*DISCLAIMER*
This e-mail (including any attachments) may contain information which is
privileged or confidential or constitute non-public information.It is to be
conveyed only to the intended recipient(s).If you received this e-mail in
error, please notify the sender immediately by e-mail or telephone and delete
the e-mail from your system without reading, copying or disclosing its contents
to any other person.
Index: jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java
===================================================================
--- jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java (revision 535)
+++ jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormAssertionsTest.java (working copy)
@@ -57,10 +57,17 @@
public void testAssertFormElementEquals() throws Throwable {
beginAt("/testPage.html");
- assertPass("assertFormElementEquals", new Object[]{"testInputElement", "testValue"});
- assertPass("assertFormElementEquals", new Object[]{"submitButton", "buttonLabel"});
- assertFail("assertFormElementEquals", new Object[]{"testInputElement", "noSuchValue"});
- assertFail("assertFormElementEquals", new Object[]{"noSuchElement", "testValue"});
+ assertPass("assertTextFieldEquals", new Object[]{"testInputElement", "testValue"});
+ assertPass("assertSubmitButtonPresent", new Object[]{"submitButton", "buttonLabel"});
+ assertPass("assertTextFieldEquals", new Object[]{"textarea", "sometexthere"});
+ assertPass("assertRadioOptionSelected", new Object[]{"cool", "dog"});
+ assertPass("assertHiddenFieldPresent", new Object[]{"hiddenelement", "hiddenvalue"});
+ assertFail("assertTextFieldEquals", new Object[]{"testInputElement", "noSuchValue"});
+ assertFail("assertTextFieldEquals", new Object[]{"noSuchElement", "testValue"});
+ assertFail("assertHiddenFieldPresent", new Object[]{"noSuchElement", "testValue"});
+ assertFail("assertHiddenFieldPresent", new Object[]{"hiddenelement", "notThisValue"});
+ assertFail("assertTextFieldEquals", new Object[]{"passwordelement", "noSuchValue"});
+ assertPass("assertTextFieldEquals", new Object[]{"passwordelement", "password"});
}
public void testCheckboxSelected() throws Throwable {
Index: jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebAssertionsTest.java
===================================================================
--- jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebAssertionsTest.java (revision 535)
+++ jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebAssertionsTest.java (working copy)
@@ -148,10 +148,23 @@
assertMatchInElement("outer_id", "Inner.*Text");
}
+ /**
+ * @deprecated
+ */
public void testAssertFormElementEquals() throws Throwable {
assertFormElementEquals("testInputElement", "testValue");
assertFail("assertFormElementEquals", new Object[] {"testInputElement", "AnotherValue"});
}
+
+ public void testAssertTextFieldEquals() throws Throwable {
+ assertTextFieldEquals("testInputElement", "testValue");
+ assertFail("assertTextFieldEquals", new Object[] {"testInputElement", "AnotherValue"});
+ }
+
+ public void testAssertHiddenFieldPresent() throws Throwable {
+ assertHiddenFieldPresent("hidden", "h");
+ assertFail("assertHiddenFieldPresent", new Object[] {"hidden", "AnotherValue"});
+ }
public void testAssertFormElementMatch() throws Throwable {
assertFormElementMatch("testInputElement", "test[Vv]alue");
Index: jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/CharsetTest.java
===================================================================
--- jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/CharsetTest.java (revision 535)
+++ jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/CharsetTest.java (working copy)
@@ -14,33 +14,33 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jesse Wilson</a>
*/
public class CharsetTest extends JWebUnitAPITestCase {
- public CharsetTest(String name) {
- super(name);
- }
+ public CharsetTest(String name) {
+ super(name);
+ }
- public static Test suite() {
- return new JettySetup(new TestSuite(CharsetTest.class));
- }
+ public static Test suite() {
+ return new JettySetup(new TestSuite(CharsetTest.class));
+ }
- public void setUp() throws Exception {
- super.setUp();
- getTestContext().setBaseUrl(HOST_PATH+"/CharsetTest");
- beginAt("/charset.html_utf-8");
- }
+ public void setUp() throws Exception {
+ super.setUp();
+ getTestContext().setBaseUrl(HOST_PATH+"/CharsetTest");
+ beginAt("/charset.html_utf-8");
+ }
- public void testEuro() {
- assertFormElementEquals("eur", "\u20AC");
- }
+ public void testEuro() {
+ assertTextFieldEquals("eur", "\u20AC");
+ }
- public void testDollar() {
- assertFormElementEquals("usd", "$");
- }
+ public void testDollar() {
+ assertTextFieldEquals("usd", "$");
+ }
- public void testYen() {
- assertFormElementEquals("yen", "\u00A5");
- }
+ public void testYen() {
+ assertTextFieldEquals("yen", "\u00A5");
+ }
- public void testPound() {
- assertFormElementEquals("gbp", "\u00A3");
- }
+ public void testPound() {
+ assertTextFieldEquals("gbp", "\u00A3");
+ }
}
\ No newline at end of file
Index: jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptEventsTest.java
===================================================================
--- jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptEventsTest.java (revision 535)
+++ jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptEventsTest.java (working copy)
@@ -99,9 +99,9 @@
public void testGreenLink() {
beginAt("index.html");
- assertFormElementEquals("color", "blue");
+ assertTextFieldEquals("color", "blue");
clickLink("SetColorGreen");
- assertFormElementEquals("color", "green");
+ assertTextFieldEquals("color", "green");
}
public void testFormOnSubmitSetTarget() {
Index: jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html
===================================================================
--- jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html (revision 535)
+++ jwebunit-commons-tests/src/main/resources/testcases/FormAssertionsTest/testPage.html (working copy)
@@ -36,6 +36,9 @@
<option value="3">three</option>
<option value="4">four</option>
</select> <input type="reset" name="resetButton" /></form>
+ <form id="form5"><textarea name="textarea">sometexthere</textarea>
+ <input type="hidden" name="hiddenelement" value="hiddenvalue"/>
+ <input type="password" name="passwordelement" value="password"></form>
</td>
</tr>
</table>
Index: jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
===================================================================
--- jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java (revision 535)
+++ jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java (working copy)
@@ -40,6 +40,7 @@
import com.gargoylesoftware.htmlunit.WebWindowListener;
import com.gargoylesoftware.htmlunit.WebWindowNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
+import com.gargoylesoftware.htmlunit.html.HtmlHiddenInput;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
@@ -308,6 +309,7 @@
*
* @param paramName
* name of the input element.
+ * @deprecated
*/
public String getFormParameterValue(String paramName) {
checkFormStateWithInput(paramName);
@@ -328,8 +330,58 @@
}
throw new RuntimeException("getFormParameterValue failed");
}
+
+ /**
+ * Return the current value of a text input element with name <code>paramName</code>.
+ *
+ * @param paramName
+ * name of the input element.
+ * TODO: Find a way to handle multiple text input element with same name.
+ */
+ public String getTextFieldParameterValue(String paramName) {
+ checkFormStateWithInput(paramName);
+ List textFieldElements = getForm().getHtmlElementsByAttribute("input", "type", "text");
+ textFieldElements.addAll(getForm().getHtmlElementsByAttribute("input", "type", "password"));
+ Iterator it = textFieldElements.iterator();
+ while(it.hasNext()) {
+ HtmlInput input = (HtmlInput) it.next();
+ if (paramName.equals(input.getNameAttribute())) {
+ return input.getValueAttribute();
+ }
+ }
+ // If no text field with the name paramName then try with textareas.
+ textFieldElements = getForm().getTextAreasByName(paramName);
+ it = textFieldElements.iterator();
+ while(it.hasNext()) {
+ HtmlTextArea textInput = (HtmlTextArea) it.next();
+ if (paramName.equals(textInput.getNameAttribute())) {
+ return textInput.getText();
+ }
+ }
+ throw new RuntimeException("getTextFieldParameterValue failed, text field with name [" + paramName + "] does not exist.");
+ }
/**
+ * Return the current value of a hidden input element with name <code>paramName</code>.
+ *
+ * @param paramName
+ * name of the input element.
+ * TODO: Find a way to handle multiple hidden input element with same name.
+ */
+ public String getHiddenFieldParameterValue(String paramName) {
+ checkFormStateWithInput(paramName);
+ List textFieldElements = getForm().getHtmlElementsByAttribute("input", "type", "hidden");
+ Iterator it = textFieldElements.iterator();
+ while(it.hasNext()) {
+ HtmlHiddenInput textInput = (HtmlHiddenInput) it.next();
+ if (paramName.equals(textInput.getNameAttribute())) {
+ return textInput.getValueAttribute();
+ }
+ }
+ throw new RuntimeException("getHiddenFieldParameterValue failed, hidden field with name [" + paramName + "] does not exist.");
+ }
+
+ /**
* Set a form text, password input element or textarea to the provided value.
*
* @param fieldName
Index: jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java
===================================================================
--- jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java (revision 535)
+++ jwebunit-core/src/main/java/net/sourceforge/jwebunit/IJWebUnitDialog.java (working copy)
@@ -175,8 +175,25 @@
*
* @param paramName
* name of the input element.
+ * @deprecated
*/
String getFormParameterValue(String paramName);
+
+ /**
+ * Return the current value of a text input element with name <code>paramName</code>.
+ *
+ * @param paramName
+ * name of the input element.
+ */
+ String getTextFieldParameterValue(String paramName);
+
+ /**
+ * Return the current value of a hidden input element with name <code>paramName</code>.
+ *
+ * @param paramName
+ * name of the input element.
+ */
+ String getHiddenFieldParameterValue(String paramName);
/**
* Fill a text, password or textarea field with the provided text.
Index: jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
===================================================================
--- jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java (revision 535)
+++ jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java (working copy)
@@ -718,6 +718,7 @@
*
* @param formElementName
* @param expectedValue
+ * @deprecated
*/
public void assertFormElementEquals(String formElementName,
String expectedValue) {
@@ -757,6 +758,34 @@
}
/**
+ * Assert that an input text element with name <code>formElementName</code> has
+ * the <code>expectedValue</code> value.
+ *
+ * @param formElementName
+ * the value of the name attribute of the element
+ * @param expectedValue
+ * the expected value of the given input element
+ */
+ public void assertTextFieldEquals(String formElementName, String expectedValue) {
+ assertFormElementPresent(formElementName);
+ Assert.assertEquals(expectedValue, getDialog().getTextFieldParameterValue(formElementName));
+ }
+
+ /**
+ * Assert that an input hidden element with name <code>formElementName</code> has
+ * the <code>expectedValue</code> value.
+ *
+ * @param formElementName
+ * the value of the name attribute of the element
+ * @param expectedValue
+ * the expected value of the given input element
+ */
+ public void assertHiddenFieldPresent(String formElementName, String expectedValue) {
+ assertFormElementPresent(formElementName);
+ Assert.assertEquals(expectedValue, getDialog().getHiddenFieldParameterValue(formElementName));
+ }
+
+ /**
* Assert that a specific checkbox is selected.
*
* @param checkBoxName
@@ -2147,7 +2176,7 @@
returned[i]);
}
}
-
+
/**
* Set the value of a form input element.
*
Index: jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java
===================================================================
--- jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java (revision 535)
+++ jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTestCase.java (working copy)
@@ -265,6 +265,9 @@
getTester().assertFormNotPresent(formName);
}
+ /**
+ * @deprecated
+ */
public void assertFormElementEquals(String formElementName,
String expectedValue) {
getTester().assertFormElementEquals(formElementName, expectedValue);
@@ -277,6 +280,14 @@
public void assertFormElementEmpty(String formElementName) {
getTester().assertFormElementEmpty(formElementName);
}
+
+ public void assertTextFieldEquals(String formElementName, String expectedValue) {
+ getTester().assertTextFieldEquals(formElementName, expectedValue);
+ }
+
+ public void assertHiddenFieldPresent(String formElementName, String expectedValue) {
+ getTester().assertHiddenFieldPresent(formElementName, expectedValue);
+ }
public void assertCheckboxSelected(String checkBoxName) {
getTester().assertCheckboxSelected(checkBoxName);
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development