[JWebUnit-development] SF.net SVN: jwebunit:[978] trunk
Brought to you by:
henryju
From: <he...@us...> - 2015-07-13 16:24:07
|
Revision: 978 http://sourceforge.net/p/jwebunit/code/978 Author: henryju Date: 2015-07-13 16:24:04 +0000 (Mon, 13 Jul 2015) Log Message: ----------- #134 WebTester.submit() does not find submit button Modified Paths: -------------- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html 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/ButtonAssertionsTest.java =================================================================== --- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java 2015-07-13 16:24:04 UTC (rev 978) @@ -34,9 +34,11 @@ @Test public void testAssertButtonwithOneFormfound() { beginAt("/pageWithOneForm.html"); + assertSubmitButtonPresent(); assertButtonPresent("button1"); assertButtonPresent("buttonOutside"); setWorkingForm("form1"); + assertSubmitButtonPresent(); assertButtonPresent("button1"); assertButtonPresent("buttonOutside"); assertButtonPresent("button1"); Modified: trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java =================================================================== --- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java 2015-07-13 16:24:04 UTC (rev 978) @@ -58,6 +58,10 @@ setTextField("color", "red"); submit(); assertTextPresent("color=[red]"); + clickLink("return"); + setTextField("color", "black"); + submit("button2"); + assertTextPresent("color=[black]"); } @Test Modified: trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html =================================================================== --- trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html 2015-07-13 16:24:04 UTC (rev 978) @@ -25,7 +25,7 @@ <html> <body> <form id="form1"> -<button id="button1">Testbutton</button> +<button id="button1" type="submit">Testbutton</button> </form> <button id="buttonOutside">Outside</button> <input type="button" value="Input button" /> Modified: trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html =================================================================== --- trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html 2015-07-13 16:24:04 UTC (rev 978) @@ -28,6 +28,7 @@ <input type="submit" name="button" value="click me"> <input type="hidden" name="myReferer" value="FormSubmissionTest/SingleNamedButtonForm.html"> <input type="hidden" name="hidden" value="foo"> + <button type="submit" name="button2">A Button</button> </form> </body> </html> \ No newline at end of file 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 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java 2015-07-13 16:24:04 UTC (rev 978) @@ -1239,9 +1239,11 @@ List<HtmlElement> btns = new LinkedList<HtmlElement>(); if (form != null) { btns.addAll(getForm().getInputsByName(buttonName)); + btns.addAll(getForm().getButtonsByName(buttonName)); } else { for (HtmlForm f : getCurrentPage().getForms()) { btns.addAll(f.getInputsByName(buttonName)); + btns.addAll(f.getButtonsByName(buttonName)); } } for (HtmlElement o : btns) { @@ -1276,9 +1278,11 @@ List<HtmlElement> btns = new LinkedList<HtmlElement>(); if (form != null) { btns.addAll(getForm().getInputsByName(buttonName)); + btns.addAll(getForm().getButtonsByName(buttonName)); } else { for (HtmlForm f : getCurrentPage().getForms()) { btns.addAll(f.getInputsByName(buttonName)); + btns.addAll(f.getButtonsByName(buttonName)); } } for (HtmlElement o : btns) { @@ -1314,9 +1318,11 @@ List<HtmlElement> btns = new LinkedList<HtmlElement>(); if (form != null) { btns.addAll(getForm().getInputsByName(buttonName)); + btns.addAll(getForm().getButtonsByName(buttonName)); } else { for (HtmlForm f : getCurrentPage().getForms()) { btns.addAll(f.getInputsByName(buttonName)); + btns.addAll(f.getButtonsByName(buttonName)); } } for (HtmlElement o : btns) { @@ -1351,16 +1357,54 @@ } return null; } + + private HtmlElement getSubmitButton() { + List<HtmlElement> btns = new LinkedList<HtmlElement>(); + if (form != null) { + btns.addAll(getForm().getElementsByAttribute("input", "type", "submit")); + btns.addAll(getForm().getElementsByAttribute("input", "type", "image")); + btns.addAll(getForm().getElementsByAttribute("button", "type", "submit")); + } else { + for (HtmlForm f : getCurrentPage().getForms()) { + btns.addAll(f.getElementsByAttribute("input", "type", "submit")); + btns.addAll(f.getElementsByAttribute("input", "type", "image")); + btns.addAll(f.getElementsByAttribute("button", "type", "submit")); + } + } + for (HtmlElement o : btns) { + if (o instanceof HtmlSubmitInput) { + HtmlSubmitInput btn = (HtmlSubmitInput) o; + if (form == null) { + form = btn.getEnclosingFormOrDie(); + } + return btn; + } + if (o instanceof HtmlImageInput) { + HtmlImageInput btn = (HtmlImageInput) o; + if (form == null) { + form = btn.getEnclosingFormOrDie(); + } + return btn; + } + if (o instanceof HtmlButton) { + HtmlButton btn = (HtmlButton) o; + if (btn.getTypeAttribute().equals("submit")) { + if (form == null) { + form = btn.getEnclosingFormOrDie(); + } + return btn; + } + } + } + return null; + } /** * {@inheritDoc} */ @Override public boolean hasSubmitButton() { - final HtmlForm htmlForm = getForm(); - List<?> l = htmlForm.getByXPath("//input[@type='submit' or @type='image']"); - List<?> l2 = htmlForm.getByXPath("//button[@type='submit']"); - return (l.size() > 0 || l2.size() > 0); + return getSubmitButton() != null; } /** @@ -1589,36 +1633,20 @@ */ @Override public void submit() { + HtmlElement btn = getSubmitButton(); + if (btn == null) { + throw new RuntimeException("No submit button found in current form."); + } try { - Object[] inpt = getForm().getHtmlElementsByTagName("input") - .toArray(); - for (int i = 0; i < inpt.length; i++) { - if (inpt[i] instanceof HtmlSubmitInput) { - ((HtmlSubmitInput) inpt[i]).click(); - return; - } - if (inpt[i] instanceof HtmlImageInput) { - ((HtmlImageInput) inpt[i]).click(); - return; - } - if (inpt[i] instanceof HtmlButton - && ((HtmlButton) inpt[i]).getTypeAttribute().equals( - "submit")) { - ((HtmlButton) inpt[i]).click(); - return; - } - } - + btn.click(); } catch (FailingHttpStatusCodeException e) { throw new TestingEngineResponseException( - e.getStatusCode(), e); + e.getStatusCode(), e); } catch (IOException e) { throw new RuntimeException( - "HtmlUnit Error submitting form using default submit button, " - + "check that form has single submit button, otherwise use submit(name): \n", - e); + "HtmlUnit Error submitting form using default submit button, " + + "check that form has single submit button, otherwise use submit(name): \n", e); } - throw new RuntimeException("No submit button found in current form."); } /** @@ -1629,37 +1657,19 @@ */ @Override public void submit(String buttonName) { - List<HtmlElement> l = new LinkedList<HtmlElement>(); - l.addAll(getForm().getInputsByName(buttonName)); - l.addAll(getForm().getButtonsByName(buttonName)); + HtmlElement btn = getSubmitButton(buttonName); + if (btn == null) { + throw new RuntimeException("No submit button found in current form."); + } try { - for (HtmlElement o : l) { - if (o instanceof HtmlSubmitInput) { - HtmlSubmitInput inpt = (HtmlSubmitInput) o; - inpt.click(); - return; - } - if (o instanceof HtmlImageInput) { - HtmlImageInput inpt = (HtmlImageInput) o; - inpt.click(); - return; - } - if (o instanceof HtmlButton) { - HtmlButton inpt = (HtmlButton) o; - if (inpt.getTypeAttribute().equals("submit")) { - inpt.click(); - return; - } - } - } + btn.click(); } catch (FailingHttpStatusCodeException e) { throw new TestingEngineResponseException( - e.getStatusCode(), e); + e.getStatusCode(), e); } catch (IOException e) { throw new RuntimeException( - "HtmlUnit Error submitting form using default submit button", e); + "HtmlUnit Error submitting form using default submit button", e); } - throw new RuntimeException("No submit button found in current form."); } /** Modified: trunk/src/changes/changes.xml =================================================================== --- trunk/src/changes/changes.xml 2015-07-13 15:52:13 UTC (rev 977) +++ trunk/src/changes/changes.xml 2015-07-13 16:24:04 UTC (rev 978) @@ -31,6 +31,9 @@ </properties> <body> <release version="3.3" date="UNKNOWN" description="HtmlUnit 2.17, Java 7"> + <action type="fix" dev="henryju" issue="134" due-to="Joerg Troester"> + WebTester.submit() does not find submit button. + </action> <action type="update" dev="henryju"> Updated to HtmlUnit 2.17. </action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |