Revision: 808
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=808&view=rev
Author: jevonwright
Date: 2009-06-01 23:02:01 +0000 (Mon, 01 Jun 2009)
Log Message:
-----------
creating branch for resolving bug 2791025: Alerts or exceptions thrown within
Ajax are not caught
Modified Paths:
--------------
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
branches/bug-2791025/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
Added Paths:
-----------
branches/bug-2791025/
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
Removed Paths:
-------------
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
Modified:
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JavaScriptTest.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -30,9 +30,9 @@
//assertTextPresent("Hello World");
}
- public void testAlert() {
+ public void testAlert() throws Exception {
setExpectedJavaScriptAlert("Foo Bar");
- beginAt("Alert.html");
+ beginAt("Alert.html");
}
public void testInvalidAlertOnPageLoad() {
@@ -96,4 +96,46 @@
assertTextNotPresent("not loaded");
}
+ /**
+ * Make sure that when Prototype is called and an alert box
+ * is thrown, that this alert box stops execution.
+ *
+ * @see bug 2791025
+ * @author Jevon
+ */
+ public void testPrototypeJsAlert() throws InterruptedException {
+ beginAt("prototype-alert.html");
+ setExpectedJavaScriptAlert("Alert box from Ajax response");
+ clickButtonWithText("do ajax");
+ // we wait a while for the ajax to return
+ Thread.sleep(500);
+ assertTextPresent("hello, world!");
+ assertTextNotPresent("not loaded");
+ }
+
+ /**
+ * Make sure that when Prototype is called and an alert box
+ * is thrown, that this alert box stops execution.
+ *
+ * @see bug 2791025
+ * @author Jevon
+ */
+ public void testPrototypeJsAlertInvalid() throws InterruptedException {
+ beginAt("prototype-alert.html");
+ setExpectedJavaScriptAlert("This alert text will never appear");
+ try {
+ clickButtonWithText("do ajax");
+ Thread.sleep(500);
+ // we wait a while for the ajax to return
+ fail("The prototype alert box was never thrown");
+ } catch (Exception e) {
+ if (e instanceof InterruptedException) {
+ throw (InterruptedException) e; // rethrow
+ }
+ // OK
+ }
+ assertTextPresent("hello, world!");
+ assertTextNotPresent("not loaded");
+ }
+
}
Deleted:
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -1,55 +0,0 @@
-/******************************************************************************
- * JWebUnit project (http://jwebunit.sourceforge.net) *
- * Distributed open-source, see full license under LICENCE.txt *
-
******************************************************************************/
-package net.sourceforge.jwebunit.tests;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import net.sourceforge.jwebunit.tests.util.JettySetup;
-
-/**
- * Test the Cookies methods provided by WebTestCase.
- *
- * @author Julien HENRY
- */
-public class WebCookieTest extends JWebUnitAPITestCase {
-
-
- public static Test suite() {
- return new JettySetup(new TestSuite(WebCookieTest.class));
- }
-
- public void setUp() throws Exception {
- super.setUp();
- getTestContext().addCookie("cookie1", "Cookievalue1",
"localhost");
- setBaseUrl(HOST_PATH);
- }
-
- public void testAddCookie() {
- beginAt("/cookies.jsp");
- assertTextPresent("cookie1=Cookievalue1");
- }
-
- public void testAddAnotherCookie() {
- getTestContext().addCookie("cookie2", "Cookievalue2", "localhost");
- beginAt("/cookies.jsp");
- assertTextPresent("cookie1=Cookievalue1");
- assertTextPresent("cookie2=Cookievalue2");
- }
-
- public void testAssertCookiePresent() throws Throwable {
- beginAt("/cookies.jsp");
- assertCookiePresent("serveurCookie");
- }
-
- public void testAssertCookieValue() throws Throwable {
- beginAt("/cookies.jsp");
- assertCookieValueEquals("serveurCookie", "foo");
- }
-
- public void testAssertCookieMatch() throws Throwable {
- beginAt("/cookies.jsp");
- assertCookieValueMatch("serveurCookie", "fo*");
- }
-}
\ No newline at end of file
Copied:
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
(from rev 791,
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java)
===================================================================
---
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
(rev 0)
+++
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/WebCookieTest.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -0,0 +1,73 @@
+/******************************************************************************
+ * JWebUnit project (http://jwebunit.sourceforge.net) *
+ * Distributed open-source, see full license under LICENCE.txt *
+
******************************************************************************/
+package net.sourceforge.jwebunit.tests;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import net.sourceforge.jwebunit.tests.util.JettySetup;
+
+/**
+ * Test the Cookies methods provided by WebTestCase.
+ *
+ * @author Julien HENRY
+ */
+public class WebCookieTest extends JWebUnitAPITestCase {
+
+
+ public static Test suite() {
+ return new JettySetup(new TestSuite(WebCookieTest.class));
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ getTestContext().addCookie("cookie1", "Cookievalue1",
"localhost");
+ setBaseUrl(HOST_PATH);
+ }
+
+ public void testAddCookie() {
+ beginAt("/cookies.jsp");
+ assertTextPresent("cookie1=Cookievalue1");
+ }
+
+ public void testAddAnotherCookie() {
+ getTestContext().addCookie("cookie2", "Cookievalue2", "localhost");
+ beginAt("/cookies.jsp");
+ assertTextPresent("cookie1=Cookievalue1");
+ assertTextPresent("cookie2=Cookievalue2");
+ }
+
+ public void testAssertCookiePresent() throws Throwable {
+ beginAt("/cookies.jsp");
+ assertCookiePresent("serveurCookie");
+ }
+
+ public void testAssertCookieValue() throws Throwable {
+ beginAt("/cookies.jsp");
+ assertCookieValueEquals("serveurCookie", "foo");
+ }
+
+ public void testAssertCookieMatch() throws Throwable {
+ beginAt("/cookies.jsp");
+ assertCookieValueMatch("serveurCookie", "fo*");
+ }
+
+ /**
+ * Test that the cookie still exists across multiple requests,
+ * even if the cookie is not explicitly set each time.
+ */
+ public void testCookieWithoutExplicitSet() {
+ beginAt("/cookies.jsp"); // beginAt also resets cookies
+ assertCookieValueEquals("serveurCookie", "foo");
+ gotoPage("/cookies.jsp?dont_set=1");
+ assertCookieValueEquals("serveurCookie", "foo"); // should still
be there
+ gotoPage("/cookies.jsp?dont_set=1");
+ assertCookieValueEquals("serveurCookie", "foo"); // should still
be there
+ gotoPage("/cookies.jsp?dont_set=1");
+ assertCookieValueEquals("serveurCookie", "foo"); // should still
be there
+
+
+ }
+
+}
\ No newline at end of file
Deleted:
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
===================================================================
---
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -1,59 +0,0 @@
-/******************************************************************************
- * JWebUnit project (http://jwebunit.sourceforge.net) *
- * Distributed open-source, see full license under LICENCE.txt *
-
******************************************************************************/
-package net.sourceforge.jwebunit.tests.util;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-public class CookiesServlet extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException,
IOException {
- doPost(request, response);
- }
-
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException,
IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.write(HtmlHelper.getStart("Submitted cookies"));
- out.write("<h1>Submitted cookies</h1>\n<p>Cookies are:");
- /*
- * Prints POST and GET cookies as name=value1[,value2...]
separated with
- * <br/>
- */
-
- Cookie[] cookies = request.getCookies();
- if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- out.write(cookies[i].getName() + "=" +
cookies[i].getValue()
- + "<br/>");
- }
- }
-
- out.write(" </p>\n");
- String ref = request.getHeader("Referer");
- if (ref == null) {
- if (request.getParameterValues("myReferer") != null) {
- ref =
request.getParameterValues("myReferer")[0];
- }
- }
- out.write(HtmlHelper.getLinkParagraph("return", ref));
-
- out.write(HtmlHelper.getEnd());
-
- Cookie cookie = new Cookie("serveurCookie","foo");
- response.addCookie(cookie);
- }
-
-}
Copied:
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
(from rev 791,
trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java)
===================================================================
---
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
(rev 0)
+++
branches/bug-2791025/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/CookiesServlet.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -0,0 +1,62 @@
+/******************************************************************************
+ * JWebUnit project (http://jwebunit.sourceforge.net) *
+ * Distributed open-source, see full license under LICENCE.txt *
+
******************************************************************************/
+package net.sourceforge.jwebunit.tests.util;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class CookiesServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ protected void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException,
IOException {
+ doPost(request, response);
+ }
+
+ protected void doPost(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException,
IOException {
+ response.setContentType("text/html");
+ PrintWriter out = response.getWriter();
+ out.write(HtmlHelper.getStart("Submitted cookies"));
+ out.write("<h1>Submitted cookies</h1>\n<p>Cookies are:");
+ /*
+ * Prints POST and GET cookies as name=value1[,value2...]
separated with
+ * <br/>
+ */
+
+ Cookie[] cookies = request.getCookies();
+ if (cookies != null) {
+ for (int i = 0; i < cookies.length; i++) {
+ out.write(cookies[i].getName() + "=" +
cookies[i].getValue()
+ + "<br/>");
+ }
+ }
+
+ out.write(" </p>\n");
+ String ref = request.getHeader("Referer");
+ if (ref == null) {
+ if (request.getParameterValues("myReferer") != null) {
+ ref =
request.getParameterValues("myReferer")[0];
+ }
+ }
+ out.write(HtmlHelper.getLinkParagraph("return", ref));
+
+ out.write(HtmlHelper.getEnd());
+
+ // to disable explicitly setting the cookie on each request
+ if (request.getParameter("dont_set") == null) {
+ Cookie cookie = new Cookie("serveurCookie","foo");
+ response.addCookie(cookie);
+ }
+ }
+
+}
Modified:
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
===================================================================
---
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -386,7 +386,7 @@
* @param regexp expected title regexp
*/
public void assertTitleMatch(String regexp) {
- assertMatch("Unable to match [" + regexp + "] in title", regexp,
getTestingEngine().getPageTitle());
+ assertMatch("Unable to match [" + regexp + "] in title '" +
getTestingEngine().getPageTitle() + "'", regexp,
getTestingEngine().getPageTitle());
}
/**
Deleted:
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
===================================================================
---
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -1,336 +0,0 @@
-/******************************************************************************
- * JWebUnit project (http://jwebunit.sourceforge.net) *
- * Distributed open-source, see full license under LICENCE.txt *
-
******************************************************************************/
-package net.sourceforge.jwebunit.util;
-
-import javax.servlet.http.Cookie;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-
-/**
- * Establish context for tests (things such as locale, base url for the
application, cookies, authorization). The
- * context can be accessed through the {...@link
net.sourceforge.jwebunit.WebTestCase}or
- * {...@link net.sourceforge.jwebunit.junit.WebTester}.
- *
- * @author Wilkes Joiner
- * @author Jim Weaver
- * @author Julien Henry
- */
-public class TestContext {
- private String user;
-
- private String passwd;
-
- private String domain;
-
- private List<javax.servlet.http.Cookie> cookies;
-
- private boolean hasAuth = false;
-
- private boolean hasNTLMAuth = false;
-
- private Locale locale = Locale.getDefault();
-
- private String resourceBundleName;
-
- private URL baseUrl;
-
- private String userAgent;
-
- private Map<String, String> requestHeaders = new HashMap<String, String>();
-
- private String proxyUser = null;
-
- private String proxyPasswd = null;
-
- private String proxyHost = null;
-
- private int proxyPort = -1;
-
- private boolean hasProxyAuth = false;
-
- /**
- * Construct a test client context.
- */
- public TestContext() {
- cookies = new ArrayList<javax.servlet.http.Cookie>();
- try {
- baseUrl = new URL("http://localhost:8080");
- } catch (MalformedURLException e) {
- // Should not be invalid
- e.printStackTrace();
- }
- }
-
- /**
- * Clear all authorizations (basic, digest, ntlm, proxy).
- *
- */
- public void clearAuthorizations() {
- hasAuth = false;
- hasNTLMAuth = false;
- hasProxyAuth = false;
- }
-
- /**
- * Set basic authentication information for the test context.
- *
- * @param user user name
- * @param passwd password
- */
- public void setAuthorization(String user, String passwd) {
- this.user = user;
- this.passwd = passwd;
- hasAuth = true;
- }
-
- /**
- * Set NTLM authentication information for the test context.
- *
- * @param user user name
- * @param passwd password
- */
- public void setNTLMAuthorization(String user, String passwd, String
domain) {
- this.user = user;
- this.passwd = passwd;
- this.domain = domain;
- hasNTLMAuth = true;
- }
-
- /**
- * Set proxy authentication information for the test context.
- *
- * @param user user name (null if none)
- * @param passwd password (null if none)
- * @param host proxy host name (null if applicable to any host).
- * @param port proxy port (negative if applicable to any port).
- */
- public void setProxyAuthorization(String user, String passwd, String host,
- int port) {
- this.proxyUser = user;
- this.proxyPasswd = passwd;
- this.proxyHost = host;
- this.proxyPort = port;
- hasProxyAuth = !(null == user && null == passwd);
- }
-
- /**
- * Add a cookie to the test context. These cookies are set on the
conversation when you use a {WebTester#beginAt}.
- *
- * @param name cookie name.
- * @param value cookie value.
- * @param domain cookie domain (ie localhost or www.foo.bar).
- */
- public void addCookie(String name, String value, String domain) {
- Cookie c = new Cookie(name, value);
- c.setDomain(domain);
- cookies.add(c);
- }
-
- /**
- * Add a cookie to the test context. These cookies are set on the
conversation when you use a {WebTester#beginAt}.
- *
- * @param cookie a cookie.
- */
- public void addCookie(Cookie cookie) {
- cookies.add(cookie);
- }
-
- /**
- * Return true if a basic authentication has been set on the context via
{...@link #setAuthorization}.
- */
- public boolean hasAuthorization() {
- return hasAuth;
- }
-
- /**
- * Return true if a NTLM authentication has been set on the context via
{...@link #setNTLMAuthorization}.
- */
- public boolean hasNTLMAuthorization() {
- return hasNTLMAuth;
- }
-
- /**
- * Return true if a proxy authentication has been set on the context via
{...@link #setProxyAuthorization}.
- */
- public boolean hasProxyAuthorization() {
- return hasProxyAuth;
- }
-
- /**
- * Return true if one or more cookies have been added to the test context.
- */
- public boolean hasCookies() {
- return cookies.size() > 0;
- }
-
- /**
- * Return the authorized user for the test context.
- */
- public String getUser() {
- return user;
- }
-
- /**
- * Return the user password.
- */
- public String getPassword() {
- return passwd;
- }
-
- /**
- * Return the user domain.
- */
- public String getDomain() {
- return domain;
- }
-
- /**
- * Return the cookies which have been added to the test context.
- */
- public List<javax.servlet.http.Cookie> getCookies() {
- return cookies;
- }
-
- public String getUserAgent() {
- return userAgent;
- }
-
- public void setUserAgent(String userAgent) {
- this.userAgent = userAgent;
- }
-
- public boolean hasUserAgent() {
- return userAgent != null;
- }
-
- /**
- * Return the locale established for the test context. If the locale has
not been explicitly set,
- * Locale.getDefault() will be returned.
- */
- public Locale getLocale() {
- return locale;
- }
-
- /**
- * Set the locale for the test context.
- */
- public void setLocale(Locale locale) {
- this.locale = locale;
- }
-
- /**
- * Set a resource bundle to use for the test context (will be used to
lookup expected values by key in WebTester).
- *
- * @param name path name of the resource bundle.
- */
- public void setResourceBundleName(String name) {
- resourceBundleName = name;
- }
-
- /**
- * Return the test context resource bundle for expected value lookups.
- */
- public String getResourceBundleName() {
- return resourceBundleName;
- }
-
- /**
- * Return the proxy server name
- */
- public String getProxyHost() {
- return proxyHost;
- }
-
- /**
- * Return the proxy server port
- */
- public int getProxyPort() {
- return proxyPort;
- }
-
- /**
- * Return the proxy user name
- */
- public String getProxyUser() {
- return proxyUser;
- }
-
- /**
- * Return the proxy password
- */
- public String getProxyPasswd() {
- return proxyPasswd;
- }
-
- /**
- * Return the base URL for the test context. The default base URL is port
8080 on localhost.
- */
- public URL getBaseUrl() {
- return baseUrl;
- }
-
- /**
- * Set the base url for the test context.
- *
- * @param url Base url value - A trailing "/" is appended if not provided.
- */
- public void setBaseUrl(String url) {
- try {
- baseUrl = new URL(url.endsWith("/") ? url : url + "/");
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Set the base url for the test context.
- *
- * @param url Base url value. Anything after trailing "/" will be skipped.
- */
- public void setBaseUrl(URL url) {
- baseUrl = url;
- }
-
- /**
- * Add a custom request header.
- * @param name header name.
- * @param value header value.
- */
- public void addRequestHeader(final String name, final String value) {
- requestHeaders.put(name, value);
- }
-
- /**
- * Remove a custom request header.
- * @param name header name.
- */
- public void removeRequestHeader(final String name) {
- requestHeaders.remove(name);
- }
-
- /**
- * Get custom request headers.
- * @param name header name.
- * @param value header value.
- */
- public Map<String, String> getRequestHeaders() {
- return requestHeaders;
- }
-
- /**
- * Clear custom request headers.
- *
- */
- public void clearRequestHeaders() {
- requestHeaders = new HashMap<String, String>();
- }
-
-}
Copied:
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
(from rev 791,
trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java)
===================================================================
---
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
(rev 0)
+++
branches/bug-2791025/jwebunit-core/src/main/java/net/sourceforge/jwebunit/util/TestContext.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -0,0 +1,351 @@
+/******************************************************************************
+ * JWebUnit project (http://jwebunit.sourceforge.net) *
+ * Distributed open-source, see full license under LICENCE.txt *
+
******************************************************************************/
+package net.sourceforge.jwebunit.util;
+
+import javax.servlet.http.Cookie;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * Establish context for tests (things such as locale, base url for the
application, cookies, authorization). The
+ * context can be accessed through the {...@link
net.sourceforge.jwebunit.WebTestCase}or
+ * {...@link net.sourceforge.jwebunit.junit.WebTester}.
+ *
+ * @author Wilkes Joiner
+ * @author Jim Weaver
+ * @author Julien Henry
+ */
+public class TestContext {
+ private String user;
+
+ private String passwd;
+
+ private String domain;
+
+ private List<javax.servlet.http.Cookie> cookies;
+
+ private boolean hasAuth = false;
+
+ private boolean hasNTLMAuth = false;
+
+ private Locale locale = Locale.getDefault();
+
+ private String resourceBundleName;
+
+ private URL baseUrl;
+
+ private String userAgent;
+
+ private Map<String, String> requestHeaders = new HashMap<String, String>();
+
+ private String proxyUser = null;
+
+ private String proxyPasswd = null;
+
+ private String proxyHost = null;
+
+ private int proxyPort = -1;
+
+ private boolean hasProxyAuth = false;
+
+ /**
+ * Construct a test client context.
+ */
+ public TestContext() {
+ cookies = new ArrayList<javax.servlet.http.Cookie>();
+ try {
+ baseUrl = new URL("http://localhost:8080");
+ } catch (MalformedURLException e) {
+ // Should not be invalid
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Clear all authorizations (basic, digest, ntlm, proxy).
+ *
+ */
+ public void clearAuthorizations() {
+ hasAuth = false;
+ hasNTLMAuth = false;
+ hasProxyAuth = false;
+ }
+
+ /**
+ * Set basic authentication information for the test context.
+ *
+ * @param user user name
+ * @param passwd password
+ */
+ public void setAuthorization(String user, String passwd) {
+ this.user = user;
+ this.passwd = passwd;
+ hasAuth = true;
+ }
+
+ /**
+ * Set NTLM authentication information for the test context.
+ *
+ * @param user user name
+ * @param passwd password
+ */
+ public void setNTLMAuthorization(String user, String passwd, String
domain) {
+ this.user = user;
+ this.passwd = passwd;
+ this.domain = domain;
+ hasNTLMAuth = true;
+ }
+
+ /**
+ * Set proxy authentication information for the test context.
+ *
+ * @param user user name (null if none)
+ * @param passwd password (null if none)
+ * @param host proxy host name (null if applicable to any host).
+ * @param port proxy port (negative if applicable to any port).
+ */
+ public void setProxyAuthorization(String user, String passwd, String host,
+ int port) {
+ this.proxyUser = user;
+ this.proxyPasswd = passwd;
+ this.proxyHost = host;
+ this.proxyPort = port;
+ hasProxyAuth = !(null == user && null == passwd);
+ }
+
+ /**
+ * Add a cookie to the test context. These cookies are set on the
conversation when you use a {WebTester#beginAt}.
+ *
+ * @param name cookie name.
+ * @param value cookie value.
+ * @param domain cookie domain (ie localhost or www.foo.bar).
+ * @param expiry the expiry date in seconds. -1 will delete this cookie, 0
will delete it at the end of the browser session.
+ */
+ public void addCookie(String name, String value, String domain, int
expiry) {
+ Cookie c = new Cookie(name, value);
+ c.setDomain(domain);
+ c.setMaxAge(expiry);
+ addCookie(c);
+ }
+
+ /**
+ * Add a cookie to the test context. These cookies are set on the
conversation when you use a {WebTester#beginAt}.
+ *
+ * @param name cookie name.
+ * @param value cookie value.
+ * @param domain cookie domain (ie localhost or www.foo.bar).
+ */
+ public void addCookie(String name, String value, String domain) {
+ Cookie c = new Cookie(name, value);
+ c.setDomain(domain);
+ addCookie(c);
+ }
+
+ /**
+ * Add a cookie to the test context. These cookies are set on the
conversation when you use a {WebTester#beginAt}.
+ *
+ * @param cookie a cookie.
+ */
+ public void addCookie(Cookie cookie) {
+ cookies.add(cookie);
+ }
+
+ /**
+ * Return true if a basic authentication has been set on the context via
{...@link #setAuthorization}.
+ */
+ public boolean hasAuthorization() {
+ return hasAuth;
+ }
+
+ /**
+ * Return true if a NTLM authentication has been set on the context via
{...@link #setNTLMAuthorization}.
+ */
+ public boolean hasNTLMAuthorization() {
+ return hasNTLMAuth;
+ }
+
+ /**
+ * Return true if a proxy authentication has been set on the context via
{...@link #setProxyAuthorization}.
+ */
+ public boolean hasProxyAuthorization() {
+ return hasProxyAuth;
+ }
+
+ /**
+ * Return true if one or more cookies have been added to the test context.
+ */
+ public boolean hasCookies() {
+ return cookies.size() > 0;
+ }
+
+ /**
+ * Return the authorized user for the test context.
+ */
+ public String getUser() {
+ return user;
+ }
+
+ /**
+ * Return the user password.
+ */
+ public String getPassword() {
+ return passwd;
+ }
+
+ /**
+ * Return the user domain.
+ */
+ public String getDomain() {
+ return domain;
+ }
+
+ /**
+ * Return the cookies which have been added to the test context.
+ */
+ public List<javax.servlet.http.Cookie> getCookies() {
+ return cookies;
+ }
+
+ public String getUserAgent() {
+ return userAgent;
+ }
+
+ public void setUserAgent(String userAgent) {
+ this.userAgent = userAgent;
+ }
+
+ public boolean hasUserAgent() {
+ return userAgent != null;
+ }
+
+ /**
+ * Return the locale established for the test context. If the locale has
not been explicitly set,
+ * Locale.getDefault() will be returned.
+ */
+ public Locale getLocale() {
+ return locale;
+ }
+
+ /**
+ * Set the locale for the test context.
+ */
+ public void setLocale(Locale locale) {
+ this.locale = locale;
+ }
+
+ /**
+ * Set a resource bundle to use for the test context (will be used to
lookup expected values by key in WebTester).
+ *
+ * @param name path name of the resource bundle.
+ */
+ public void setResourceBundleName(String name) {
+ resourceBundleName = name;
+ }
+
+ /**
+ * Return the test context resource bundle for expected value lookups.
+ */
+ public String getResourceBundleName() {
+ return resourceBundleName;
+ }
+
+ /**
+ * Return the proxy server name
+ */
+ public String getProxyHost() {
+ return proxyHost;
+ }
+
+ /**
+ * Return the proxy server port
+ */
+ public int getProxyPort() {
+ return proxyPort;
+ }
+
+ /**
+ * Return the proxy user name
+ */
+ public String getProxyUser() {
+ return proxyUser;
+ }
+
+ /**
+ * Return the proxy password
+ */
+ public String getProxyPasswd() {
+ return proxyPasswd;
+ }
+
+ /**
+ * Return the base URL for the test context. The default base URL is port
8080 on localhost.
+ */
+ public URL getBaseUrl() {
+ return baseUrl;
+ }
+
+ /**
+ * Set the base url for the test context.
+ *
+ * @param url Base url value - A trailing "/" is appended if not provided.
+ */
+ public void setBaseUrl(String url) {
+ try {
+ baseUrl = new URL(url.endsWith("/") ? url : url + "/");
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Set the base url for the test context.
+ *
+ * @param url Base url value. Anything after trailing "/" will be skipped.
+ */
+ public void setBaseUrl(URL url) {
+ baseUrl = url;
+ }
+
+ /**
+ * Add a custom request header.
+ * @param name header name.
+ * @param value header value.
+ */
+ public void addRequestHeader(final String name, final String value) {
+ requestHeaders.put(name, value);
+ }
+
+ /**
+ * Remove a custom request header.
+ * @param name header name.
+ */
+ public void removeRequestHeader(final String name) {
+ requestHeaders.remove(name);
+ }
+
+ /**
+ * Get custom request headers.
+ * @param name header name.
+ * @param value header value.
+ */
+ public Map<String, String> getRequestHeaders() {
+ return requestHeaders;
+ }
+
+ /**
+ * Clear custom request headers.
+ *
+ */
+ public void clearRequestHeaders() {
+ requestHeaders = new HashMap<String, String>();
+ }
+
+}
Modified:
branches/bug-2791025/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
2008-12-12 20:52:32 UTC (rev 790)
+++
branches/bug-2791025/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitElementImpl.java
2009-06-01 23:02:01 UTC (rev 808)
@@ -11,6 +11,7 @@
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
/**
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development