[JWebUnit-development] SF.net SVN: jwebunit:[957] trunk
Brought to you by:
henryju
|
From: <he...@us...> - 2012-11-12 17:37:51
|
Revision: 957
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=957&view=rev
Author: henryju
Date: 2012-11-12 17:37:41 +0000 (Mon, 12 Nov 2012)
Log Message:
-----------
Update to HtmlUnit 2.11
Modified Paths:
--------------
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java
trunk/jwebunit-htmlunit-plugin/pom.xml
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
trunk/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImplTest.java
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java
trunk/pom.xml
trunk/src/changes/changes.xml
Modified: trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java
===================================================================
--- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java 2012-09-13 00:28:45 UTC (rev 956)
+++ trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java 2012-11-12 17:37:41 UTC (rev 957)
@@ -19,6 +19,10 @@
package net.sourceforge.jwebunit.tests;
+import org.junit.Test;
+
+import java.net.SocketTimeoutException;
+
import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderEquals;
import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderMatches;
import static net.sourceforge.jwebunit.junit.JWebUnit.assertHeaderNotPresent;
@@ -36,98 +40,99 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import java.net.SocketTimeoutException;
-
-import org.junit.Test;
-
/**
* Test redirection support.
- *
+ *
* @author Julien Henry
*/
public class ResponseServletTest extends JWebUnitAPITestCase {
- public void setUp() throws Exception {
- super.setUp();
- setIgnoreFailingStatusCodes(true); // ignore failing status codes
- setBaseUrl(HOST_PATH + "/ResponseServletTest");
- }
+ public void setUp() throws Exception {
+ super.setUp();
+ setIgnoreFailingStatusCodes(true); // ignore failing status codes
+ setBaseUrl(HOST_PATH + "/ResponseServletTest");
+ }
- /*
- * currently we can't get the response code from HtmlUnit unless it is a failing code
- */
- @Test public void testDefault() {
- beginAt("/SimpleForm.html");
- submit();
- assertResponseCodeBetween(200, 299);
-
- // test the headers
- assertHeaderPresent("Test");
- assertHeaderNotPresent("Not-present");
- assertHeaderEquals("Test", "test2");
- assertHeaderMatches("Header-Added", "[0-9]{2}");
- }
+ /*
+ * currently we can't get the response code from HtmlUnit unless it is a failing code
+ */
+ @Test
+ public void testDefault() {
+ beginAt("/SimpleForm.html");
+ submit();
+ assertResponseCodeBetween(200, 299);
- @Test public void testResponse200() {
- beginAt("/SimpleForm.html");
- setTextField("status", "200");
- submit();
- assertResponseCode(200);
- }
+ // test the headers
+ assertHeaderPresent("Test");
+ assertHeaderNotPresent("Not-present");
+ assertHeaderEquals("Test", "test2");
+ assertHeaderMatches("Header-Added", "[0-9]{2}");
+ }
- /*
- * HtmlUnit cannot handle a 301 without a valid Location: header
+ @Test
+ public void testResponse200() {
+ beginAt("/SimpleForm.html");
+ setTextField("status", "200");
+ submit();
+ assertResponseCode(200);
+ }
+
+ /*
+ * HtmlUnit cannot handle a 301 without a valid Location: header
@Test public void testResponse301() {
beginAt("/SimpleForm.html");
setTextField("status", "301");
submit();
assertResponseCode(301);
}
- */
+ */
- @Test public void testResponse404() {
- beginAt("/SimpleForm.html");
- assertTitleEquals("response form");
- setTextField("status", "404");
- submit();
- assertResponseCode(404);
- }
+ @Test
+ public void testResponse404() {
+ beginAt("/SimpleForm.html");
+ assertTitleEquals("response form");
+ setTextField("status", "404");
+ submit();
+ assertResponseCode(404);
+ }
- @Test public void testResponse501() {
- beginAt("/SimpleForm.html");
- assertTitleEquals("response form");
- setTextField("status", "501");
- submit();
- assertResponseCode(501);
+ @Test
+ public void testResponse501() {
+ beginAt("/SimpleForm.html");
+ assertTitleEquals("response form");
+ setTextField("status", "501");
+ submit();
+ assertResponseCode(501);
+ }
+
+ /**
+ * Issue 1674646: add support for specifying the timeout of pages
+ */
+ @Test
+ public void testTimeout() {
+
+ // test that timeout was fired
+ setTimeout(500); // specify a global timeout of 0.5 seconds (must be set before the WebConnection is initialised)
+ beginAt("/SimpleForm.html");
+ assertTitleEquals("response form");
+ setTextField("timeout", "1"); // server wait for 1 seconds
+ try {
+ submit();
+ fail("timeout was not called"); // we should not get here
+ } catch (RuntimeException e) {
+ assertTrue("timeout caused by SocketTimeoutException, but was " + e.getCause().getClass(), e.getCause() instanceof SocketTimeoutException);
}
-
- /**
- * Issue 1674646: add support for specifying the timeout of pages
- */
- @Test public void testTimeout() {
-
- // test that timeout was fired
- setTimeout(500); // specify a global timeout of 0.5 seconds (must be set before the WebConnection is initialised)
- beginAt("/SimpleForm.html");
- assertTitleEquals("response form");
- setTextField("timeout", "1"); // server wait for 1 seconds
- try {
- submit();
- fail("timeout was not called"); // we should not get here
- } catch (RuntimeException e) {
- assertTrue("timeout caused by SocketTimeoutException, but was " + e.getCause().getClass(), e.getCause() instanceof SocketTimeoutException);
- }
-
- // close and reset the browser
- closeBrowser();
-
- // test that timeout wasn't fired
- setTimeout(2000); // specify a global timeout of 2 seconds (must be set before the WebConnection is initialised)
- beginAt("/SimpleForm.html");
- assertTitleEquals("response form");
- setTextField("timeout", "1"); // server wait for 1 seconds
- submit();
- assertTextPresent("hello, world!");
- }
+ // close and reset the browser
+ closeBrowser();
+
+ // test that timeout wasn't fired
+ setTimeout(2000); // specify a global timeout of 2 seconds (must be set before the WebConnection is initialised)
+ beginAt("/SimpleForm.html");
+ assertTitleEquals("response form");
+ setTextField("timeout", "1"); // server wait for 1 seconds
+ submit();
+ assertTextPresent("hello, world!");
+ }
+
}
Modified: trunk/jwebunit-htmlunit-plugin/pom.xml
===================================================================
--- trunk/jwebunit-htmlunit-plugin/pom.xml 2012-09-13 00:28:45 UTC (rev 956)
+++ trunk/jwebunit-htmlunit-plugin/pom.xml 2012-11-12 17:37:41 UTC (rev 957)
@@ -23,7 +23,7 @@
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
- <version>2.10</version>
+ <version>2.11</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
Modified: trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
===================================================================
--- trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java 2012-09-13 00:28:45 UTC (rev 956)
+++ trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java 2012-11-12 17:37:41 UTC (rev 957)
@@ -17,194 +17,204 @@
* along with JWebUnit. If not, see <http://www.gnu.org/licenses/>.
*/
/**
- *
+ *
*/
package net.sourceforge.jwebunit.htmlunit;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.sourceforge.jwebunit.api.IElement;
-
+import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
+import net.sourceforge.jwebunit.api.IElement;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* HtmlUnit implementation of IElement wrapper.
- *
+ *
* @author jmwright
*
*/
public class HtmlUnitElementImpl implements IElement {
-
- /**
- * The wrapped element.
- */
- private HtmlElement element;
-
- public HtmlUnitElementImpl(HtmlElement element) {
- if (element == null)
- throw new NullPointerException("Cannot create an IElement for a null element.");
- this.element = element;
- }
+ /**
+ * The wrapped element.
+ */
+ private DomElement element;
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
- */
- public String getAttribute(String name) {
- if ("value".equals(name) && element instanceof HtmlOption) {
- // for options, we want text if no value was specified
- return ((HtmlOption) element).getValueAttribute();
- } else {
- if (!element.hasAttribute(name))
- return null;
-
- return element.getAttribute(name);
- }
- }
+ public HtmlUnitElementImpl(DomElement element) {
+ if (element == null)
+ throw new NullPointerException("Cannot create an IElement for a null element.");
+ this.element = element;
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
+ */
+ public String getAttribute(String name) {
+ if ("value".equals(name) && element instanceof HtmlOption) {
+ // for options, we want text if no value was specified
+ return ((HtmlOption) element).getValueAttribute();
+ } else {
+ if (!element.hasAttribute(name))
+ return null;
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#name()
- */
- public String getName() {
- return element.getNodeName();
- }
+ return element.getAttribute(name);
+ }
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#name()
+ */
+ public String getName() {
+ return element.getNodeName();
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#getChildren()
- */
- public List<IElement> getChildren() {
- List<IElement> children = new ArrayList<IElement>();
- for (HtmlElement e : element.getChildElements()) {
- if (e != null)
- children.add(new HtmlUnitElementImpl(e));
- }
- return children;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getChildren()
+ */
+ public List<IElement> getChildren() {
+ List<IElement> children = new ArrayList<IElement>();
+ for (DomElement e : element.getChildElements()) {
+ if (e != null)
+ children.add(new HtmlUnitElementImpl(e));
+ }
+ return children;
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getParent()
+ */
+ public IElement getParent() {
+ DomNode p = element.getParentNode();
+ while (true) {
+ if (p == null)
+ return null;
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#getParent()
- */
- public IElement getParent() {
- DomNode p = element.getParentNode();
- while (true) {
- if (p == null)
- return null;
-
- if (p instanceof HtmlElement)
- return new HtmlUnitElementImpl((HtmlElement) p);
-
- // get next parent
- p = p.getParentNode();
- }
- }
+ if (p instanceof HtmlElement)
+ return new HtmlUnitElementImpl((HtmlElement) p);
+ // get next parent
+ p = p.getParentNode();
+ }
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
- */
- public String getTextContent() {
- return element.getTextContent();
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
+ */
+ public String getTextContent() {
+ return element.getTextContent();
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
+ */
+ public IElement getElement(String xpath) {
+ // if this fails with a ClassCastException, use getElements().get(0) (performance penalty)
+ return new HtmlUnitElementImpl((HtmlElement) element.getFirstByXPath(xpath));
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
- */
- public IElement getElement(String xpath) {
- // if this fails with a ClassCastException, use getElements().get(0) (performance penalty)
- return new HtmlUnitElementImpl((HtmlElement) element.getFirstByXPath(xpath));
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
+ */
+ public List<IElement> getElements(String xpath) {
+ List<IElement> elements = new ArrayList<IElement>();
+ for (Object o : element.getByXPath(xpath)) {
+ if (o instanceof HtmlElement)
+ elements.add(new HtmlUnitElementImpl((HtmlElement) o));
+ }
+ return elements;
+ }
+ public String toString() {
+ return "IElement[name=" + getName() + " wrapped=" + element + "]";
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
- */
- public List<IElement> getElements(String xpath) {
- List<IElement> elements = new ArrayList<IElement>();
- for (Object o : element.getByXPath(xpath)) {
- if (o instanceof HtmlElement)
- elements.add(new HtmlUnitElementImpl((HtmlElement) o));
- }
- return elements;
- }
-
- public String toString() {
- return "IElement[name=" + getName() + " wrapped=" + element + "]";
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
+ */
+ public void setAttribute(String string) {
+ element.setAttributeNS(null, string, "1");
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String, java.lang.String)
+ */
+ public void setAttribute(String name, String value) {
+ if ("value".equals(name) && element instanceof HtmlInput) {
+ // for HtmlInputs, we want to run any onChange code if the value changes
+ ((HtmlInput) element).setValueAttribute(value);
+ } else {
+ element.setAttribute(name, value);
+ }
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
- */
- public void setAttribute(String string) {
- element.setAttributeNS(null, string, "1");
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
+ */
+ public void setTextContent(String value) {
+ if (element instanceof HtmlTextArea) {
+ ((HtmlTextArea) element).setText(value);
+ } else {
+ element.setTextContent(value);
+ }
+ }
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((element == null) ? 0 : element.hashCode());
+ return result;
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String, java.lang.String)
- */
- public void setAttribute(String name, String value) {
- if ("value".equals(name) && element instanceof HtmlInput) {
- // for HtmlInputs, we want to run any onChange code if the value changes
- ((HtmlInput) element).setValueAttribute(value);
- } else {
- element.setAttribute(name, value);
- }
- }
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final HtmlUnitElementImpl other = (HtmlUnitElementImpl) obj;
+ if (element == null) {
+ if (other.element != null)
+ return false;
+ } else if (!element.equals(other.element))
+ return false;
+ return true;
+ }
+ /**
+ * Return the unwrapped HtmlUnit element that this IElement represents.
+ *
+ * @return the HtmlUnit element this IElement represents.
+ */
+ public DomElement getHtmlElement() {
+ return element;
+ }
- /* (non-Javadoc)
- * @see net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
- */
- public void setTextContent(String value) {
- if (element instanceof HtmlTextArea) {
- ((HtmlTextArea) element).setText(value);
- } else {
- element.setTextContent(value);
- }
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((element == null) ? 0 : element.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- final HtmlUnitElementImpl other = (HtmlUnitElementImpl) obj;
- if (element == null) {
- if (other.element != null)
- return false;
- } else if (!element.equals(other.element))
- return false;
- return true;
- }
-
- /**
- * Return the unwrapped HtmlUnit element that this IElement represents.
- *
- * @return the HtmlUnit element this IElement represents.
- */
- public HtmlElement getHtmlElement() {
- return element;
- }
-
}
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 2012-09-13 00:28:45 UTC (rev 956)
+++ trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java 2012-11-12 17:37:41 UTC (rev 957)
@@ -18,51 +18,6 @@
*/
package net.sourceforge.jwebunit.htmlunit;
-import com.gargoylesoftware.htmlunit.TopLevelWindow;
-
-import net.sourceforge.jwebunit.api.HttpHeader;
-
-import org.apache.http.auth.AuthScope;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.InetAddress;
-import java.net.URL;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import net.sourceforge.jwebunit.api.IElement;
-import net.sourceforge.jwebunit.api.ITestingEngine;
-import net.sourceforge.jwebunit.exception.ExpectedJavascriptAlertException;
-import net.sourceforge.jwebunit.exception.ExpectedJavascriptConfirmException;
-import net.sourceforge.jwebunit.exception.ExpectedJavascriptPromptException;
-import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
-import net.sourceforge.jwebunit.exception.UnableToSetFormException;
-import net.sourceforge.jwebunit.exception.UnexpectedJavascriptAlertException;
-import net.sourceforge.jwebunit.exception.UnexpectedJavascriptConfirmException;
-import net.sourceforge.jwebunit.exception.UnexpectedJavascriptPromptException;
-import net.sourceforge.jwebunit.html.Cell;
-import net.sourceforge.jwebunit.html.Row;
-import net.sourceforge.jwebunit.html.Table;
-import net.sourceforge.jwebunit.javascript.JavascriptAlert;
-import net.sourceforge.jwebunit.javascript.JavascriptConfirm;
-import net.sourceforge.jwebunit.javascript.JavascriptPrompt;
-import net.sourceforge.jwebunit.util.TestContext;
-
-import org.apache.regexp.RE;
-import org.apache.regexp.RESyntaxException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ConfirmHandler;
@@ -75,6 +30,7 @@
import com.gargoylesoftware.htmlunit.PromptHandler;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.TextPage;
+import com.gargoylesoftware.htmlunit.TopLevelWindow;
import com.gargoylesoftware.htmlunit.UnexpectedPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
@@ -103,2313 +59,2344 @@
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
-import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow.CellIterator;
+import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.util.Cookie;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.gargoylesoftware.htmlunit.xml.XmlPage;
+import net.sourceforge.jwebunit.api.HttpHeader;
+import net.sourceforge.jwebunit.api.IElement;
+import net.sourceforge.jwebunit.api.ITestingEngine;
+import net.sourceforge.jwebunit.exception.ExpectedJavascriptAlertException;
+import net.sourceforge.jwebunit.exception.ExpectedJavascriptConfirmException;
+import net.sourceforge.jwebunit.exception.ExpectedJavascriptPromptException;
+import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
+import net.sourceforge.jwebunit.exception.UnableToSetFormException;
+import net.sourceforge.jwebunit.exception.UnexpectedJavascriptAlertException;
+import net.sourceforge.jwebunit.exception.UnexpectedJavascriptConfirmException;
+import net.sourceforge.jwebunit.exception.UnexpectedJavascriptPromptException;
+import net.sourceforge.jwebunit.html.Cell;
+import net.sourceforge.jwebunit.html.Row;
+import net.sourceforge.jwebunit.html.Table;
+import net.sourceforge.jwebunit.javascript.JavascriptAlert;
+import net.sourceforge.jwebunit.javascript.JavascriptConfirm;
+import net.sourceforge.jwebunit.javascript.JavascriptPrompt;
+import net.sourceforge.jwebunit.util.TestContext;
+import org.apache.http.auth.AuthScope;
+import org.apache.regexp.RE;
+import org.apache.regexp.RESyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetAddress;
+import java.net.URL;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
/**
* Acts as the wrapper for HtmlUnit access. A testing engine is initialized with a given URL, and maintains conversational state
* as the dialog progresses through link navigation, form submission, etc.
- *
+ *
* @author Julien Henry
- *
+ *
*/
public class HtmlUnitTestingEngineImpl implements ITestingEngine {
- /**
- * Logger for this class.
- */
- private final Logger logger = LoggerFactory.getLogger(HtmlUnitTestingEngineImpl.class);
+ /**
+ * Logger for this class.
+ */
+ private final Logger logger = LoggerFactory.getLogger(HtmlUnitTestingEngineImpl.class);
- /**
- * holder for alternative refresh handler.
- */
- private RefreshHandler refreshHandler;
- /**
- * Encapsulate browser abilities.
- */
- private WebClient wc;
+ /**
+ * holder for alternative refresh handler.
+ */
+ private RefreshHandler refreshHandler;
+ /**
+ * Encapsulate browser abilities.
+ */
+ private WebClient wc;
- /**
- * The currently selected window.
- */
- private WebWindow win;
+ /**
+ * The currently selected window.
+ */
+ private WebWindow win;
- /**
- * A ref to the test context.
- */
- private TestContext testContext;
+ /**
+ * A ref to the test context.
+ */
+ private TestContext testContext;
- /**
- * The currently selected form.
- */
- private HtmlForm form;
+ /**
+ * The currently selected form.
+ */
+ private HtmlForm form;
- /**
- * Is Javascript enabled.
- */
- private boolean jsEnabled = true;
+ /**
+ * Is Javascript enabled.
+ */
+ private boolean jsEnabled = true;
- /**
- * Should throw exception on Javascript error.
- */
- private boolean throwExceptionOnScriptError = true;
+ /**
+ * Should throw exception on Javascript error.
+ */
+ private boolean throwExceptionOnScriptError = true;
- /**
- * Javascript alerts.
- */
- private List<JavascriptAlert> expectedJavascriptAlerts = new LinkedList<JavascriptAlert>();
+ /**
+ * Javascript alerts.
+ */
+ private List<JavascriptAlert> expectedJavascriptAlerts = new LinkedList<JavascriptAlert>();
- /**
- * Javascript confirms.
- */
- private List<JavascriptConfirm> expectedJavascriptConfirms = new LinkedList<JavascriptConfirm>();
+ /**
+ * Javascript confirms.
+ */
+ private List<JavascriptConfirm> expectedJavascriptConfirms = new LinkedList<JavascriptConfirm>();
- /**
- * Javascript prompts.
- */
- private List<JavascriptPrompt> expectedJavascriptPrompts = new LinkedList<JavascriptPrompt>();
-
- /**
- * The default browser version.
- */
- private BrowserVersion defaultBrowserVersion = BrowserVersion.FIREFOX_3_6;
-
- /**
+ /**
+ * Javascript prompts.
+ */
+ private List<JavascriptPrompt> expectedJavascriptPrompts = new LinkedList<JavascriptPrompt>();
+
+ /**
+ * The default browser version.
+ */
+ private BrowserVersion defaultBrowserVersion = BrowserVersion.FIREFOX_3_6;
+
+ /**
* Should we ignore failing status codes?
*/
- private boolean ignoreFailingStatusCodes = false;
-
- /**
- * Do we provide a timeout limit (in seconds)? Default 0 = unlimited timeout.
- */
- private int timeout = 0;
-
- // Implementation of IJWebUnitDialog
+ private boolean ignoreFailingStatusCodes = false;
- /**
- * Initializes default HtmlUnit testing engine implementation.
- */
- public HtmlUnitTestingEngineImpl() {
- }
+ /**
+ * Do we provide a timeout limit (in seconds)? Default 0 = unlimited timeout.
+ */
+ private int timeout = 0;
- /**
- * Initializes HtmlUnit testing engine implementation with web client.
- *
- * @param client web client
- */
- HtmlUnitTestingEngineImpl(WebClient client) {
- this.wc = client;
- }
+ // Implementation of IJWebUnitDialog
- /**
- * Begin a dialog with an initial URL and test client context.
- *
- * @param initialURL absolute url at which to begin dialog.
- * @param context contains context information for the test client.
- * @throws TestingEngineResponseException
- */
- public void beginAt(URL initialURL, TestContext context)
- throws TestingEngineResponseException {
- this.setTestContext(context);
- initWebClient();
- gotoPage(initialURL);
- }
+ /**
+ * Initializes default HtmlUnit testing engine implementation.
+ */
+ public HtmlUnitTestingEngineImpl() {
+ }
- /**
- * Close the browser and check that all expected Javascript alerts, confirms and
- * prompts have been taken care of.
- */
- public void closeBrowser() throws ExpectedJavascriptAlertException,
- ExpectedJavascriptConfirmException,
- ExpectedJavascriptPromptException {
- if (wc!=null) {
- wc.closeAllWindows();
- wc = null;
- }
- form = null; // reset current form
- if (this.expectedJavascriptAlerts.size() > 0) {
- throw new ExpectedJavascriptAlertException(
- ((JavascriptAlert) (expectedJavascriptAlerts.get(0)))
- .getMessage());
- }
- if (this.expectedJavascriptConfirms.size() > 0) {
- throw new ExpectedJavascriptConfirmException(
- ((JavascriptConfirm) (expectedJavascriptConfirms.get(0)))
- .getMessage());
- }
- if (this.expectedJavascriptPrompts.size() > 0) {
- throw new ExpectedJavascriptPromptException(
- ((JavascriptPrompt) (expectedJavascriptPrompts.get(0)))
- .getMessage());
- }
+ /**
+ * Initializes HtmlUnit testing engine implementation with web client.
+ *
+ * @param client web client
+ */
+ HtmlUnitTestingEngineImpl(WebClient client) {
+ this.wc = client;
+ }
- }
+ /**
+ * Begin a dialog with an initial URL and test client context.
+ *
+ * @param initialURL absolute url at which to begin dialog.
+ * @param context contains context information for the test client.
+ * @throws TestingEngineResponseException
+ */
+ public void beginAt(URL initialURL, TestContext context)
+ throws TestingEngineResponseException {
+ this.setTestContext(context);
+ initWebClient();
+ gotoPage(initialURL);
+ }
- /**
- * Go to a particular page.
- *
- * @throws TestingEngineResponseException if an error response code is encountered
- * and ignoreFailingStatusCodes is not enabled.
- */
- public void gotoPage(URL initialURL) throws TestingEngineResponseException {
- try {
- wc.getPage(initialURL);
- win = wc.getCurrentWindow();
- form = null;
- } catch (FailingHttpStatusCodeException ex) {
- // only throw exception if necessary
- if (!ignoreFailingStatusCodes) {
- throw new TestingEngineResponseException(ex.getStatusCode(),
- "unexpected status code ["+ex.getStatusCode()+"] at URL: ["+initialURL+"]", ex);
- }
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
+ /**
+ * Close the browser and check that all expected Javascript alerts, confirms and
+ * prompts have been taken care of.
+ */
+ public void closeBrowser() throws ExpectedJavascriptAlertException,
+ ExpectedJavascriptConfirmException,
+ ExpectedJavascriptPromptException {
+ if (wc != null) {
+ wc.closeAllWindows();
+ wc = null;
}
-
- /**
- * @see net.sourceforge.jwebunit.api.IJWebUnitDialog#setScriptingEnabled(boolean)
- */
- public void setScriptingEnabled(boolean value) {
- // This variable is used to set Javascript before wc is instancied
- jsEnabled = value;
- if (wc != null) {
- wc.setJavaScriptEnabled(value);
- }
+ form = null; // reset current form
+ if (this.expectedJavascriptAlerts.size() > 0) {
+ throw new ExpectedJavascriptAlertException(
+ ((JavascriptAlert) (expectedJavascriptAlerts.get(0)))
+ .getMessage());
}
-
- public void setThrowExceptionOnScriptError(boolean value) {
- throwExceptionOnScriptError = value;
- if (wc != null) {
- wc.setThrowExceptionOnScriptError(throwExceptionOnScriptError);
- }
+ if (this.expectedJavascriptConfirms.size() > 0) {
+ throw new ExpectedJavascriptConfirmException(
+ ((JavascriptConfirm) (expectedJavascriptConfirms.get(0)))
+ .getMessage());
}
-
- public List<javax.servlet.http.Cookie> getCookies() {
- List<javax.servlet.http.Cookie> result = new LinkedList<javax.servlet.http.Cookie>();
- Set<Cookie> cookies = wc.getCookieManager().getCookies();
- for (Cookie cookie : cookies) {
- javax.servlet.http.Cookie c = new javax.servlet.http.Cookie(
- cookie.getName(), cookie.getValue());
- c.setComment(cookie.toHttpClient().getComment());
- c.setDomain(cookie.getDomain());
- Date expire = cookie.toHttpClient().getExpiryDate();
- if (expire == null) {
- c.setMaxAge(-1);
- } else {
- Date now = Calendar.getInstance().getTime();
- // Convert milli-second to second
- Long second = Long.valueOf((expire.getTime() - now.getTime()) / 1000);
- c.setMaxAge(second.intValue());
- }
- c.setPath(cookie.getPath());
- c.setSecure(cookie.toHttpClient().isSecure());
- c.setVersion(cookie.toHttpClient().getVersion());
- result.add(c);
- }
- return result;
+ if (this.expectedJavascriptPrompts.size() > 0) {
+ throw new ExpectedJavascriptPromptException(
+ ((JavascriptPrompt) (expectedJavascriptPrompts.get(0)))
+ .getMessage());
}
- public boolean hasWindow(String windowName) {
- try {
- getWindow(windowName);
- } catch (WebWindowNotFoundException e) {
- return false;
- }
- return true;
- }
+ }
- public boolean hasWindowByTitle(String title) {
- return getWindowByTitle(title) != null;
+ /**
+ * Go to a particular page.
+ *
+ * @throws TestingEngineResponseException if an error response code is encountered
+ * and ignoreFailingStatusCodes is not enabled.
+ */
+ public void gotoPage(URL initialURL) throws TestingEngineResponseException {
+ try {
+ wc.getPage(initialURL);
+ win = wc.getCurrentWindow();
+ form = null;
+ } catch (FailingHttpStatusCodeException ex) {
+ throw new TestingEngineResponseException(ex.getStatusCode(),
+ "unexpected status code [" + ex.getStatusCode() + "] at URL: [" + initialURL + "]", ex);
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
}
+ }
- /**
- * Make the window with the given name in the current conversation active.
- *
- * @param windowName
- */
- public void gotoWindow(String windowName) {
- setMainWindow(getWindow(windowName));
+ /**
+ * @see net.sourceforge.jwebunit.api.IJWebUnitDialog#setScriptingEnabled(boolean)
+ */
+ public void setScriptingEnabled(boolean value) {
+ // This variable is used to set Javascript before wc is instancied
+ jsEnabled = value;
+ if (wc != null) {
+ wc.getOptions().setJavaScriptEnabled(value);
}
+ }
- public void gotoWindow(int windowID) {
- setMainWindow((WebWindow) wc.getWebWindows().get(windowID));
+ public void setThrowExceptionOnScriptError(boolean value) {
+ throwExceptionOnScriptError = value;
+ if (wc != null) {
+ wc.getOptions().setThrowExceptionOnScriptError(value);
}
+ }
- public int getWindowCount() {
- return wc.getWebWindows().size();
+ public List<javax.servlet.http.Cookie> getCookies() {
+ List<javax.servlet.http.Cookie> result = new LinkedList<javax.servlet.http.Cookie>();
+ Set<Cookie> cookies = wc.getCookieManager().getCookies();
+ for (Cookie cookie : cookies) {
+ javax.servlet.http.Cookie c = new javax.servlet.http.Cookie(
+ cookie.getName(), cookie.getValue());
+ c.setComment(cookie.toHttpClient().getComment());
+ c.setDomain(cookie.getDomain());
+ Date expire = cookie.toHttpClient().getExpiryDate();
+ if (expire == null) {
+ c.setMaxAge(-1);
+ } else {
+ Date now = Calendar.getInstance().getTime();
+ // Convert milli-second to second
+ Long second = Long.valueOf((expire.getTime() - now.getTime()) / 1000);
+ c.setMaxAge(second.intValue());
+ }
+ c.setPath(cookie.getPath());
+ c.setSecure(cookie.toHttpClient().isSecure());
+ c.setVersion(cookie.toHttpClient().getVersion());
+ result.add(c);
}
+ return result;
+ }
- /**
- * Goto first window with the given title.
- *
- * @param title
- */
- public void gotoWindowByTitle(String title) {
- WebWindow window = getWindowByTitle(title);
- if (window != null) {
- setMainWindow(window);
- }
- else {
- throw new RuntimeException("No window found with title [" + title + "]");
- }
+ public boolean hasWindow(String windowName) {
+ try {
+ getWindow(windowName);
+ } catch (WebWindowNotFoundException e) {
+ return false;
}
+ return true;
+ }
- /**
- * Close the current window.
- */
- public void closeWindow() {
- if (win != null) {
- ((TopLevelWindow) win.getTopWindow()).close();
- win = wc.getCurrentWindow();
- form = null;
- }
+ public boolean hasWindowByTitle(String title) {
+ return getWindowByTitle(title) != null;
+ }
- }
+ /**
+ * Make the window with the given name in the current conversation active.
+ *
+ * @param windowName
+ */
+ public void gotoWindow(String windowName) {
+ setMainWindow(getWindow(windowName));
+ }
- /**
- * {@inheritDoc}
- */
- public boolean hasFrame(String frameNameOrId) {
- return getFrame(frameNameOrId) != null;
- }
+ public void gotoWindow(int windowID) {
+ setMainWindow((WebWindow) wc.getWebWindows().get(windowID));
+ }
- /**
- * {@inheritDoc}
- */
- public void gotoFrame(String frameNameOrId) {
- WebWindow frame = getFrame(frameNameOrId);
- if (frame == null) {
- throw new RuntimeException("No frame found in current page with name or id [" + frameNameOrId + "]");
- }
- win = frame;
- }
+ public int getWindowCount() {
+ return wc.getWebWindows().size();
+ }
- /**
- * {@inheritDoc}
- */
- public void setWorkingForm(int index) {
- setWorkingForm(getForm(index));
+ /**
+ * Goto first window with the given title.
+ *
+ * @param title
+ */
+ public void gotoWindowByTitle(String title) {
+ WebWindow window = getWindowByTitle(title);
+ if (window != null) {
+ setMainWindow(window);
}
-
- /**
- * {@inheritDoc}
- */
- public void setWorkingForm(String nameOrId, int index) {
- setWorkingForm(getForm(nameOrId, index));
+ else {
+ throw new RuntimeException("No window found with title [" + title + "]");
}
+ }
- /**
- * Return true if the current response contains a form.
- */
- public boolean hasForm() {
- return ((HtmlPage) win.getEnclosedPage()).getForms().size() > 0;
+ /**
+ * Close the current window.
+ */
+ public void closeWindow() {
+ if (win != null) {
+ ((TopLevelWindow) win.getTopWindow()).close();
+ win = wc.getCurrentWindow();
+ form = null;
}
- /**
- * Return true if the current response contains a specific form.
- *
- * @param nameOrID name of id of the form to check for.
- */
- public boolean hasForm(String nameOrID) {
- return getForm(nameOrID) != null;
- }
+ }
- public boolean hasFormParameterNamed(String paramName) {
- for (HtmlElement e : getCurrentPage().getHtmlElementDescendants()) {
- if (e.getAttribute("name").equals(paramName)) {
- // set the working form if none has been set
- if (e.getEnclosingForm() != null && getWorkingForm() == null)
- setWorkingForm( e.getEnclosingForm() );
- return true;
- }
- }
- return false;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public boolean hasFrame(String frameNameOrId) {
+ return getFrame(frameNameOrId) != null;
+ }
- /**
- * 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 getTextFieldValue(String paramName) {
- // first try the current form
- if (form != null) {
- for (HtmlElement e : form.getHtmlElementDescendants()) {
- if (e instanceof HtmlInput && e.getAttribute("name").equals(paramName)) {
- // we found it
- return ((HtmlInput) e).getValueAttribute();
- }
- if (e instanceof HtmlTextArea && e.getAttribute("name").equals(paramName)) {
- // we found it
- return ((HtmlTextArea) e).getText();
- }
- }
- }
-
- // not in the current form: try *all* elements
- HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
- if (outside_element != null) {
- if (outside_element instanceof HtmlInput) {
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return ((HtmlInput) outside_element).getValueAttribute();
- }
- if (outside_element instanceof HtmlTextArea) {
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return ((HtmlTextArea) outside_element).getText();
- }
- }
-
- // we can't find it anywhere
- throw new RuntimeException(
- "getTextFieldParameterValue failed, text field with name ["
- + paramName + "] does not exist.");
+ /**
+ * {@inheritDoc}
+ */
+ public void gotoFrame(String frameNameOrId) {
+ WebWindow frame = getFrame(frameNameOrId);
+ if (frame == null) {
+ throw new RuntimeException("No frame found in current page with name or id [" + frameNameOrId + "]");
}
+ win = frame;
+ }
- /**
- * 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 getHiddenFieldValue(String paramName) {
- // first try the current form
- if (form != null) {
- for (HtmlElement e : form.getHtmlElementDescendants()) {
- if (e instanceof HtmlHiddenInput && e.getAttribute("name").equals(paramName)) {
- // we found it
- return ((HtmlInput) e).getValueAttribute();
- }
- }
- }
+ /**
+ * {@inheritDoc}
+ */
+ public void setWorkingForm(int index) {
+ setWorkingForm(getForm(index));
+ }
- // not in the current form: try *all* elements
- HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
- if (outside_element != null) {
- if (outside_element instanceof HtmlHiddenInput) {
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return ((HtmlHiddenInput) outside_element).getValueAttribute();
- }
- }
-
- // we can't find it anywhere
- throw new RuntimeException("No hidden field with name [" + paramName
- + "] was found.");
+ /**
+ * {@inheritDoc}
+ */
+ public void setWorkingForm(String nameOrId, int index) {
+ setWorkingForm(getForm(nameOrId, index));
+ }
+
+ /**
+ * Return true if the current response contains a form.
+ */
+ public boolean hasForm() {
+ return ((HtmlPage) win.getEnclosedPage()).getForms().size() > 0;
+ }
+
+ /**
+ * Return true if the current response contains a specific form.
+ *
+ * @param nameOrID name of id of the form to check for.
+ */
+ public boolean hasForm(String nameOrID) {
+ return getForm(nameOrID) != null;
+ }
+
+ public boolean hasFormParameterNamed(String paramName) {
+ for (HtmlElement e : getCurrentPage().getHtmlElementDescendants()) {
+ if (e.getAttribute("name").equals(paramName)) {
+ // set the working form if none has been set
+ if (e.getEnclosingForm() != null && getWorkingForm() == null)
+ setWorkingForm(e.getEnclosingForm());
+ return true;
+ }
}
+ return false;
+ }
- /**
- * Set a form text, password input element or textarea to the provided value.
- *
- * @param fieldName name of the input element or textarea
- * @param text parameter value to submit for the element.
- */
- public void setTextField(String paramName, String text) {
- // first try the current form
- if (form != null) {
- for (HtmlElement e : form.getHtmlElementDescendants()) {
- if (e instanceof HtmlInput && e.getAttribute("name").equals(paramName)) {
- // we found it
- ((HtmlInput) e).setValueAttribute(text);
- return;
- }
- if (e instanceof HtmlTextArea && e.getAttribute("name").equals(paramName)) {
- // we found it
- ((HtmlTextArea) e).setText(text);
- return;
- }
- }
- }
-
- // not in the current form: try *all* elements
- HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
- if (outside_element != null) {
- if (outside_element instanceof HtmlInput) {
- ((HtmlInput) outside_element).setValueAttribute(text);
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return;
- }
- if (outside_element instanceof HtmlTextArea) {
- ((HtmlTextArea) outside_element).setText(text);
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return;
- }
+ /**
+ * 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 getTextFieldValue(String paramName) {
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getHtmlElementDescendants()) {
+ if (e instanceof HtmlInput && e.getAttribute("name").equals(paramName)) {
+ // we found it
+ return ((HtmlInput) e).getValueAttribute();
}
-
- // we can't find it anywhere
- throw new RuntimeException("No text field with name [" + paramName
- + "] was found.");
- }
-
- /**
- * Set a form hidden element to the provided value.
- *
- * @param fieldName name of the hidden input element
- * @param paramValue parameter value to submit for the element.
- */
- public void setHiddenField(String fieldName, String text) {
- // first try the current form
- if (form != null) {
- for (HtmlElement e : form.getHtmlElementDescendants()) {
- if (e instanceof HtmlHiddenInput && e.getAttribute("name").equals(fieldName)) {
- // we found it
- ((HtmlHiddenInput) e).setValueAttribute(text);
- return;
- }
- }
- }
-
- // not in the current form: try *all* elements
- HtmlElement outside_element = getHtmlElementWithAttribute("name", fieldName);
- if (outside_element != null) {
- if (outside_element instanceof HtmlHiddenInput) {
- ((HtmlHiddenInput) outside_element).setValueAttribute(text);
- // set current form if not null
- if (outside_element.getEnclosingForm() != null)
- form = outside_element.getEnclosingForm();
- return;
- }
+ if (e instanceof HtmlTextArea && e.getAttribute("name").equals(paramName)) {
+ // we found it
+ return ((HtmlTextArea) e).getText();
}
-
- // we can't find it anywhere
- throw new RuntimeException("No hidden field with name [" + fieldName
- + "] was found.");
+ }
}
- /**
- * Return a string array of select box option values.
- *
- * @param selectName name of the select box.
- */
- public String[] getSelectOptionValues(String selectName) {
- HtmlSelect sel = getForm().getSelectByName(selectName);
- ArrayList<String> result = new ArrayList<String>();
- for (HtmlOption opt : sel.getOptions()) {
- result.add(opt.getValueAttribute());
- }
- return result.toArray(new String[result.size()]);
+ // not in the current form: try *all* elements
+ HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
+ if (outside_element != null) {
+ if (outside_element instanceof HtmlInput) {
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return ((HtmlInput) outside_element).getValueAttribute();
+ }
+ if (outside_element instanceof HtmlTextArea) {
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return ((HtmlTextArea) outside_element).getText();
+ }
}
- /**
- * Return a string array of the Nth select box option values.
- *
- * @param selectName name of the select box.
- * @param index the 0-based index when more than one
- * select with the same name is expected.
- */
- public String[] getSelectOptionValues(String selectName, int index) {
- List<HtmlSelect> sels = getForm().getSelectsByName(selectName);
- if ( sels == null || sels.size() < index+1) {
- throw new RuntimeException("Did not find select with name [" + selectName
- + "] at index " + index);
- }
- HtmlSelect sel = sels.get(index);
- ArrayList<String> result = new ArrayList<String>();
- for (HtmlOption opt : sel.getOptions()) {
- result.add(opt.getValueAttribute());
+ // we can't find it anywhere
+ 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 getHiddenFieldValue(String paramName) {
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getHtmlElementDescendants()) {
+ if (e instanceof HtmlHiddenInput && e.getAttribute("name").equals(paramName)) {
+ // we found it
+ return ((HtmlInput) e).getValueAttribute();
}
- return (String[]) result.toArray(new String[result.size()]);
+ }
}
-
- private String[] getSelectedOptions(HtmlSelect sel) {
- String[] result = new String[sel.getSelectedOptions().size()];
- int i = 0;
- for (HtmlOption opt : sel.getSelectedOptions()) {
- result[i++] = opt.getValueAttribute();
- }
- return result;
- }
-
- public String[] getSelectedOptions(String selectName) {
- HtmlSelect sel = getForm().getSelectByName(selectName);
- return getSelectedOptions(sel);
+ // not in the current form: try *all* elements
+ HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
+ if (outside_element != null) {
+ if (outside_element instanceof HtmlHiddenInput) {
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return ((HtmlHiddenInput) outside_element).getValueAttribute();
+ }
}
- public String[] getSelectedOptions(String selectName, int index) {
- List<HtmlSelect> sels = getForm().getSelectsByName(selectName);
- if ( sels == null || sels.size() < index+1) {
- throw new RuntimeException("Did not find select with name [" + selectName
- + "] at index " + index);
- }
- HtmlSelect sel = sels.get(index);
- return getSelectedOptions(sel);
- }
+ // we can't find it anywhere
+ throw new RuntimeException("No hidden field with name [" + paramName
+ + "] was found.");
+ }
-
- private String getSelectOptionValueForLabel(HtmlSelect sel, String label) {
- for (HtmlOption opt : sel.getOptions()) {
- if (opt.asText().equals(label)) {
- return opt.getValueAttribute();
- }
+ /**
+ * Set a form text, password input element or textarea to the provided value.
+ *
+ * @param fieldName name of the input element or textarea
+ * @param text parameter value to submit for the element.
+ */
+ public void setTextField(String paramName, String text) {
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getHtmlElementDescendants()) {
+ if (e instanceof HtmlInput && e.getAttribute("name").equals(paramName)) {
+ // we found it
+ ((HtmlInput) e).setValueAttribute(text);
+ return;
}
- throw new RuntimeException("Unable to find option " + label + " for "
- + sel.getNameAttribute());
+ if (e instanceof HtmlTextArea && e.getAttribute("name").equals(paramName)) {
+ // we found it
+ ((HtmlTextArea) e).setText(text);
+ return;
+ }
+ }
}
- public String getSelectOptionValueForLabel(String selectName, String label) {
- HtmlSelect sel = getForm().getSelectByName(selectName);
- return getSelectOptionValueForLabel(sel, label);
+ // not in the current form: try *all* elements
+ HtmlElement outside_element = getHtmlElementWithAttribute("name", paramName);
+ if (outside_element != null) {
+ if (outside_element instanceof HtmlInput) {
+ ((HtmlInput) outside_element).setValueAttribute(text);
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return;
+ }
+ if (outside_element instanceof HtmlTextArea) {
+ ((HtmlTextArea) outside_element).setText(text);
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return;
+ }
}
-
- public String getSelectOptionValueForLabel(String selectName, int index, String label) {
- List<HtmlSelect> sels = getForm().getSelectsByName(selectName);
- if ( sels == null || sels.size() < index+1) {
- throw new RuntimeException("Did not find select with name [" + selectName
- + "] at index " + index);
+
+ // we can't find it anywhere
+ throw new RuntimeException("No text field with name [" + paramName
+ + "] was found.");
+ }
+
+ /**
+ * Set a form hidden element to the provided value.
+ *
+ * @param fieldName name of the hidden input element
+ * @param paramValue parameter value to submit for the element.
+ */
+ public void setHiddenField(String fieldName, String text) {
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getHtmlElementDescendants()) {
+ if (e instanceof HtmlHiddenInput && e.getAttribute("name").equals(fieldName)) {
+ // we found it
+ ((HtmlHiddenInput) e).setValueAttribute(text);
+ return;
}
- HtmlSelect sel = (HtmlSelect)sels.get(index);
- return getSelectOptionValueForLabel(sel, label);
+ }
}
- private String getSelectOptionLabelForValue(HtmlSelect sel, String value) {
- for (HtmlOption opt : sel.getOptions()) {
- if (opt.getValueAttribute().equals(value)) {
- return opt.asText();
- }
- }
- throw new RuntimeException("Unable to find option " + value + " for "
- + sel.getNameAttribute());
+ // not in the current form: try *all* elements
+ HtmlElement outside_element = getHtmlElementWithAttribute("name", fieldName);
+ if (outside_element != null) {
+ if (outside_element instanceof HtmlHiddenInput) {
+ ((HtmlHiddenInput) outside_element).setValueAttribute(text);
+ // set current form if not null
+ if (outside_element.getEnclosingForm() != null)
+ form = outside_element.getEnclosingForm();
+ return;
+ }
}
-
- public String getSelectOptionLabelForValue(String selectName, String value) {
- HtmlSelect sel = getForm().getSelectByName(selectName);
- return getSelectOptionLabelForValue(sel, value);
+
+ // we can't find it anywhere
+ throw new RuntimeException("No hidden field with name [" + fieldName
+ + "] was found.");
+ }
+
+ /**
+ * Return a string array of select box option values.
+ *
+ * @param selectName name of the select box.
+ */
+ public String[] getSelectOptionValues(String selectName) {
+ HtmlSelect sel = getForm().getSelectByName(selectName);
+ ArrayList<String> result = new ArrayList<String>();
+ for (HtmlOption opt : sel.getOptions()) {
+ result.add(opt.getValueAttribute());
}
-
- public String getSelectOptionLabelForValue(String selectName, int index, String value) {
- List<HtmlSelect> sels = getForm().getSelectsByName(selectName);
- if ( sels == null || sels.size() < index+1) {
- throw new RuntimeException("Did not find select with name [" + selectName
- + "] at index " + index);
- }
- HtmlSelect sel = (HtmlSelect)sels.get(index);
- return getSelectOptionLabelForValue(sel, value);
- }
+ return result.toArray(new String[result.size()]);
+ }
-
-
- public URL getPageURL() {
- return win.getEnclosedPage().getWebResponse().getWebRequest().getUrl();
+ /**
+ * Return a string array of the Nth select box option values.
+ *
+ * @param selectName name of the select box.
+ * @param index the 0-based index when more than one
+ * select with the same name is expected.
+ */
+ public String[] getSelectOptionValues(String selectName, int index) {
+ List<HtmlSelect> sels = getForm().getSelectsByName(selectName);
+ if (sels == null || sels.size() < index + 1) {
+ throw new RuntimeException("Did not find select with name [" + selectName
+ + "] at index " + index);
}
-
- public String getPageSource() {
- return win.getEnclosedPage().getWebResponse()
- .getContentAsString();
+ HtmlSelect sel = sels.get(index);
+ ArrayList<String> result = new ArrayList<String>();
+ for (HtmlOption opt : sel.getOptions()) {
+ result.add(opt.getValueAttribute());
}
+ return (String[]) result.toArray(new String[result.size()]);
+ }
- public String getPageTitle() {
- return getCurrentPageTitle();
+ private String[] getSelectedOptions(HtmlSelect sel) {
+ String[] result = new String[sel.getSelectedOptions().size()];
+ int i = 0;
+ for (HtmlOption opt : sel.getSelectedOptions()) {
+ result[i++] = opt.getValueAttribute();
}
+ return result;
+ }
- public String getPageText() {
- Page page = win.getEnclosedPage();
- if (page instanceof HtmlPage) {
- return ((HtmlPage) page).getBody().asText();
- }
- if (page instanceof TextPage) {
- return ((TextPage) page).getContent();
- }
- if (page instanceof JavaScriptPage) {
- return ((JavaScriptPage) page).getContent();
- }
- if (page instanceof XmlPage) {
- return ((XmlPage) page).getContent();
- }
- if (page instanceof UnexpectedPage) {
- return ((UnexpectedPage) page).getWebResponse()
- .getContentAsString();
- }
- throw new RuntimeException(
- "Unexpected error in getPageText(). This method need to be updated.");
- }
+ public String[] getSelectedOptions(String selectName) {
+ HtmlSelect sel = getForm().getSelectByName(selectName);
+ return getSelectedOptions(sel);
+ }
- public String getServerResponse() {
- StringBuf...
[truncated message content] |