Revision: 490
Author: henryju
Date: 2006-06-21 08:09:31 -0700 (Wed, 21 Jun 2006)
ViewCVS: http://svn.sourceforge.net/jwebunit/?rev=490&view=rev
Log Message:
-----------
Add checkbox specific methods (assertCheckBoxPresent).
Update test cases to highlight HttpUnit limitations.
Add some test cases to JWebUnitTest of each plugin.
Ad missing files to SVN.
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/FormSubmissionCheckboxesTest.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/ExpectedTableAssertionsTest/TableAssertionsTestPageXHtml.html
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
trunk/jwebunit-jacobie-plugin/src/test/java/net/sourceforge/jwebunit/jacobie/JWebUnitTest.java
trunk/jwebunit-selenium-plugin/src/test/java/net/sourceforge/jwebunit/selenium/JWebUnitTest.java
Added Paths:
-----------
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
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
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ButtonAssertionsTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -4,17 +4,36 @@
public void setUp() throws Exception {
super.setUp();
+ getTestContext().setBaseUrl(HOST_PATH+"/ButtonAssertionsTest");
}
public void testAssertButtonwithOneFormfound() {
- beginAt("/pagewithOneform.html");
+ beginAt("/pageWithOneForm.html");
assertButtonPresent("button1");
+ assertButtonPresent("buttonOutside");
+ setWorkingForm("form1");
+ assertButtonPresent("button1");
+ assertButtonPresent("buttonOutside");
+ assertButtonPresent("button1");
+ assertButtonPresent("buttonOutside");
}
public void testAssertButtonwithTowFormsfound() {
- beginAt("/pagewithTowforms.html");
+ beginAt("/pageWithTwoForms.html");
assertButtonPresent("button1");
assertButtonPresent("button2");
+ assertButtonPresent("buttonOutside");
+ setWorkingForm("form1");
+ assertButtonPresent("button1");
+ assertButtonPresent("button2");
+ assertButtonPresent("buttonOutside");
+ setWorkingForm("form2");
+ assertButtonPresent("button1");
+ assertButtonPresent("button2");
+ assertButtonPresent("buttonOutside");
+ assertButtonPresent("button1");
+ assertButtonPresent("button2");
+ assertButtonPresent("buttonOutside");
}
}
Modified:
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionCheckboxesTest.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionCheckboxesTest.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionCheckboxesTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -56,7 +56,7 @@
private void doTestRadioBefore(int value) {
beginAt("CheckboxFormWithLabels.html");
checkCheckboxBeforeLabel("Radio " + value);
- assertFormElementEquals("radio", "" + value);
+ assertRadioOptionSelected("radio", "" + value);
submit();
assertTextPresent(" radio=" + value + " ");
}
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
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/FormSubmissionTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -40,10 +40,11 @@
public void testCheckBoxSelection() {
beginAt("/SingleNamedButtonForm.html");
- checkCheckbox("checkBox");
+ checkCheckbox("checkBox"); //Fail with httpunit because of
hidden field with same name
setTextField("color", "blue");
submit();
- assertTextPresent("Params are: color=blue checkBox=on");
+ //checkBox contains 2 parameters: one for the hidden input and one for
the checkbox
+ assertTextPresent("Params are: color=blue checkBox=,on");
}
public void testCheckBoxSelectionWithSameFieldName() {
@@ -66,8 +67,8 @@
public void testCheckBoxDeselection() {
beginAt("/SingleNamedButtonForm.html");
- checkCheckbox("checkBox");
- assertFormElementEquals("checkBox", "on");
+ checkCheckbox("checkBox"); //Fail with httpunit because of
hidden field with same name
+ assertCheckboxSelected("checkBox");
setTextField("color", "blue");
uncheckCheckbox("checkBox");
submit();
@@ -143,8 +144,8 @@
beginAt("/MultiFormPage.html");
setTextField("param2", "anyvalue");
submit("button2b");
- assertTextPresent(" param2=anyvalue ");
- assertTextPresent(" button2b=b2b ");
+ assertTextPresent("param2=anyvalue ");
+ assertTextPresent(" button2b=b2b");
}
public void testSubmissionReset() {
@@ -152,8 +153,8 @@
setTextField("param2", "anyvalue");
reset();
submit("button2b");
- assertTextNotPresent(" param2=anyvalue ");
- assertTextPresent(" button2b=b2b ");
+ assertTextNotPresent("param2=anyvalue ");
+ assertTextPresent(" button2b=b2b");
}
public void testSelectOption() {
Modified:
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html
===================================================================
---
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithOneForm.html
2006-06-21 15:09:31 UTC (rev 490)
@@ -7,5 +7,6 @@
<form id="form1">
<button id="button1">Testbutton</button>
</form>
+<button id="buttonOutside">Outside</button>
</body>
</html>
Added:
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
===================================================================
---
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
(rev 0)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/ButtonAssertionsTest/pageWithTwoForms.html
2006-06-21 15:09:31 UTC (rev 490)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+<body>
+<form id="form1">
+<button id="button1">Testbutton</button>
+</form>
+<form id="form2">
+<button id="button2">Testbutton</button>
+</form>
+<button id="buttonOutside">Outside</button>
+</body>
+</html>
Modified:
trunk/jwebunit-commons-tests/src/main/resources/testcases/ExpectedTableAssertionsTest/TableAssertionsTestPageXHtml.html
===================================================================
---
trunk/jwebunit-commons-tests/src/main/resources/testcases/ExpectedTableAssertionsTest/TableAssertionsTestPageXHtml.html
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/ExpectedTableAssertionsTest/TableAssertionsTestPageXHtml.html
2006-06-21 15:09:31 UTC (rev 490)
@@ -15,8 +15,7 @@
<tr>
<th rowspan="2" />
<th colspan="2">Average</th>
- <th rowspan="2">Red<br />
- eyes</th>
+ <th rowspan="2">Red<br/>eyes</th>
</tr>
<tr>
<th>height</th>
Modified:
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
===================================================================
---
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-commons-tests/src/main/resources/testcases/FormSubmissionTest/SingleNamedButtonForm.html
2006-06-21 15:09:31 UTC (rev 490)
@@ -3,6 +3,7 @@
<body>
<form method="GET" action="../params.jsp">
<input type="text" name="color" />
+ <input type="hidden" name="checkBox"/>
<input type="checkbox" name="checkBox" />
<input type="submit" name="button">
<input type="hidden" name="myReferer"
value="FormSubmissionTest/SingleNamedButtonForm.html">
Modified:
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
===================================================================
--- trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
2006-06-21 09:48:58 UTC (rev 489)
+++ trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/WebTester.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -239,7 +239,8 @@
*/
public void assertTextPresent(String text) {
if (!(getDialog().getPageText().indexOf(text) >= 0))
- Assert.fail("Expected text not found in response: [" + text + "]");
+ Assert.fail("Expected text not found in current page: [" + text
+ + "]\n Page content was: " + getDialog().getPageText());
}
/**
@@ -635,6 +636,32 @@
}
/**
+ * Assert that a form checkbox with a given name is present.
+ *
+ * @param checkboxName
+ * checkbox name.
+ */
+ public void assertCheckboxPresent(String checkboxName) {
+ assertFormPresent();
+ Assert.assertTrue("Did not find form checkbox with name ["
+ + checkboxName + "].", getDialog().hasElementByXPath(
+ "//[EMAIL PROTECTED]'checkbox' and @name='" + checkboxName +
"']"));
+ }
+
+ /**
+ * Assert that a form checkbox with a given name is not present.
+ *
+ * @param checkboxName
+ * checkbox name.
+ */
+ public void assertCheckboxNotPresent(String checkboxName) {
+ assertFormPresent();
+ Assert.assertFalse("Found form checkbox with name [" + checkboxName
+ + "] when not expected.", getDialog().hasElementByXPath(
+ "//[EMAIL PROTECTED]'checkbox' and @name='" + checkboxName +
"']"));
+ }
+
+ /**
* Assert that a form input element with a given label is present.
*
* @param formElementLabel
@@ -746,7 +773,7 @@
* @param checkBoxName
*/
public void assertCheckboxSelected(String checkBoxName) {
- assertFormElementPresent(checkBoxName);
+ assertCheckboxPresent(checkBoxName);
if (!getDialog().isCheckboxSelected(checkBoxName)) {
Assert.fail("Checkbox with name [" + checkBoxName
+ "] was not found selected.");
@@ -759,7 +786,7 @@
* @param checkBoxName
*/
public void assertCheckboxNotSelected(String checkBoxName) {
- assertFormElementPresent(checkBoxName);
+ assertCheckboxPresent(checkBoxName);
if (getDialog().isCheckboxSelected(checkBoxName)) {
Assert.fail("Checkbox with name [" + checkBoxName
+ "] was found selected.");
@@ -1167,7 +1194,7 @@
}
/**
- * Assert that a button with a given id is present.
+ * Assert that a button with a given id is present in the current window.
*
* @param buttonId
*/
@@ -1178,7 +1205,7 @@
}
/**
- * Assert that a button with a given text is present.
+ * Assert that a button with a given text is present in the current window.
*
* @param text
*/
@@ -1188,7 +1215,7 @@
}
/**
- * Assert that a button with a given text is not present.
+ * Assert that a button with a given text is not present in the current
window.
*
* @param text
*/
@@ -1198,13 +1225,13 @@
}
/**
- * Assert that a button with a given id is not present.
+ * Assert that a button with a given id is not present in the current
window.
*
* @param buttonId
*/
public void assertButtonNotPresent(String buttonId) {
assertFormPresent();
- Assert.assertFalse("Button [" + buttonId + "] found.", getDialog()
+ Assert.assertFalse("Button [" + buttonId + "] found when not
expected.", getDialog()
.hasButton(buttonId));
}
@@ -1503,7 +1530,7 @@
public void assertWindowCountEquals(int windowCount) {
Assert.assertTrue("Window count is " + getDialog().getWindowCount()
+ " but " + windowCount + " was expected.", getDialog()
- .getWindowCount()==windowCount);
+ .getWindowCount() == windowCount);
}
/**
@@ -1642,7 +1669,7 @@
* name of checkbox to be selected.
*/
public void checkCheckbox(String checkBoxName) {
- assertFormElementPresent(checkBoxName);
+ assertCheckboxPresent(checkBoxName);
getDialog().checkCheckbox(checkBoxName);
}
@@ -1656,7 +1683,7 @@
* value of checkbox to be selected.
*/
public void checkCheckbox(String checkBoxName, String value) {
- assertFormElementPresent(checkBoxName);
+ assertCheckboxPresent(checkBoxName);
getDialog().checkCheckbox(checkBoxName, value);
}
@@ -2146,18 +2173,18 @@
* Exemple: <br/>
*
* <pre>
- * <FORM action="http://my_host/doit"
method="post">
- * <P>
- * <SELECT multiple size="4"
name="component-select">
- * <OPTION selected
value="Component_1_a">Component_1</OPTION>
- * <OPTION selected
value="Component_1_b">Component_2</OPTION>
- * <OPTION>Component_3</OPTION>
- * <OPTION>Component_4</OPTION>
- * <OPTION>Component_5</OPTION>
- * </SELECT>
- * <INPUT type="submit"
value="Send"><INPUT type="reset">
- * </P>
- * </FORM>
+ * <FORM action="http://my_host/doit"
method="post">
+ * <P>
+ * <SELECT multiple size="4"
name="component-select">
+ * <OPTION selected
value="Component_1_a">Component_1</OPTION>
+ * <OPTION selected
value="Component_1_b">Component_2</OPTION>
+ * <OPTION>Component_3</OPTION>
+ * <OPTION>Component_4</OPTION>
+ * <OPTION>Component_5</OPTION>
+ * </SELECT>
+ * <INPUT type="submit"
value="Send"><INPUT type="reset">
+ * </P>
+ * </FORM>
* </pre>
*
* Should return [Component_1, Component_2, Component_3, Component_4,
Modified:
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
===================================================================
---
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -11,7 +11,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
@@ -21,7 +20,6 @@
import java.util.Arrays;
import java.util.List;
-
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
import org.jaxen.JaxenException;
@@ -240,9 +238,9 @@
}
public void gotoWindow(int windowID) {
- setMainWindow((WebWindow)wc.getWebWindows().get(windowID));
+ setMainWindow((WebWindow) wc.getWebWindows().get(windowID));
}
-
+
public int getWindowCount() {
return wc.getWebWindows().size();
}
@@ -260,15 +258,14 @@
}
public void closeWindow() {
- if (getWindowCount()==1) {
+ if (getWindowCount() == 1) {
closeBrowser();
- }
- else {
+ } else {
wc.deregisterWebWindow(win);
- win=wc.getCurrentWindow();
- form=null;
+ win = wc.getCurrentWindow();
+ form = null;
}
-
+
}
public boolean hasFrame(String frameName) {
@@ -332,8 +329,9 @@
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();
+ // TODO What should I return when it is a multi-select
+ return ((HtmlOption) getForm().getSelectByName(paramName)
+ .getSelectedOptions().get(0)).getValueAttribute();
} catch (ElementNotFoundException e) {
}
@@ -358,22 +356,22 @@
getForm().getInputByName(fieldName).setValueAttribute(paramValue);
}
-// /**
-// * Return a string array of select box option labels.
-// *
-// * @param selectName
-// * name of the select box.
-// */
-// public String[] getOptionsFor(String selectName) {
-// HtmlSelect sel = getForm().getSelectByName(selectName);
-// ArrayList result = new ArrayList();
-// List opts = sel.getOptions();
-// for (int i = 0; i < opts.size(); i++) {
-// HtmlOption opt = (HtmlOption) opts.get(i);
-// result.add(opt.asText());
-// }
-// return (String[]) result.toArray(new String[0]);
-// }
+ // /**
+ // * Return a string array of select box option labels.
+ // *
+ // * @param selectName
+ // * name of the select box.
+ // */
+ // public String[] getOptionsFor(String selectName) {
+ // HtmlSelect sel = getForm().getSelectByName(selectName);
+ // ArrayList result = new ArrayList();
+ // List opts = sel.getOptions();
+ // for (int i = 0; i < opts.size(); i++) {
+ // HtmlOption opt = (HtmlOption) opts.get(i);
+ // result.add(opt.asText());
+ // }
+ // return (String[]) result.toArray(new String[0]);
+ // }
/**
* Return a string array of select box option values.
@@ -443,8 +441,8 @@
}
private void initWebClient() {
- wc = new WebClient(new
BrowserVersion(BrowserVersion.INTERNET_EXPLORER, "4.0", testContext
- .getUserAgent(), "1.2", 6));
+ wc = new WebClient(new BrowserVersion(BrowserVersion.INTERNET_EXPLORER,
+ "4.0", testContext.getUserAgent(), "1.2", 6));
wc.setJavaScriptEnabled(jsEnabled);
wc.setThrowExceptionOnScriptError(true);
wc.addWebWindowListener(new WebWindowListener() {
@@ -469,8 +467,8 @@
String newPageTitle = "non_html";
if (newPage instanceof HtmlPage)
newPageTitle = ((HtmlPage) newPage).getTitleText();
- LOGGER.info("Window \"" + win + "\" changed : \"" +
oldPageTitle
- + "\" became \"" + newPageTitle +"\"");
+ LOGGER.info("Window \"" + win + "\" changed : \""
+ + oldPageTitle + "\" became \"" + newPageTitle + "\"");
}
public void webWindowOpened(WebWindowEvent event) {
@@ -504,10 +502,14 @@
}
private HtmlElement getElementByXPath(String xpath) {
+ return getElementByXPath(getCurrentPage(), xpath);
+ }
+
+ private HtmlElement getElementByXPath(Object parent, String xpath) {
List l = null;
try {
final HtmlUnitXPath xp = new HtmlUnitXPath(xpath);
- l = xp.selectNodes(getCurrentPage());
+ l = xp.selectNodes(parent);
} catch (JaxenException e) {
return null;
}
@@ -522,8 +524,8 @@
for (int i = 0; i < webWindows.size(); i++) {
WebWindow window = (WebWindow) webWindows.get(i);
if (window.getEnclosedPage() instanceof HtmlPage
- && ((HtmlPage) window.getEnclosedPage())
- .getTitleText().equals(title)) {
+ && ((HtmlPage) window.getEnclosedPage()).getTitleText()
+ .equals(title)) {
return window;
}
}
@@ -561,9 +563,17 @@
* response.
*/
private HtmlForm getForm() {
- if (form == null && hasForm())
- setWorkingForm(getForm(0));
- return form;
+ if (form == null) {
+ if (hasForm()) {
+ setWorkingForm(getForm(0));
+ return getForm(0);
+ }
+ else {
+ throw new RuntimeException("No form in current page");
+ }
+ } else {
+ return form;
+ }
}
private HtmlForm getForm(int formIndex) {
@@ -777,7 +787,8 @@
public boolean hasSubmitButton() {
List l = null;
try {
- final HtmlUnitXPath xp = new HtmlUnitXPath("//[EMAIL
PROTECTED]"submit\"]");
+ final HtmlUnitXPath xp = new HtmlUnitXPath(
+ "//[EMAIL PROTECTED]"submit\"]");
l = xp.selectNodes(getForm());
} catch (JaxenException e) {
throw new RuntimeException(e);
@@ -801,7 +812,8 @@
public boolean hasResetButton() {
List l = null;
try {
- final HtmlUnitXPath xp = new HtmlUnitXPath("//[EMAIL
PROTECTED]"reset\"]");
+ final HtmlUnitXPath xp = new HtmlUnitXPath(
+ "//[EMAIL PROTECTED]"reset\"]");
l = xp.selectNodes(getForm());
} catch (JaxenException e) {
throw new RuntimeException(e);
@@ -818,18 +830,17 @@
*
* @param buttonId
*/
- public ClickableElement getButton(String buttonId) {
+ private ClickableElement getButton(String buttonId) {
HtmlElement btn = null;
try {
btn = getCurrentPage().getHtmlElementById(buttonId);
+ if (btn instanceof HtmlButton || btn instanceof HtmlButtonInput
+ || btn instanceof HtmlSubmitInput
+ || btn instanceof HtmlResetInput)
+ return (ClickableElement) btn;
} catch (ElementNotFoundException e) {
- // Non trouv\xE9
return null;
}
- if (btn instanceof HtmlButton || btn instanceof HtmlButtonInput
- || btn instanceof HtmlSubmitInput
- || btn instanceof HtmlResetInput)
- return (ClickableElement) btn;
return null;
}
@@ -873,14 +884,12 @@
}
public boolean isCheckboxSelected(String checkBoxName) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) getForm().getInputByName(
- checkBoxName);
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName);
return cb.isChecked();
}
public boolean isCheckboxNotSelected(String checkBoxName) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) getForm().getInputByName(
- checkBoxName);
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName);
return !cb.isChecked();
}
@@ -915,13 +924,14 @@
public Table getTable(String tableSummaryOrId) {
HtmlTable table = getHtmlTable(tableSummaryOrId);
Table result = new Table();
- for (int i=0; i<table.getRowCount(); i++) {
+ for (int i = 0; i < table.getRowCount(); i++) {
Row newRow = new Row();
HtmlTableRow htmlRow = table.getRow(i);
CellIterator cellIt = htmlRow.getCellIterator();
- while(cellIt.hasNext()) {
+ while (cellIt.hasNext()) {
HtmlTableCell htmlCell = cellIt.nextCell();
- newRow.appendCell(new Cell(htmlCell.asText(),
htmlCell.getColumnSpan(), htmlCell.getRowSpan()));
+ newRow.appendCell(new Cell(htmlCell.asText(), htmlCell
+ .getColumnSpan(), htmlCell.getRowSpan()));
}
result.appendRow(newRow);
}
@@ -965,19 +975,20 @@
*/
public void submit() {
try {
- Object[] inpt =
getForm().getHtmlElementsByTagName("input").toArray();
- for (int i=0; i<inpt.length; i++) {
+ Object[] inpt = getForm().getHtmlElementsByTagName("input")
+ .toArray();
+ for (int i = 0; i < inpt.length; i++) {
if (inpt[i] instanceof HtmlSubmitInput) {
((HtmlSubmitInput) inpt[i]).click();
return;
}
- }
- for (int i=0; i<inpt.length; i++) {
+ }
+ for (int i = 0; i < inpt.length; i++) {
if (inpt[i] instanceof HtmlButtonInput) {
((HtmlButtonInput) inpt[i]).click();
return;
}
- }
+ }
} catch (IOException e) {
throw new RuntimeException(
"HtmlUnit Error submitting form using default submit
button, "
@@ -1136,6 +1147,28 @@
}
}
+ private HtmlCheckBoxInput getCheckbox(String checkBoxName) {
+ Object[] l = getForm().getInputsByName(checkBoxName).toArray();
+ for (int i = 0; i < l.length; i++) {
+ if (l[i] instanceof HtmlCheckBoxInput)
+ return (HtmlCheckBoxInput) l[i];
+ }
+ throw new RuntimeException("No checkbox with name [" + checkBoxName
+ + "] was found in current form.");
+ }
+
+ private HtmlCheckBoxInput getCheckbox(String checkBoxName, String value) {
+ Object[] l = getForm().getInputsByName(checkBoxName).toArray();
+ for (int i = 0; i < l.length; i++) {
+ if (l[i] instanceof HtmlCheckBoxInput)
+ if (((HtmlCheckBoxInput) l[i]).getValueAttribute()
+ .equals(value))
+ return (HtmlCheckBoxInput) l[i];
+ }
+ throw new RuntimeException("No checkbox with name [" + checkBoxName
+ + "] and value [" + value + "] was found in current form.");
+ }
+
/**
* Select a specified checkbox. If the checkbox is already checked then the
* checkbox will stay checked.
@@ -1144,8 +1177,7 @@
* name of checkbox to be deselected.
*/
public void checkCheckbox(String checkBoxName) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) getForm().getInputByName(
- checkBoxName);
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName);
if (!cb.isChecked())
try {
cb.click();
@@ -1155,6 +1187,17 @@
}
}
+ public void checkCheckbox(String checkBoxName, String value) {
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName, value);
+ if (!cb.isChecked())
+ try {
+ cb.click();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException("checkCheckbox failed :" + e);
+ }
+ }
+
/**
* Deselect a specified checkbox. If the checkbox is already unchecked then
* the checkbox will stay unchecked.
@@ -1163,8 +1206,7 @@
* name of checkbox to be deselected.
*/
public void uncheckCheckbox(String checkBoxName) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) getForm().getInputByName(
- checkBoxName);
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName);
if (cb.isChecked())
try {
cb.click();
@@ -1174,6 +1216,17 @@
}
}
+ public void uncheckCheckbox(String checkBoxName, String value) {
+ HtmlCheckBoxInput cb = getCheckbox(checkBoxName, value);
+ if (cb.isChecked())
+ try {
+ cb.click();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException("uncheckCheckbox failed :" + e);
+ }
+ }
+
/**
* Clicks a radio option. Asserts that the radio option exists first. *
*
@@ -1245,7 +1298,7 @@
throw new RuntimeException("Click failed");
}
}
-
+
public boolean hasElement(String anID) {
return getElement(anID) != null;
}
@@ -1253,7 +1306,7 @@
public boolean hasElementByXPath(String xpath) {
return getElementByXPath(xpath) != null;
}
-
+
public void clickElementByXPath(String xpath) {
HtmlElement e = getElementByXPath(xpath);
if (e == null)
@@ -1483,27 +1536,6 @@
return testContext;
}
- /*
- * (non-Javadoc)
- *
- * @see
net.sourceforge.jwebunit.IJWebUnitDialog#checkCheckbox(java.lang.String,
- * java.lang.String)
- */
- public void checkCheckbox(String checkBoxName, String value) {
- List l = getForm().getInputsByName(checkBoxName);
- for (int i = 0; i < l.size(); i++) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) l.get(i);
- if (cb.getValueAttribute().equals(value) && !cb.isChecked())
- try {
- cb.click();
- return;
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException("checkCheckbox failed :" + e);
- }
- }
- }
-
public void clickLinkWithTextAfterText(String linkText, String labelText) {
throw new UnsupportedOperationException("clickLinkWithTextAfterText");
}
@@ -1532,19 +1564,4 @@
public void setFormParameter(String paramName, String paramValue) {
setTextField(paramName, paramValue);
}
-
- public void uncheckCheckbox(String checkBoxName, String value) {
- List l = getForm().getInputsByName(checkBoxName);
- for (int i = 0; i < l.size(); i++) {
- HtmlCheckBoxInput cb = (HtmlCheckBoxInput) l.get(0);
- if (cb.getValueAttribute().equals(value) && cb.isChecked())
- try {
- cb.click();
- return;
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException("uncheckCheckbox failed :" + e);
- }
- }
- }
}
\ No newline at end of file
Modified:
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
===================================================================
---
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -45,6 +45,7 @@
suite.addTestSuite(NavigationTest.class);
suite.addTestSuite(XPathTest.class);
suite.addTestSuite(CharsetTest.class);
+ suite.addTestSuite(ButtonAssertionsTest.class);
//$JUnit-END$
return new JettySetup(suite);
}
Modified:
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
===================================================================
---
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-httpunit-plugin/src/main/java/net/sourceforge/jwebunit/httpunit/HttpUnitDialog.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -186,8 +186,8 @@
}
/**
- * Return the page title of the current page, encoded as specified
- * by the current [EMAIL PROTECTED] net.sourceforge.jwebunit.TestContext}.
+ * Return the page title of the current page, encoded as specified by the
+ * current [EMAIL PROTECTED] net.sourceforge.jwebunit.TestContext}.
*/
public String getPageTitle() {
try {
@@ -274,9 +274,16 @@
* the response.
*/
public WebForm getForm() {
- if (form == null && hasForm())
- setWorkingForm(getForm(0));
- return form;
+ if (form == null) {
+ if (hasForm()) {
+ setWorkingForm(getForm(0));
+ return getForm(0);
+ } else {
+ throw new RuntimeException("No form in current page");
+ }
+ } else {
+ return form;
+ }
}
private WebForm getForm(int formIndex) {
@@ -631,7 +638,8 @@
}
public boolean hasResetButton(String buttonName) {
- return hasElementByXPath("//[EMAIL PROTECTED]'reset' and
@name='"+buttonName+"']");
+ return hasElementByXPath("//[EMAIL PROTECTED]'reset' and @name='"
+ + buttonName + "']");
}
/**
@@ -639,8 +647,18 @@
*
* @param buttonId
*/
- public Button getButton(String buttonId) {
- return getForm().getButtonWithID(buttonId);
+ private Button getButton(String buttonId) {
+ Button btn = null;
+ try {
+ WebForm[] forms = resp.getForms();
+ for (int i = 0; i < forms.length; i++) {
+ if ((btn = forms[i].getButtonWithID(buttonId)) != null)
+ break;
+ }
+ } catch (SAXException e) {
+ throw new RuntimeException(e);
+ }
+ return btn;
}
/**
@@ -676,11 +694,7 @@
* @return <code>true</code> when the button was found.
*/
public boolean hasButton(String buttonId) {
- try {
- return getButton(buttonId) != null;
- } catch (UnableToSetFormException e) {
- return false;
- }
+ return getButton(buttonId) != null;
}
/**
@@ -1075,11 +1089,27 @@
* name of checkbox to be deselected.
*/
public void checkCheckbox(String checkBoxName) {
- setFormParameter(checkBoxName, "on");
+ checkFormStateWithParameter(checkBoxName);
+ WebResponse oldPage = getWebClient().getCurrentPage();
+ getForm().setCheckbox(checkBoxName, true);
+ // if an onchange event caused our page to change, set response to new
+ // page - otherwise leave
+ // the response alone.
+ if (oldPage != getWebClient().getCurrentPage()) {
+ resp = getWebClient().getCurrentPage();
+ }
}
public void checkCheckbox(String checkBoxName, String value) {
- updateFormParameter(checkBoxName, value);
+ checkFormStateWithParameter(checkBoxName);
+ WebResponse oldPage = getWebClient().getCurrentPage();
+ getForm().setCheckbox(checkBoxName, value, true);
+ // if an onchange event caused our page to change, set response to new
+ // page - otherwise leave
+ // the response alone.
+ if (oldPage != getWebClient().getCurrentPage()) {
+ resp = getWebClient().getCurrentPage();
+ }
}
/**
@@ -1090,11 +1120,27 @@
* name of checkbox to be deselected.
*/
public void uncheckCheckbox(String checkBoxName) {
- removeFormParameter(checkBoxName);
+ checkFormStateWithParameter(checkBoxName);
+ WebResponse oldPage = getWebClient().getCurrentPage();
+ getForm().setCheckbox(checkBoxName, false);
+ // if an onchange event caused our page to change, set response to new
+ // page - otherwise leave
+ // the response alone.
+ if (oldPage != getWebClient().getCurrentPage()) {
+ resp = getWebClient().getCurrentPage();
+ }
}
public void uncheckCheckbox(String checkBoxName, String value) {
- removeFormParameterWithValue(checkBoxName, value);
+ checkFormStateWithParameter(checkBoxName);
+ WebResponse oldPage = getWebClient().getCurrentPage();
+ getForm().setCheckbox(checkBoxName, value, false);
+ // if an onchange event caused our page to change, set response to new
+ // page - otherwise leave
+ // the response alone.
+ if (oldPage != getWebClient().getCurrentPage()) {
+ resp = getWebClient().getCurrentPage();
+ }
}
/**
@@ -1235,11 +1281,18 @@
* @param buttonId
*/
public void clickButton(String buttonId) {
- try {
- getButton(buttonId).click();
- resp = wc.getCurrentPage();
- } catch (Exception e) {
- throw new RuntimeException(ExceptionUtility.stackTraceToString(e));
+ Button btn = getButton(buttonId);
+ if (btn != null) {
+ try {
+ btn.click();
+ resp = wc.getCurrentPage();
+ } catch (Exception e) {
+ throw new RuntimeException(ExceptionUtility
+ .stackTraceToString(e));
+ }
+ } else {
+ throw new RuntimeException("Button with id [" + buttonId
+ + "] was not found");
}
}
@@ -1486,7 +1539,7 @@
public void gotoWindow(int windowID) {
setMainWindow(wc.getOpenWindows()[windowID]);
}
-
+
public int getWindowCount() {
return wc.getOpenWindows().length;
}
@@ -1573,11 +1626,12 @@
for (int i = 0; i < httpTable.getRowCount(); i++) {
Row newRow = new Row();
TableRow row = rows[i];
- TableCell[] cells = row.getCells();
+ TableCell[] cells = row.getCells();
for (int j = 0; j < cells.length; j++) {
TableCell httpCell = cells[j];
- if (httpCell!=null) newRow.appendCell(new
Cell(httpCell.getText(), httpCell
- .getColSpan(), httpCell.getRowSpan()));
+ if (httpCell != null)
+ newRow.appendCell(new Cell(httpCell.getText(), httpCell
+ .getColSpan(), httpCell.getRowSpan()));
}
result.appendRow(newRow);
}
@@ -1585,17 +1639,17 @@
}
public boolean hasElement(String anID) {
- return hasElementByXPath("//[EMAIL PROTECTED]'"+anID+"']");
+ return hasElementByXPath("//[EMAIL PROTECTED]'" + anID + "']");
}
public boolean hasElementByXPath(String xpath) {
- return getElementByXPath(xpath)!=null;
+ return getElementByXPath(xpath) != null;
}
-
+
private XPath makeXpath(String xpathString) throws JaxenException {
return new DOMXPath(xpathString);
}
-
+
private Object getElementByXPath(String xpath) {
try {
Document rootNode = getResponse().getDOM();
@@ -1611,11 +1665,11 @@
public void clickElementByXPath(String xpath) {
Object o = getElementByXPath(xpath);
if (o instanceof Node) {
- //TODO fix clickElementByXPath in HttpUnitDialog
+ // TODO fix clickElementByXPath in HttpUnitDialog
throw new UnsupportedOperationException("clickElementByXPath");
- }
- else {
- throw new RuntimeException("Don't know how to click on this
element");
+ } else {
+ throw new RuntimeException(
+ "Don't know how to click on this element");
}
}
@@ -1700,13 +1754,12 @@
public void closeWindow() {
wc.getMainWindow().close();
- if (wc.getOpenWindows().length>0) {
+ if (wc.getOpenWindows().length > 0) {
wc.setMainWindow(wc.getOpenWindows()[0]);
- resp=wc.getMainWindow().getCurrentPage();
- form=null;
- }
- else
- closeBrowser();
+ resp = wc.getMainWindow().getCurrentPage();
+ form = null;
+ } else
+ closeBrowser();
}
/*
Modified:
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
===================================================================
---
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-httpunit-plugin/src/test/java/net/sourceforge/jwebunit/httpunit/JWebUnitTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -46,6 +46,7 @@
suite.addTestSuite(NavigationTest.class);
suite.addTestSuite(XPathTest.class);
suite.addTestSuite(CharsetTest.class);
+ suite.addTestSuite(ButtonAssertionsTest.class);
//$JUnit-END$
return new JettySetup(suite);
}
Modified:
trunk/jwebunit-jacobie-plugin/src/test/java/net/sourceforge/jwebunit/jacobie/JWebUnitTest.java
===================================================================
---
trunk/jwebunit-jacobie-plugin/src/test/java/net/sourceforge/jwebunit/jacobie/JWebUnitTest.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-jacobie-plugin/src/test/java/net/sourceforge/jwebunit/jacobie/JWebUnitTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -43,6 +43,7 @@
suite.addTestSuite(FormAssertionsTest.class);
suite.addTestSuite(NavigationTest.class);
suite.addTestSuite(CharsetTest.class);
+ suite.addTestSuite(ButtonAssertionsTest.class);
//$JUnit-END$
return new JettySetup(suite);
}
Modified:
trunk/jwebunit-selenium-plugin/src/test/java/net/sourceforge/jwebunit/selenium/JWebUnitTest.java
===================================================================
---
trunk/jwebunit-selenium-plugin/src/test/java/net/sourceforge/jwebunit/selenium/JWebUnitTest.java
2006-06-21 09:48:58 UTC (rev 489)
+++
trunk/jwebunit-selenium-plugin/src/test/java/net/sourceforge/jwebunit/selenium/JWebUnitTest.java
2006-06-21 15:09:31 UTC (rev 490)
@@ -44,6 +44,7 @@
suite.addTestSuite(NavigationTest.class);
suite.addTestSuite(XPathTest.class);
suite.addTestSuite(CharsetTest.class);
+ suite.addTestSuite(ButtonAssertionsTest.class);
//$JUnit-END$
return new JettySetup(suite);
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development