in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Java Tools for Extreme programming mastering open source tools including - Hightower R.

Hightower R. Java Tools for Extreme programming mastering open source tools including - Wiley publishing , 2002. - 417 p.
ISBN: 0-471-20708
Download (direct link): javatoolsforextremeprog2002.pdf
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 159 >> Next

list.addElement( new WebLink( _baseURL, _baseTarget, child ) );
to find all the Nodes (roughly corresponding to tags, in this case) with the name "a" (as in <a href="blah">) in the underlying HTML.
Using the DOMs provided by HttpUnit can be somewhat difficult, simply because DOM programming can be difficult. The DOM API (the Javadocs can be found at was designed to be language independent and thus doesn't jibe with Java as well as it might.
JDOM, a lightweight API for XML interaction, is available under an Apache-style open source license from Its API is significantly more intuitive and Java-like than DOM, and in a book covering top-quality open source tools, it seemed worth mentioning.
Manipulating the HTML DOM can yield information that would otherwise be unavailable for assertion. As an example, let's return to our earlier test of the sales report page. The sales report tables were labeled with captions, an element not specifically searched for or returned by HttpUnit. Let's say that it becomes essential to validate these captions. The following DOM code could find the value of the <caption> element for inspection:
* @param cell A table cell containing a single nested table.
private String findCaption(TableCell cell) throws Exception{
Node node = cell.getDOM();
Element elem = (Element)node;
NodeList listOfCaptions = elem.getElementsByTagName("caption");
/*presume only 1*/
Node firstCaption = listOfCaptions.item(0);
/*contents are actually contained in a child node of the caption Node*/
Node contents = firstCaption.getFirstChild(); return contents.getNodeValue();
As you can see, DOM code can get a bit involved. But, on the bright side, our sales report test is now more accurate!
Headers and Cookies
HttpUnit's WebConversation class internally stores all the headers that will be sent to the Web server with each request. Naturally, this storage covers cookies, authentication headers, and various other request headers. In order to set a header for transmission to the server with all future requests, use WebConversations's setHeaderField(String fieldName, String fieldValue) method. Shortcut methods exist for commonly used headers, such as authorization headers: setAuthorization(String userName, String password). Setting a header field to null will remove the header from all subsequent requests.
Cookies are handled slightly differently. WebConversation has methods (getCookieNames() and getCookieValue()) that return all of the cookie's names to be sent as well as an individual cookie's value. addCookie(String name, String value) adds a cookie to the list of cookies to send. Unlike with header fields, there is no way to remove an individual cookie from the list.
As for server-defined cookies, the WebResponse class allows inspection of new cookies through two methods: getNewCookieNames() and getNewCookieValue(). For example, the following code would print all the new cookies set in a Web server response:
for(int i =0; i < names.length; i++){
System.out.print(names[i] + " ");
This facility allows verification that the server has tried to set a specific cookie in the response.
HttpUnit handles frames in a straightforward manner. We give an extensive example on working with frames in the description of the WebClient class in Chapter 16, "HttpUnit API Reference," which won't be reproduced here; however, the brief explanation is that the WebClient stores a Hashtable of frames internally. The contents of a given frame can be accessed by name with
WebResponse response = WebClient.getFrameContents("someFrameName")
If a test follows a link from within one frame that updates another frame, the response from the WebConversation will be the contents of the target frame (which will also be accessible through getFrameContents).
HttpUnit can test sites that use Secure Sockets Layer (SSL), but doing so is somewhat involved. The process requires two basic steps: The server must have an SSL certificate installed, and the JVM used by HttpUnit must trust the installed certificate. (Certificates from Verisign or Thawte are automatically trusted.) A number of technical details surround SSL support in Java/HttpUnit, most of which are covered in the SSL FAQ hosted on HttpUnit's SourceForge site (
The HttpUnitOptions class provides a series of static properties that configure the behavior of HttpUnit. HttpUnitOptions provides options that determine whether link searches are case-sensitive, whether to follow
page refresh requests automatically, and whether to print headers to the System.out stream as they are sent and received. Chapter 16, "HttpUnit API Reference," contains more detailed information on each of these options. It is worth noting that because these properties are merely static variables, you need to exercise some care in using them in a multithreaded testing environment (such as running JUnitPerf with HttpUnit) if the options are set to conflicting values in different tests.
Previous << 1 .. 87 88 89 90 91 92 < 93 > 94 95 96 97 98 99 .. 159 >> Next