Tips on HttpUnit
- import package.
import com.meterware.httpunit.*;
import junit.framework.*; - Unit test source code of httpunit is primary resource to study how to use it.
httpunit-1.6.2/test/com/meterware/httpunit - Code fragment which shows how to log in.
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest( "http://localhost/portal" );
WebResponse response = conversation.getResponse( request );
WebForm loginForm = response.getForms()[0];
request = loginForm.getRequest();
request.setParameter( "user", "user" );
request.setParameter( "pwd", "password123" );
response = conversation.getResponse( request );
assertEquals( "login url", "http://localhost/portal/servlet/login", response.getURL().toString() );- Code fragment which shows how to disable javascript. Some web application does do some bad html which httpunit can't parse correctly.
WebConversation conversation = new WebConversation();
//
// Disabling JavaScript validation
//
HttpUnitOptions.setScriptingEnabled(false);
WebRequest request = new GetMethodWebRequest( "http://localhost" );
WebResponse response = conversation.getResponse( request ); - Code fragment which shows how to get frame.
String[] frames = response.getFrameNames();
for ( int i = 0; i < frames.length; i++ )
{
System.out.println( "frame[" + i + "]:" + frames[i] );
}
WebResponse frameResponse = response.getSubframeContents( "topFrame" );
// WebResponse frameResponse = response.getSubframeContents( "leftFrame" );
System.out.println( "mainFrame:" + frameResponse.getURL().toString() );
WebLink[] links = frameResponse.getLinks();
for ( int i = 0; i < links.length; i++ )
{
System.out.println( "link:" + links[i].getText() + ", url:" +links[i].getURLString() );
}
WebResponse menuResponse = getFrameWithURL( conversation, "topmenu" );
assertNotNull( "topMenu is null", menuResponse );
links = frameResponse.getLinks();
for ( int i = 0; i < links.length; i++ )
{
System.out.println( "topmenu link:" + links[i].getText() + ", url:" +links[i].getURLString() );
}
NOTE: the following methods are from httpunit's own unit test.
private String getNameOfFrameWithURL( WebConversation wc, String urlString ) {
String[] names = wc.getFrameNames();
for (int i = 0; i < names.length; i++) {
WebResponse candidate = wc.getFrameContents( names[i] );
System.out.println( "getNameOfFrameWithURL() frame url:" + candidate.getURL().toExternalForm() );
if (candidate.getURL().toExternalForm().indexOf( urlString ) >= 0) {
return names[i];
}
}
return null;
}
private WebResponse getFrameWithURL( WebConversation wc, String urlString ) {
String name = getNameOfFrameWithURL( wc, urlString );
System.out.println( "getFrameWithURL:" + name );
if (name == null) return null;
return wc.getFrameContents( name );
}- Httpunit can't parse iframe's id attribute but name attribute is accepted.
- Httpunit can't parse iframe's id attribute but name attribute is accepted.
- Code fragment which shows how to disable javascript. Some web application does do some bad html which httpunit can't parse correctly.