Skip navigation links

Package com.meterware.httpunit

Classes for testing http server systems.

See: Description

Package com.meterware.httpunit Description

Classes for testing http server systems. Each test session should begin by creating a WebConversation to which it should submit an initial http request using the getResponse method. With each subsequent step, it will typically examine the response either textually or as a DOM, and create new requests based on either submitting a form or clicking on a link.

Installation

The package depends on a number of external jar files, provided in the jar directory:
nekohtml.jar
The NekoHTML parser, used to convert raw HTML into an XML DOM. This is required for handling HTML.
js.jar
The Rhino JavaScript interpreter, required for any JavaScript processing.
xmlParserAPIs.jar
The interfaces for a W3-compliant XML parser. Required for interpreting either HTML or XML pages.
xercesImpl.jar
The Xerces 2 implementation of an XML parser. NekoHTML requires this implementation.
servlet.jar
The APIs and common classes for the Java Servlet 1.3 standard. Required for use with ServletUnit.
junit.jar
JUnit, the unit test framework. Used to test HttpUnit and recommended for writing tests that use HttpUnit.
tidy.jar
JTidy, an alternate HTML parser/validator. JTidy is a lot pickier about HTML structure than NekoHTML, and uses its own implementation of the DOM classes, rather than using those found in the xerces jar. Some JavaScript features, such as document.write() will only work with NekoHTML.

Example

In the following code, a web conversation is started and an initial request sent. The program then prints out the response and extracts the first form (the login form) from it. After setting the name parameter to the desired value, it submits the form and prints the response.
import com.meterware.httpunit.*;

import java.io.IOException;
import java.net.MalformedURLException;

import org.xml.sax.*;

public class Example {


    public static void main( String[] params ) {
        try {
            WebConversation     conversation = new WebConversation();
            
            WebResponse response = conversation.getResponse( "http://www.meterware.com/servlet/TopSecret" );
            System.out.println( response );

            WebForm loginForm = response.getForms()[0];

            loginForm.setParameter( "name", "master" );
            response = loginForm.submit();
            System.out.println( response );

        } catch (Exception e) {
            System.err.println( "Exception: " + e );
        }
    }
}
Please direct any questions to Russell Gold.
Skip navigation links
Copyright © 2000-2008 Russell Gold. See license agreement for rights granted.