Java Reference
In-Depth Information
elements like fonts, colors, and layout cause an application to be difficult or impossi-
ble to use.
What we can test is the content of web pages to any level of detail (we could
include spelling), the application structure or navigation (following links to their
expected destination, for example), and the ability to verify user stories with accep-
tance tests. 1 We can also verify that the site works with required browsers and operat-
ing systems.
12.1
Choosing a testing framework
We look at two free open source tools to implement presentation-layer tests within
JU nit: HtmlUnit and Selenium.
HtmlUnit is a 100 percent Java headless browser framework that runs in the same
virtual machine as your tests. Use HtmlUnit when your application is independent of
operating system features and browser-specific implementations not accounted for by
HtmlUnit, like JavaScript, DOM, CSS, and so on.
Selenium drives various web browsers programmatically and checks the results
from JU nit. Selenium also provides a simple IDE to record and play back tests and can
generate test code. Use Selenium when you require validation of specific browsers and
operating systems, especially if the application takes advantage of or depends on a
browser's specific implementation of JavaScript, DOM , CSS , and the like. Let's start
with HtmlUnit.
12.2
Introducing HtmlUnit
HtmlUnit is an open source Java headless browser framework. It allows tests to imitate
programmatically the user of a browser-based web application. HtmlUnit tests don't
display a user interface. The framework lets you test all aspects of a web application.
We describe here the most common tasks; for the rest, you'll find the API quite intui-
tive and easy to use.
In the remainder of this HtmlUnit section when we talk about “testing with a
web browser,” it's with the understanding that we're testing by emulating a spe-
cific web browser.
To install HtmlUnit, see appendix E, “Installing software.”
12.2.1
A live example
Let's jump in with the example in listing 12.1. You can test now, assuming you can con-
nect to the internet. The test will go to the HtmlUnit website, navigate the Javadoc,
and make sure a class has the proper documentation.
1
Extreme programming acceptance tests: http://www.extremeprogramming.org/rules/functionaltests.html
 
 
 
 
 
 
 
Search WWH ::




Custom Search