First of all, although the name suggests otherwise, HttpUnit is NOT a unit-testing framework.
What is it then? Well, read this excerpt from the HttpUnit site:
“HttpUnit emulates the relevant portions of browser behavior, including form submission, JavaScript, basic http authentication, cookies and automatic page redirection, and allows Java test code to examine returned pages either as text, an XML DOM, or containers of forms, tables, and links. When combined with a framework such as JUnit, it is fairly easy to write tests that very quickly verify the functioning of a web site.”
It looks a bit like a thin layer around Commons HttpClient (i don’t believe it is though), emulating the user-experience of a site like viewing pages, clicking links and buttons, and filling in forms, all the while keeping himself strictly to the request-response paradigma.
It’s a lot like recording mousegestures like for example some loadtestingtools, only this is the programmer’s version, line by line.
Even though in itself HttpUnit is not a testing-tool, the combined package of HttpUnit and JUnit can very well be used for “black-box” testing of web sites.
Enough theory, here are some examples.
Example 1, just printing the html
// Create a 'session' WebConversation wc = new WebConversation(); // Obtain the main page on the web site WebRequest request = new GetMethodWebRequest("http://www.cnn.com"); WebResponse response = wc.getResponse( request ); System.out.println(response.getText());
By adding a line like this the first online ‘test’ could be created.
System.out.println("It really is CNN: " + (response.getText().indexOf("Cable News Network") != -1));
Example 2, finding an expected login-form in the response, filling it’s fields, and submitting it.
WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest(URL); WebResponse response = wc.getResponse(request); // assuming we've got the first form on the page WebForm loginForm = response.getForms()[0]; // creating the upcoming request and setting parameters on it request = loginForm.getRequest(); request.setParameter("userId", "someUser"); request.setParameter("password", "secret567"); // getting it's response, and testing if the word "Welcome" does appear somewhere on the page response = wc.getResponse(request); System.out.println("Correct login: " + (response.getText().indexOf("Welcome") != -1));
As you can see, the response object has methods with which the text, links, buttons, forms etc. on the page can be found and manipulated.
Example 3, combining HttpUnit with JUnit
import junit.framework.TestCase; import com.meterware.httpunit.*; public class LoginTest extends TestCase { public static final String URL = "http://someURL/login.do"; public static void main(String[] args) { junit.textui.TestRunner.run(LoginTest.class); } public LoginTest(String name) { super(name); } public static void testBadLogin() throws Exception { WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest(URL); WebResponse response = wc.getResponse(request); WebForm loginForm = response.getForms()[0]; request = loginForm.getRequest(); response = wc.getResponse(request); assertTrue("Login not rejected while logging in without parameters", response.getText().indexOf("Invalid Username or Password") != -1); } public static void testGoodLogin() throws Exception { WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest(URL); WebResponse response = wc.getResponse(request); WebForm loginForm = response.getForms()[0]; request = loginForm.getRequest(); request.setParameter("userId", "someUser"); request.setParameter("password", "secret567"); response = wc.getResponse(request); assertTrue("Login rejected while logging in with 'someUser/secret567'", response.getText().indexOf("Welcome") != -1); } }
See the API for the com.meterware.httpunit.WebResponse class for extended options like accessing tables and their cells, retrieving the DOM, (limited) javascript-support, etc.
Surprisingly, since the API is so simple and intuitive, HttpUnit could also be useful for rapid development of spider/agent-like applications or a crude (no out-of-the-box reports) stresstest-tool.
In the next part, we’ll have a look at the other part of the package, ServletUnit.
No Responses