Java Reference
In-Depth Information
public void end(WebResponse webResponse) throws IOException {
WebClient webClient = new WebClient();
HtmlPage page = HTMLParser.parse(webResponse,
webClient.getCurrentWindow());
WebAssert.assertTitleEquals(page, "Hello World");
webClient.closeAllWindows();
}
We create a
WebClient
only to fulfill the needs of the
HTMLP
arser
API
, which requires
a
WebWindow
, which the
WebClient
holds.
Cactus tip
In Cactus, you don't use
getPage
to get an HtmlPage. Instead, you parse it from
the
WebResponse
with
HTMLParser.parse
.
Once you have an HtmlPage, you're back to using the standard HtmlUnit
API
.
We've finished covering HtmlUnit for this chapter; the
API
is intuitive and straight-
forward, so we invite you to explore the rest on your own. Let's now look at Selenium,
a testing framework that differs from HtmlUnit in a fundamental way: instead of emu-
lating a web browser, Selenium drives a real web browser process.
12.5
Introducing Selenium
Selenium
13
is a free open source tool suite used to test web applications. Sele-
nium's strength lies in its ability to run tests against a real browser on a specific
operating system. This is unlike HtmlUnit, which emulates the browser in the same
VM
as your tests. This strength comes at a cost: the complexity of setting up and
managing the Selenium runtime. Although Selenium provides many components,
we consider the following components: the Selenium Remote Control (
RC
) server,
IDE
, and client driver
API
.
The Selenium
IDE
is a Firefox add-on used to record, play back, and generate tests
in many languages, including Java. The Selenium client driver
API
is what tests call to
drive the application; it communicates to the remote control server, which in turns
drives the web browser.
The client driver connects to the server over
TCP
/
IP
; the server doesn't need to
run in the
JVM
or even on the same physical machine. Selenium recommends
14
run-
ning the server on many different machines, with different operating systems and
browser installations. A test connects to a server by specifying a hostname and port
number to the
DefaultSelenium
class.
Figure 12.2 shows the main Selenium
IDE
(1.0 Beta 2) window. The
IDE
generates
code against the client driver
API
but doesn't support change management. You
13
Selenium site:
http://seleniumhq.org/
14
Selenium Server setup:
http://seleniumhq.org/documentation/remote-control/languages/java.html