Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HttpUnit Tutorial

Reply
Thread Tools

HttpUnit Tutorial

 
 
Phlip
Guest
Posts: n/a
 
      04-21-2004
Java Junkies:

Below my sig is a tutorial using HttpUnit to test a Web site.

Many programmers' sanity depends on reading Dilbert every day.
However, we are programmers. We should not need to put up with all the
popup adds and such around the payload.

This HttpUnit snippet reads the HTML page - without reading all its
extras. Then it locates the actual Dilbert cartoon, downloads it to
your c:/temp folder, and presents it in Internet Explorer.

The presentation system is easy to reconfigure for other browsers.

Note the // reveal(page) statement. If you de-comment it, you will see
the Dilbert page, without all its GIFs.

When using HttpUnit to build a Web site, you can easily configure
reveal() to serve a page with the GIFs available. This permits your
tests to temporarily show the page under test, giving you more rapid
feedback than bouncing a server would.

But in production, you would want to comment out calls to reveal() or
explore() before integrating!

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces


import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.regex.Pattern;
import junit.framework.TestCase;
import org.xml.sax.SAXException;
import com.meterware.httpunit.*;

public class filchDilbert
extends TestCase
{
protected void setUp() {
HttpUnitOptions.setScriptingEnabled(false);
// HttpUnitOptions.setMatchesIgnoreCase(true);
// HttpUnitOptions.setImagesTreatedAsAltText(true);
// HttpUnitOptions.setParserWarningsEnabled(true);
}

private void assertMatch(String regex, String input) throws Exception
{
boolean shouldMatch = Pattern.matches(regex, input);
assertTrue("<" + regex + "> did not match <" + input + ">",
shouldMatch);
}

public void reveal(WebResponse page)
throws FileNotFoundException,
IOException
{
// TODO put this one in TFUI

String location = "c:/temp/test.html";
FileWriter out = new FileWriter(location);
String htmlContents = page.getText();
out.write(htmlContents);
out.close();
explore(location);
System.out.println(htmlContents);
}

private void explore(String location) throws IOException
{
Runtime.getRuntime().exec("\"C:\\Program Files\\Internet
Explorer\\iexplore.exe\" "+ location);
}
private void saveBinary(File fileOut, String dilbertsGif) throws
FileNotFoundException, IOException
{
FileOutputStream fos = new FileOutputStream(fileOut);
DataOutputStream stream = new DataOutputStream(fos);
stream.writeBytes(dilbertsGif);
stream.close();
fos.close();
}

public void test_hitFrontPage() throws MalformedURLException,
IOException, SAXException, Exception
{
WebConversation conversation = new WebConversation();
String server = "http://www.dilbert.com/";

WebRequest request = new GetMethodWebRequest( server );
WebResponse page = conversation.getResponse( request );
assertMatch("Dilbert.*", page.getTitle());

// reveal(page);

// find: <IMG SRC="/comics/dilbert/archive/images/dilbert2091507040420.gif"
BORDER=0 ALT="Today's Dilbert Comic">

WebImage todaysDilbert = page.getImageWithAltText("Today's Dilbert
Comic");
System.out.println(todaysDilbert.getSource());
request = todaysDilbert.getRequest();
WebResponse response = conversation.getResponse(request);
File fileOut = new File("C:/temp/dilbert.gif" );
saveBinary(fileOut, response.getText());
explore(fileOut.getAbsolutePath());
}


}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HttpUnit - button.click() related question Jonathan Taub Java 1 02-13-2004 01:03 PM
Httpunit help? Maintaining sessions/cookies. John Laury Java 0 11-18-2003 05:54 PM
HttpUnit JWebUnit for WAP apps DNasmars Java 0 08-26-2003 04:46 PM
Re: HttpUnit throws java.lang.NoSuchMethodError as soon as I attempt to create a WebConversation. Neomorph Java 0 08-14-2003 10:12 AM
httpUnit Tom Koenning Java 1 07-24-2003 08:28 PM



Advertisments