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

Commit 5319a00

Browse files
committed
[1793818] Added setHiddenFieldValue method to WebTestCase
1 parent c8549cb commit 5319a00

7 files changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ public void testClickButtonWithText1() {
310310
assertTextPresent("Submitted parameters");
311311
assertTextPresent("color=blue");
312312
}
313+
314+
public void testSetHiddenField() {
315+
beginAt("/SingleNamedButtonForm.html");
316+
assertHiddenFieldPresent("hidden", "foo");
317+
setHiddenField("hidden", "bar");
318+
submit();
319+
assertTextPresent("hidden=bar");
320+
}
313321

314322

315323
}

jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<input type="CHECKBOX" name="checkBox" />
88
<input type="submit" name="button" value="click me">
99
<input type="hidden" name="myReferer" value="FormSubmissionTest/SingleNamedButtonForm.html">
10+
<input type="hidden" name="hidden" value="foo">
1011
</form>
1112
</body>
1213
</html>

jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/ITestingEngine.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ void closeBrowser() throws ExpectedJavascriptAlertException,
205205
* @param text value to type in the field.
206206
*/
207207
void setTextField(String inputName, String text);
208+
209+
/**
210+
* Fill hidden field with the provided text.
211+
*
212+
* @param inputName name of the hidden element
213+
* @param text value to set in the hidden field.
214+
*/
215+
void setHiddenField(String inputName, String text);
208216

209217
/**
210218
* Return a string array of select box option values.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,18 @@ public void setTextField(String inputName, String value) {
21152115
assertFormElementPresent(inputName);
21162116
getTestingEngine().setTextField(inputName, value);
21172117
}
2118+
2119+
/**
2120+
* Set the value of an hidden input field.
2121+
*
2122+
* @param inputName name of form element.
2123+
* @param value value to set.
2124+
*/
2125+
public void setHiddenField(String inputName, String value) {
2126+
assertFormPresent();
2127+
assertFormElementPresent(inputName);
2128+
getTestingEngine().setHiddenField(inputName, value);
2129+
}
21182130

21192131
/**
21202132
* Select a specified checkbox. If the checkbox is already checked then the checkbox will stay checked.

jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,37 @@ public void setTextField(String fieldName, String text) {
496496
throw new RuntimeException("No text field with name [" + fieldName
497497
+ "] was found.");
498498
}
499+
500+
/**
501+
* Set a form hidden element to the provided value.
502+
*
503+
* @param fieldName name of the hidden input element
504+
* @param paramValue parameter value to submit for the element.
505+
*/
506+
public void setHiddenField(String fieldName, String text) {
507+
List hiddenFieldElements = new LinkedList();
508+
if (form != null) {
509+
hiddenFieldElements.addAll(getForm().getInputsByName(fieldName));
510+
} else {
511+
for (Iterator i = getCurrentPage().getForms().iterator(); i
512+
.hasNext();) {
513+
HtmlForm f = (HtmlForm) i.next();
514+
hiddenFieldElements.addAll(f.getInputsByName(fieldName));
515+
}
516+
}
517+
for (Iterator i = hiddenFieldElements.iterator(); i.hasNext();) {
518+
HtmlElement e = (HtmlElement) i.next();
519+
if (e instanceof HtmlHiddenInput) {
520+
((HtmlHiddenInput) e).setValueAttribute(text);
521+
if (form == null) {
522+
form = e.getEnclosingFormOrDie();
523+
}
524+
return;
525+
}
526+
}
527+
throw new RuntimeException("No hidden field with name [" + fieldName
528+
+ "] was found.");
529+
}
499530

500531
/**
501532
* Return a string array of select box option values.

jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ public void setTextField(String inputName, String text) {
427427
String xpath2 = formSelector() + "//textarea[@name='"+inputName+"']";
428428
selenium.type("xpath=" + xpath1 + "|" + xpath2, text);
429429
}
430+
431+
public void setHiddenField(String inputName, String text) {
432+
String xpath = formSelector() + "//input[@name='"+inputName+"' and @type='hidden']";
433+
selenium.type("xpath=" + xpath, text);
434+
}
430435

431436
public void setWorkingForm(String nameOrId, int index) {
432437
if (nameOrId != null)

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
</properties>
99
<body>
1010
<release version="1.5" date="UNKNOW" description="The latest major release before Java 1.5 migration">
11+
<action type="add" dev="henryju" issue="1793818" due-to="Achim Huegen">
12+
Added setHiddenField method to WebTestCase.
13+
</action>
1114
<action type="fix" dev="henryju" issue="1747033" due-to="Carlo Possati">
1215
assertCheckbox[Not]Present now works if HTML type attribute value is not lowercase (e.g.: type="CHECKBOX").
1316
</action>

0 commit comments

Comments
 (0)