Revision: 753
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=753&view=rev
Author: jevonwright
Date: 2008-10-28 23:50:09 +0000 (Tue, 28 Oct 2008)
Log Message:
-----------
added assertHeader*() and assertResponseCode*() methods
cleaning up HtmlUnitTestingEngineImpl and adding more documentation
Modified Paths:
--------------
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ResponseServlet.java
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/ITestingEngine.java
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java
trunk/jwebunit-webtestcase-generator/src/main/javacc/Java1.5.jj
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
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResponseServletTest.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -28,11 +28,17 @@
/*
* currently we can't get the response code from HtmlUnit unless it is a
failing code
- *
+ */
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}");
}
public void testResponse200() {
@@ -41,7 +47,6 @@
submit();
assertResponseCode(200);
}
- */
/*
* HtmlUnit cannot handle a 301 without a valid Location: header
@@ -51,7 +56,7 @@
submit();
assertResponseCode(301);
}
- */
+ */
public void testResponse404() {
beginAt("/SimpleForm.html");
Modified:
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ResponseServlet.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ResponseServlet.java
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/ResponseServlet.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -32,7 +32,11 @@
// set the content type?
if (request.getParameter("content-type") != null &&
request.getParameter("content-type").length() > 0) {
response.setContentType( request.getParameter("content-type") );
- }
+ }
+
+ // lets set some headers
+ response.setHeader("Test", "test2");
+ response.setHeader("Header-Added", new java.util.Date().toString());
}
Modified:
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/ITestingEngine.java
===================================================================
---
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/ITestingEngine.java
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/api/ITestingEngine.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -878,6 +878,21 @@
int getServerResponseCode();
/**
+ * Get a particular header or null.
+ *
+ * @param name The header name
+ * @return The first header value or null
+ */
+ String getHeader(String name);
+
+ /**
+ * Get all headers.
+ *
+ * @return The header values stored in a map.
+ */
+ java.util.Map<String,String> getAllHeaders();
+
+ /**
* Should the tester ignore failing status codes (300+)? Otherwise,
* failing status codes will throw an exception.
*
Modified:
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
===================================================================
---
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -17,6 +17,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
@@ -281,6 +282,70 @@
public void setIgnoreFailingStatusCodes(boolean ignore) {
getTestingEngine().setIgnoreFailingStatusCodes(ignore);
}
+
+ /**
+ * Assert a header is present.
+ *
+ * @param name The header to find
+ */
+ public void assertHeaderPresent(String name) {
+ Assert.assertFalse( "header '" + name + "' not present",
getTestingEngine().getHeader(name) == null );
+ }
+
+ /**
+ * Assert a header is NOT present.
+ *
+ * @param name The header to find
+ */
+ public void assertHeaderNotPresent(String name) {
+ Assert.assertTrue( "header '" + name + "' present",
getTestingEngine().getHeader(name) == null );
+ }
+
+ /**
+ * Assert a header is equal to a particular value.
+ *
+ * @param name Header to find
+ * @param value Value to compare against
+ */
+ public void assertHeaderEquals(String name, String value) {
+ Assert.assertEquals( value, getTestingEngine().getHeader(name) );
+ }
+
+ /**
+ * Assert a header matches a particular pattern.
+ *
+ * @param name Header to find
+ * @param regexp Pattern to compare against
+ */
+ public void assertHeaderMatches(String name, String regexp) {
+ RE re = null;
+ try {
+ re = new RE(regexp, RE.MATCH_SINGLELINE);
+ } catch (RESyntaxException e) {
+ Assert.fail(e.toString());
+ }
+ Assert.assertTrue("Unable to match [" + regexp + "] in header [" +
name + "]",
+ re.match( getTestingEngine().getHeader(name) ));
+ }
+
+ /**
+ * Get a particular header value.
+ *
+ * @param name Header to find
+ * @return The found header value, or null
+ */
+ public String getHeader(String name) {
+ return getTestingEngine().getHeader(name);
+ }
+
+ /**
+ * Get all response headers.
+ *
+ * @return A map of response headers
+ */
+ public Map<String, String> getAllHeaders() {
+ return getTestingEngine().getAllHeaders();
+ }
/**
* Assert title of current html page in conversation matches an expected
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
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitTestingEngineImpl.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -142,11 +142,6 @@
* Javascript prompts.
*/
private LinkedList<JavascriptPrompt> expectedJavascriptPrompts = new
LinkedList<JavascriptPrompt>();
-
- /**
- * The last web response status code, if HtmlUnit threw a
FailingHttpStatusCodeException.
- */
- private int lastWebResponse = 0;
/**
* Should we ignore failing status codes?
@@ -169,6 +164,10 @@
gotoPage(initialURL);
}
+ /**
+ * 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 {
@@ -194,7 +193,8 @@
/**
* Go to a particular page.
*
- * @throws TestingEngineResponseException if an error response code is
encountered and ignoreFailingStatusCodes is not enabled.
+ * @throws TestingEngineResponseException if an error response code is
encountered
+ * and ignoreFailingStatusCodes is not enabled.
*/
public void gotoPage(URL initialURL) throws TestingEngineResponseException
{
try {
@@ -202,9 +202,6 @@
win = wc.getCurrentWindow();
form = null;
} catch (FailingHttpStatusCodeException ex) {
- // save this web response
- lastWebResponse = ex.getStatusCode();
-
// only throw exception if necessary
if (!ignoreFailingStatusCodes) {
throw new TestingEngineResponseException(
@@ -294,6 +291,9 @@
}
}
+ /**
+ * Close the current window.
+ */
public void closeWindow() {
if (win != null) {
wc.deregisterWebWindow(win);
@@ -414,31 +414,30 @@
* name.
*/
public String getHiddenFieldValue(String paramName) {
- List<HtmlElement> hiddenFieldElements = new LinkedList<HtmlElement>();
- if (form != null) {
- hiddenFieldElements.addAll(getForm().getHtmlElementsByAttribute(
- "input", "type", "hidden"));
- } else {
- for (Iterator<HtmlForm> i =
getCurrentPage().getForms().iterator(); i
- .hasNext();) {
- HtmlForm f = (HtmlForm) i.next();
- hiddenFieldElements.addAll(f.getHtmlElementsByAttribute(
- "input", "type", "hidden"));
- }
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getAllHtmlChildElements()) {
+ if (e instanceof HtmlHiddenInput &&
e.getAttribute("name").equals(paramName)) {
+ // we found it
+ return ((HtmlInput) e).getValueAttribute();
+ }
+ }
+ }
+
+ // 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();
+ }
}
- Iterator<HtmlElement> it = hiddenFieldElements.iterator();
- while (it.hasNext()) {
- HtmlHiddenInput hiddenInput = (HtmlHiddenInput) it.next();
- if (paramName.equals(hiddenInput.getNameAttribute())) {
- if (form == null) {
- form = hiddenInput.getEnclosingFormOrDie();
- }
- return hiddenInput.getValueAttribute();
- }
- }
- throw new RuntimeException(
- "getHiddenFieldParameterValue failed, hidden field with name ["
- + paramName + "] does not exist.");
+
+ // we can't find it anywhere
+ throw new RuntimeException("No hidden field with name [" + paramName
+ + "] was found.");
}
/**
@@ -495,26 +494,30 @@
* @param paramValue parameter value to submit for the element.
*/
public void setHiddenField(String fieldName, String text) {
- List<HtmlInput> hiddenFieldElements = new LinkedList<HtmlInput>();
- if (form != null) {
- hiddenFieldElements.addAll(getForm().getInputsByName(fieldName));
- } else {
- for (Iterator<HtmlForm> i =
getCurrentPage().getForms().iterator(); i
- .hasNext();) {
- HtmlForm f = (HtmlForm) i.next();
- hiddenFieldElements.addAll(f.getInputsByName(fieldName));
- }
+ // first try the current form
+ if (form != null) {
+ for (HtmlElement e : form.getAllHtmlChildElements()) {
+ 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;
+ }
}
- for (Iterator<HtmlInput> i = hiddenFieldElements.iterator();
i.hasNext();) {
- HtmlElement e = (HtmlElement) i.next();
- if (e instanceof HtmlHiddenInput) {
- ((HtmlHiddenInput) e).setValueAttribute(text);
- if (form == null) {
- form = e.getEnclosingFormOrDie();
- }
- return;
- }
- }
+
+ // we can't find it anywhere
throw new RuntimeException("No hidden field with name [" + fieldName
+ "] was found.");
}
@@ -1428,7 +1431,6 @@
throw new TestingEngineResponseException(
e.getStatusCode(), e);
- lastWebResponse = e.getStatusCode();
return;
} catch (IOException e) {
throw new RuntimeException(
@@ -1475,7 +1477,6 @@
throw new TestingEngineResponseException(
e.getStatusCode(), e);
- lastWebResponse = e.getStatusCode();
return;
} catch (IOException e) {
throw new RuntimeException(
@@ -1527,7 +1528,6 @@
throw new TestingEngineResponseException(
e.getStatusCode(), e);
- lastWebResponse = e.getStatusCode();
return;
} catch (IOException e) {
throw new RuntimeException(
@@ -2175,8 +2175,15 @@
* @see
net.sourceforge.jwebunit.api.ITestingEngine#getServerResponseCode()
*/
public int getServerResponseCode() {
- return this.lastWebResponse;
+ return getWebResponse().getStatusCode();
}
+
+ /**
+ * Get the last WebResponse from HtmlUnit.
+ */
+ public WebResponse getWebResponse() {
+ return wc.getCurrentWindow().getEnclosedPage().getWebResponse();
+ }
/*
* @return the ignoreFailingStatusCodes
@@ -2192,4 +2199,22 @@
this.ignoreFailingStatusCodes = ignore;
}
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.ITestingEngine#getHeader(java.lang.String)
+ */
+ public String getHeader(String name) {
+ return getWebResponse().getResponseHeaderValue(name);
+ }
+
+ /* (non-Javadoc)
+ * @see net.sourceforge.jwebunit.api.ITestingEngine#getAllHeaders()
+ */
+ public Map<String, String> getAllHeaders() {
+ Map<String, String> map = new java.util.HashMap<String,
String>();
+ for (NameValuePair header :
getWebResponse().getResponseHeaders()) {
+ map.put(header.getName(), header.getValue());
+ }
+ return map;
+ }
+
}
Modified:
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java
===================================================================
---
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java
2008-10-28 12:34:23 UTC (rev 752)
+++
trunk/jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java
2008-10-28 23:50:09 UTC (rev 753)
@@ -9,6 +9,7 @@
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import net.sourceforge.jwebunit.api.IElement;
import net.sourceforge.jwebunit.api.ITestingEngine;
@@ -695,4 +696,28 @@
throw new
UnsupportedOperationException("getServerResponseCode");
}
+ /* (non-Javadoc)
+ * @see net.sourceforge.jwebunit.api.ITestingEngine#getAllHeaders()
+ */
+ public Map<String, String> getAllHeaders() {
+ // TODO implement method
+ throw new UnsupportedOperationException("getAllHeaders");
+ }
+
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.ITestingEngine#getHeader(java.lang.String)
+ */
+ public String getHeader(String name) {
+ // TODO implement method
+ throw new UnsupportedOperationException("getHeader");
+ }
+
+ /* (non-Javadoc)
+ * @see
net.sourceforge.jwebunit.api.ITestingEngine#setIgnoreFailingStatusCodes(boolean)
+ */
+ public void setIgnoreFailingStatusCodes(boolean ignore) {
+ // TODO implement method
+ throw new
UnsupportedOperationException("setIgnoreFailingStatusCodes");
+ }
+
}
Modified: trunk/jwebunit-webtestcase-generator/src/main/javacc/Java1.5.jj
===================================================================
--- trunk/jwebunit-webtestcase-generator/src/main/javacc/Java1.5.jj
2008-10-28 12:34:23 UTC (rev 752)
+++ trunk/jwebunit-webtestcase-generator/src/main/javacc/Java1.5.jj
2008-10-28 23:50:09 UTC (rev 753)
@@ -491,6 +491,7 @@
sb.append("import java.io.File;\n");
sb.append("import java.io.PrintStream;\n");
sb.append("import java.util.List;\n");
+ sb.append("import java.util.Map;\n");
sb.append("import java.net.URL;\n\n");
sb.append("import net.sourceforge.jwebunit.api.IElement;\n");
sb.append("import net.sourceforge.jwebunit.api.ITestingEngine;\n");
Modified: trunk/src/changes/changes.xml
===================================================================
--- trunk/src/changes/changes.xml 2008-10-28 12:34:23 UTC (rev 752)
+++ trunk/src/changes/changes.xml 2008-10-28 23:50:09 UTC (rev 753)
@@ -9,8 +9,11 @@
<body>
<release version="2.1" date="Unknown">
<action type="add" dev="jevonwright">
- Adding new IElement element interface, and methods to
directly access elements by XPath.
+ Added new IElement element interface, and methods to
directly access elements by XPath.
</action>
+ <action type="add" dev="jevonwright" issue="1744628">
+ Added assertHeader*() and assertResponseCode*() methods
+ </action>
<action type="update" dev="jevonwright">
BC CHANGE: setFormElement(), assertFormElementEquals()
methods will no longer assert that a form already exists in the page (as
allowed by the HTML standard).
</action>
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development