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

Commit 41e409e

Browse files
committed
[ 1845893 ] No way to click input of type button. Applied patch from Mike with some modifications and test cases added.
1 parent 2033762 commit 41e409e

6 files changed

Lines changed: 93 additions & 20 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,26 @@ public void testAssertButtonwithTowFormsfound() {
3939
assertButtonPresent("button2");
4040
assertButtonPresent("buttonOutside");
4141
}
42+
43+
public void testAssertButtonWithText() {
44+
beginAt("/pageWithTwoForms.html");
45+
assertButtonPresentWithText("Testbutton");
46+
assertButtonPresentWithText("Testbutton2");
47+
assertButtonPresentWithText("Outside");
48+
setWorkingForm("form1");
49+
assertButtonPresentWithText("Testbutton");
50+
assertButtonPresentWithText("Testbutton2");
51+
assertButtonPresentWithText("Outside");
52+
assertButtonPresentWithText("the submit btn");
53+
assertButtonPresentWithText("the reset btn");
54+
assertButtonPresentWithText("the btn btn");
55+
setWorkingForm("form2");
56+
assertButtonPresentWithText("Testbutton");
57+
assertButtonPresentWithText("Testbutton2");
58+
assertButtonPresentWithText("Outside");
59+
assertButtonPresentWithText("Testbutton");
60+
assertButtonPresentWithText("Testbutton2");
61+
assertButtonPresentWithText("Outside");
62+
}
4263

4364
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,17 @@ public void testCachedForm() {
298298
submit();
299299
assertTextPresent("Page 3");
300300
}
301+
302+
/**
303+
* Submit input
304+
*/
305+
public void testClickButtonWithText1() {
306+
beginAt("/SingleNamedButtonForm.html");
307+
setTextField("color", "blue");
308+
clickButtonWithText("click me");
309+
assertTextPresent("Submitted parameters");
310+
assertTextPresent("color=blue");
311+
}
312+
301313

302314
}

jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
<html>
66
<body>
77
<form id="form1">
8-
<button id="button1">Testbutton</button>
8+
<button id="button1">Testbutton</button>
9+
<input type="submit" value="the submit btn"/>
10+
<input type="reset" value="the reset btn"/>
11+
<input type="button" value="the btn btn"/>
912
</form>
1013
<form id="form2">
11-
<button id="button2">Testbutton</button>
14+
<button id="button2">Testbutton2</button>
1215
</form>
1316
<button id="buttonOutside">Outside</button>
1417
</body>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<input type="text" name="color" />
66
<input type="hidden" name="checkBox"/>
77
<input type="checkbox" name="checkBox" />
8-
<input type="submit" name="button">
8+
<input type="submit" name="button" value="click me">
99
<input type="hidden" name="myReferer" value="FormSubmissionTest/SingleNamedButtonForm.html">
1010
</form>
1111
</body>

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

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,25 +1204,48 @@ private ClickableElement getButton(String buttonId) {
12041204
return null;
12051205
}
12061206

1207+
12071208
/**
1208-
* Checks if a button with <code>text</code> is present.
1209-
*
1210-
* @param text the text of the button (between &lt;button&gt;&lt;/button&gt;).
1209+
* Checks whether a button containing the specified text as its label exists.
1210+
* For HTML input tags of type submit, reset, or button, this checks the
1211+
* value attribute. For HTML button tags, this checks the element's
1212+
* content by converting it to text.
1213+
* @param text the text of the button (between &lt;button&gt;&lt;/button&gt;)
1214+
* or the value of the "value" attribute.
12111215
* @return <code>true</code> when the button with text could be found.
12121216
*/
12131217
public boolean hasButtonWithText(String text) {
1214-
boolean bReturn = getButtonWithText(text) != null ? true : false;
1215-
return bReturn;
1218+
return getButtonWithText(text) != null ? true : false;
12161219
}
12171220

1218-
public HtmlButton getButtonWithText(String buttonValueText) {
1219-
List l = ((HtmlPage) win.getEnclosedPage()).getDocumentElement()
1221+
/**
1222+
* Returns the first button that contains the specified text as its label.
1223+
* For HTML input tags of type submit, reset, or button, this checks the
1224+
* value attribute. For HTML button tags, this checks the element's
1225+
* content by converting it to text.
1226+
* @param buttonValueText the text of the button (between &lt;button&gt;&lt;/button&gt;)
1227+
* or the value of the "value" attribute.
1228+
* @return the ClickableElement with the specified text or null if
1229+
* no such button is found.
1230+
*/
1231+
public ClickableElement getButtonWithText(String buttonValueText) {
1232+
List l = ((HtmlPage) win.getEnclosedPage()).getDocumentHtmlElement()
12201233
.getHtmlElementsByTagNames(
1221-
Arrays.asList(new String[] { "button" }));
1234+
Arrays.asList(new String[] { "button", "input" }));
12221235
for (int i = 0; i < l.size(); i++) {
12231236
HtmlElement e = (HtmlElement) l.get(i);
1224-
if (((HtmlButton) e).asText().equals(buttonValueText))
1225-
return (HtmlButton) e;
1237+
if ( e instanceof HtmlButton )
1238+
{
1239+
if (((HtmlButton) e).asText().equals(buttonValueText))
1240+
return (ClickableElement) e;
1241+
}
1242+
else if ( e instanceof HtmlButtonInput ||
1243+
e instanceof HtmlSubmitInput ||
1244+
e instanceof HtmlResetInput )
1245+
{
1246+
if ( buttonValueText.equals(e.getAttributeValue("value")) )
1247+
return (ClickableElement)e;
1248+
}
12261249
}
12271250
return null;
12281251
}
@@ -1706,14 +1729,24 @@ public void clickButton(String buttonId) {
17061729
}
17071730
}
17081731

1732+
/**
1733+
* Clicks the first button that contains the specified text as its label.
1734+
* For HTML input tags of type submit, reset, or button, this checks the
1735+
* value attribute. For HTML button tags, this checks the element's
1736+
* content by converting it to text. or an HTML &lt;button&gt; tag.
1737+
*/
17091738
public void clickButtonWithText(String buttonValueText) {
1710-
try {
1711-
if (hasButtonWithText(buttonValueText)) {
1712-
getButtonWithText(buttonValueText).click();
1713-
}
1714-
} catch (Exception e) {
1715-
throw new RuntimeException(e);
1716-
}
1739+
ClickableElement b = getButtonWithText(buttonValueText);
1740+
if (b != null) {
1741+
try {
1742+
b.click();
1743+
} catch (Exception e) {
1744+
throw new RuntimeException(e);
1745+
}
1746+
}
1747+
else {
1748+
throw new RuntimeException("No button found with text: " + buttonValueText);
1749+
}
17171750
}
17181751

17191752
/**

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
</properties>
99
<body>
1010
<release version="1.5" date="UNKNOW">
11+
<action type="fix" dev="Julien Henry" issue="1845893" due-to="mtc3b">
12+
assertButtonPresentWithText and clickButtonWithText now work for input button
13+
(submit, reset, button) matching the value attribute.
14+
</action>
1115
<action type="update" dev="Julien Henry">
1216
Update to HtmlUnit 1.13.
1317
</action>

0 commit comments

Comments
 (0)