HttpUnit Tutorial

P

Phlip

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?TestFirstUserInterfaces


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());
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top