HttpUnit Tutorial

Discussion in 'Java' started by Phlip, Apr 21, 2004.

  1. Phlip

    Phlip Guest

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


    }
     
    Phlip, Apr 21, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Koenning

    httpUnit

    Tom Koenning, Jul 22, 2003, in forum: Java
    Replies:
    1
    Views:
    4,431
    Tom Koenning
    Jul 24, 2003
  2. Neomorph
    Replies:
    0
    Views:
    1,108
    Neomorph
    Aug 14, 2003
  3. DNasmars

    HttpUnit JWebUnit for WAP apps

    DNasmars, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    500
    DNasmars
    Aug 26, 2003
  4. John Laury
    Replies:
    0
    Views:
    797
    John Laury
    Nov 18, 2003
  5. Jonathan Taub
    Replies:
    1
    Views:
    1,940
    Russell Gold
    Feb 13, 2004
Loading...

Share This Page