Back

HTTPUnit

HTTPUnit is a very useful extension to JUnit. It allows parsing and checking web pages. The main site is httpunit.sourceforge.net. Alas the documentatiin is a bit spartan and the cookbook has only a toy implementation of a form parse. This page shows a more realistic example.

The HTTP test form page contains both the cookbook form example as two variants which both check the second table. The first one test3, uses id attributes to find a FormControl. The second one test4, uses cell coordinates to find a HTMLElement. In this case I couldn't find the <SELECT... <OPTION...> ...> construct.

package cookbook;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;

import org.junit.Test;
import org.xml.sax.SAXException;

import com.meterware.httpunit.FormControl;
import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.meterware.httpunit.controls.SelectionFormControl;

public class CookBook {

	@Test
	public void test1() throws IOException, SAXException {
	    WebConversation wc = new WebConversation();
	    WebResponse   resp = wc.getResponse( "http://electrickery.xs4all.nl/tmp/HTTPTestForm.html" );

	    WebForm form = resp.getForms()[0];      // select the first form in the page
	    assertNotNull("form not found", form);
	    assertEquals( "La Cerentolla", form.getParameterValue( "Name" ) );
	    assertEquals( "Chinese",       form.getParameterValue( "Food" ) );
	    assertEquals( "Manayunk",      form.getParameterValue( "Location" ) );
	    assertEquals( "on",            form.getParameterValue( "CreditCard" ) );
	}
	
	
	/**
	 * Identify elements by id only (FormControl interface)
	 * @throws IOException
	 * @throws SAXException
	 */
	@Test
	public void test3() throws IOException, SAXException {
	    WebConversation wc = new WebConversation();
	    WebResponse   resp = wc.getResponse( "http://electrickery.xs4all.nl/tmp/HTTPTestForm.html" );
	    
	    WebForm nnForm = resp.getFormWithID("nonameForm");
	    assertNotNull("form not found", nnForm);
	    
	    FormControl txtField11 = (FormControl)nnForm.getControlWithID("RName");
	    assertNotNull("Null FormControl", txtField11);
	    assertEquals("Cell contents mismatch", "La Cerentolla",    txtField11.getAttribute("value"));

	    FormControl radioButton21 = (FormControl)nnForm.getControlWithID("RType");
	    assertNotNull("Null FormControl", radioButton21);
	    assertEquals("Cell contents mismatch", "Chinese", radioButton21.getAttribute("value"));

	    SelectionFormControl selectOption31 = (SelectionFormControl)nnForm.getControlWithID("RLocation");
	    assertNotNull("Null FormControl", selectOption31);
	    assertEquals("Selected option count not 1", 1, selectOption31.getValues().length);
	    assertEquals("Cell contents mismatch", "Manayunk",    selectOption31.getValues()[0]);

	    FormControl checkBox41 = (FormControl)nnForm.getControlWithID("RPayCC");
	    assertNotNull("Null FormControl", checkBox41);
	    assertEquals("Cell contents mismatch", "checked",    checkBox41.getAttribute("checked"));

	}
	
	/**
	 * Identify elements by cell coordinates only (assuming only one element per cell)
	 *  (HTMLElement interface)
	 * @throws IOException
	 * @throws SAXException
	 */
	@Test
	public void test4() throws IOException, SAXException {
	    WebConversation wc = new WebConversation();
	    WebResponse   resp = wc.getResponse( "http://electrickery.xs4all.nl/tmp/HTTPTestForm.html" );
	    
	    WebForm nnForm = resp.getFormWithID("nonameForm");
	    assertNotNull("form not found", nnForm);

	    WebTable nnTable = resp.getTableWithID("nonameTable");
	    assertNotNull("table not found", nnTable);

	    TableCell tc11 = nnTable.getTableCell(1, 1);
	    assertEquals("Element count not 1", 1, tc11.getElementNames().length);
	    assertEquals("Cell contents mismatch", "La Cerentolla",    
	    		tc11.getElementsWithName(tc11.getElementNames()[0])[0].getAttribute("value"));
	    
	    TableCell tc21 = nnTable.getTableCell(2, 1);
	    assertEquals("Element count not 1", 1, tc21.getElementNames().length);
	    assertEquals("Cell contents mismatch", "Chinese", 
	    		tc21.getElementsWithName(tc21.getElementNames()[0])[0].getAttribute("value"));
	    
//	    TableCell tc31 = nnTable.getTableCell(3, 1);
//	    assertEquals("Element count not 1", 1, tc31.getElementNames().length);
//	    for (String elemNames : tc31.getElementNames()) {
//	    	System.out.println("*  " + elemNames);
//	    }
	    
	    TableCell tc41 = nnTable.getTableCell(4, 1);
	    assertEquals("Element count not 1", 1, tc41.getElementNames().length);
	    assertEquals("Cell contents mismatch", "checked", 
	    		tc41.getElementsWithName(tc41.getElementNames()[0])[0].getAttribute("checked"));

	    
	}
}

Last updated: 2008-11-30

email