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

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 .. 86 87 88 89 90 91 < 92 > 93 94 95 96 97 98 .. 159 >> Next

*/
255
String cellStr = cell.asText(); assertTrue(cellStr.indexOf("Board Members ") > -1);
}
The Feedback Form
At this point, the sales report page is all but verified. Verifying that the board member list exists involves only another substring search through a text rendering of a cell. All that remains in this TestCase is to check on the feedback form at the bottom of the page (Figure 9.2).
BEE
¹ Fit view Fivoritm loch Help
J^5e*(h ' _?HtlCry
AjJdfHS |g] h.;p:/nocjJFro5t:i0?jJjcwnpt7ijpiiva[c ”1 ^Go Laik« ”
3
We'd like your feedback!
Your name. Your tmad address
[AnpnymoMS
Did you 6n<i ihi; intranet StnJ a response 1» my
pTHelptui'- 3 feedback <na email. P
Teltusl |
?]Oone | [ ft Local nfranut
Figure 9.2: The Feedback Form that Appears Below Every Page.
To complete our functional test of this component of the Web application, we have to check that the form at the bottom of the page is structured correctly and that it will accept average user input. The page designers have named this form so that it can be retrieved with this simple code:
WebForm feedbackForm = response.getFormWithName("feedback");
WebForm (naturally) represents an HTML form. If we examine the page's HTML, we can see that this form is laid out internally using a table—this layout should not affect anything we are doing, because WebForm models the form itself, not the exact HTML on which it is based. The first check we can run is that the form contains all of the possible parameters we expect. First we retrieve the possible parameters with:
String[] parameters = feedbackForm.getParameterNames();
Then we can check these versus our expectations with a custom assert function:
String[] expected = {"name", "email", "reply", "usefulness"}; assertArraysEqual(expected, parameters);
Now we can verify that the "usefulness" drop-down box contains the correct parameters using a similar technique:
String[] usefulnessValues = feedbackForm.getOptionValues("usefulness");
String[] expectedValues = {"1","2","3"}; assertArraysEqual(expectedValues, usefulnessValues);
We can also assert that a given parameter has a default value:
assertEquals("2", feedbackForm.getParameterValue("usefulness"));
Once we have checked on the state of the form in the received page, we can fill it out and submit it. We have already covered the basics of form submission with the login page; only a couple of wrinkles remain undiscussed. First, all form parameters are set with request.setParameter(). Parameters that accept more than one value (multiple select boxes, for instance) can be set with request.setParameter(String name, String[] values). By default, parameters are checked against the acceptable values in the underlying form. Attempting to set the "usefulness" parameter to "600" would yield an IllegalRequestParameterException. Checkboxes and other request parameters can be removed from the request with request.removeParameter(String name).
256
Note that HttpUnit does not check whether you are removing a parameter that could not be removed from the browser. The statement
feedbackRequest.removeParameter("usefulness");
would be just fine by HttpUnit. Once all the request parameters are set, the request can be submitted in the usual manner.
Advanced Topics in HttpUnit
If you have gotten this far in the chapter, you already know enough about HttpUnit to begin developing. With the source code and the reference chapter at your side, you should be able to write some mean Web testing code. The rest of this chapter will be devoted to giving an overview of the remaining issues in the HttpUnit framework and developing a more sophisticated testing program that leverages the power of HttpUnit's Java base. This section covers topics in HttpUnit such as DOM inspection, headers and validation, and HttpUnit's configurable options.
DOM Inspection
HttpUnit provides the ability to inspect an HTML document or part of an HTML document as a DOM. The Document Object Model (DOM) is a standard developed by the World Wide Web Consortium (www.w3.org/DOM/) that treats documents as objects that can be manipulated. DOM was developed to allow programmatic access to data in languages such as XML and HTML. A full discussion of DOM lies outside the scope of this book; if you need a primer, every-Java journal with online archives is sure to have several articles on XML and its two common program interfaces, DOM and SAX (Simple API for XML). What you need to know about DOM and HttpUnit is that HttpUnit uses JTidy—an HTML parser—to turn a server response into an in-memory DOM whose contents can be accessed at random. Almost all of HttpUnit's powerful assertion capabilities (WebResponse.getTables(), for instance) rely on DOM manipulation under the hood. For instance, WebResponse.getLinks() uses
NodeList nl = NodeUtils.getElementsByTagName( _rootNode, "a" );
Vector list = new Vector();
for (int i = 0; i < nl.getLength(); i++) {
Node child = nl.item(i);
if (isLinkAnchor( child )) {
Previous << 1 .. 86 87 88 89 90 91 < 92 > 93 94 95 96 97 98 .. 159 >> Next