Laboratorio di Sistemi Software - A.A. 2003/2004
Introduction
HttpUnit, available from http://www.httpunit.org, is an open source Java library for programmatically interacting with HTTP servers.
HttpUnit provides an API for parsing HTML, submitting forms, following hyperlinks, setting cookies, and performing many other tasks normally associated with web browsers.
Installing HttpUnit
Add httpunit.jar and Tidy.jar, both included with HttpUnit, to your classpath. Also ensure that an XML parser is installed.
httpunit.jar contains the class files for the HttpUnit application Tidy.jar is an open source tool for checking the syntax of
Laboratorio di Sistemi Software - A.A. 2003/2004
“Agenda”
The recipes are presented in order as portions of a simple web application developed using a test-first approach.
If the web application were complete, it would allow users to subscribe and unsubscribe from a newsletter. It would also allow administrators to view the complete list of newsletter subscribers, but only after providing a username and
Preparing for Test-First Development
Problem:
You want to configure your development environment to support test-first development with HttpUnit, JUnit, Tomcat, and Ant.
Solution:
Create an Ant buildfile to automatically build and deploy your web application. The buildfile allows you to quickly redeploy and test after each change to your code.
Laboratorio di Sistemi Software - A.A. 2003/2004
Configuring Tomcat
Configuring Ant
In order to test using Ant's junit task, you should copy
servlet.jar, httpunit.jar, junit.jar, and Tidy.jar to Ant's lib
directory. This makes it easy to ensure that all of the required JAR files are loaded using the same Java ClassLoader when you are running your tests.
Laboratorio di Sistemi Software - A.A. 2003/2004
Classpath e compile targets
Define a classpath:
Web Application aRchive target
index.html … \--- WEB-INF | web.xml |--- classes |--- lib index.html … \--- WEB-INF | web.xml |--- classes |--- libLaboratorio di Sistemi Software - A.A. 2003/2004
Tomcat deploy
Use Tomcat’s manager application:
http://localhost:8080/manager/install?path=/news&war=jar:file:/path/to/news.war!/ Or create a deploy target:
Tomcat undeploy
Use Tomcat’s manager application: http://localhost:8080/manager/remove?path=/news Or create an undeploy target:
Laboratorio di Sistemi Software - A.A. 2003/2004
JUnit target
Laboratorio di Sistemi Software - A.A. 2003/2004
Following Hyperlinks (2)
Run the faulty test first, then write the servlet to generate a minimal subscribtion page (TDD
Laboratorio di Sistemi Software - A.A. 2003/2004
Writing testable HTML
Problem:
You want to use HttpUnit to test your web application, but it can't parse your HTML.
Solution:
Write well-formed HTML, ensuring the tags are properly nested and attributes are quoted. For best results, write XHTML.
If you are encountering problems, call this method to turn on HTML Tidy warnings:
This causes HTML Tidy to print warning messages to the console, letting you know which lines of your HTML pages are incorrect.
Laboratorio di Sistemi Software - A.A. 2003/2004
Testing HTML tables (2)
Testing HTML tables (3)
Laboratorio di Sistemi Software - A.A. 2003/2004
Testing HTML tables (4)
Extending the servlet behavior
Laboratorio di Sistemi Software - A.A. 2003/2004
Laboratorio di Sistemi Software - A.A. 2003/2004
Refactoring the Servlet
Laboratorio di Sistemi Software - A.A. 2003/2004
Testing Elements on HTML Forms (1)
Check existence of buttons:
Testing Elements on HTML Forms (2)
Check existence of input fields:
Laboratorio di Sistemi Software - A.A. 2003/2004
Newsletter Subscription page
Note:
the HttpUnit tests are not verifying every aspect of page layout.
Laboratorio di Sistemi Software - A.A. 2003/2004
Laboratorio di Sistemi Software - A.A. 2003/2004
Laboratorio di Sistemi Software - A.A. 2003/2004
Testing through a firewall
Set the proxySet, proxyHost, and proxyPort system properties.
¾ HttpUnit uses java.net.HttpURLConnection, which checks the proxySet system property to determine if it should operate through a proxy server.
Testing cookies
Cookies are little pieces of information that web applications store on the client browser's machine.
¾ Cookies allow web sites to maintain state information as you view different web pages.
Use WebConversation's addCookie( ) method to create new cookies, and its getCookieValue( ) method to retrieve cookie values.
Laboratorio di Sistemi Software - A.A. 2003/2004
Laboratorio di Sistemi Software - A.A. 2003/2004
Testing secure pages
You want to test a page that requires a username and password for login.
Simulate HTTP BASIC authentication using WebConversation's setAuthorization( ) method.
Bibliography
Eric M. Burke & Brian M. Coyner
“Java Extreme Programming Cookbook” O’Reilly