Http Unit Tutorial

Many programmers' sanity depends on reading DilBert every day. However, we are programmers. We should not need to put up with all the popup ads 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

    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 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 = input.matches(regex); assertTrue("<" + regex + "> did not match <" + input + ">", shouldMatch); }

public void reveal(WebResponse? page) throws FileNotFoundException?, IOException {

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

}

CategoryTesting, CategoryWebScraping


EditText of this page (last edited February 11, 2010) or FindPage with title or text search