Java Reference
In-Depth Information
test simulates a user and checks the state of visited web pages. You can choose to have
the tests emulate a web browser (HtmlUnit) or drive a live web browser (Selenium.)
As always with browser-emulation software, the key is how well it supports JavaScript.
13.2.2
Client-side script unit testing
Here we divide the scripts into two piles: the ones that use
XHR
to manipulate a
DOM
and the ones that don't.
Although we can test some script functions and libraries independently from their
hosting page where scripts call
XHR
objects and modify the
DOM
of the current page,
we need a JavaScript engine and browser; the browser in turn may be emulated or live.
For these scripts, we should use a functional test.
For other scripts, we're testing a library of functions, and we prefer to deliver these
functions in standalone files as opposed to embedded in
HTML
pages.
We can test all scripts through functional tests of the pages that call on them, but
we want to provide a lighter-weight test pass that's more along the line of a true unit
test. Heavier-weight and slower functional tests using web browsers should ideally be
reserved for when scripts can't be otherwise verified.
Let's look at JavaScript unit testing. What should you look for in a JavaScript test-
ing framework? From the
TDD
point of view, the most important feature is the ability
to automate tests. We must be able to integrate JavaScript testing in our Ant or Maven
build. Second, we want the ability to run the tests from
JU
nit. You'll find many
JavaScript frameworks that provide for creating and running tests by embedding test
invocation in an
HTML
page (JsUnit,
5
JsUnitTest,
6
script.aculo.us
7
), whereas others do
it all from JavaScript (RhinoUnit).
8
The best integration for our purposes must
include support for
JU
nit. Although
JU
nit or Ant integration can be custom coded for
a specific JavaScript testing framework, JsUnit provides this functionality out of the
box. JsUnit is a free, open source framework integrated with
JU
nit that goes one step
further by providing advanced Selenium-type distributed configuration options.
JsUnit is to JavaScript testing what Selenium is to web application testing. JsUnit
allows you to test JavaScript, from
JU
nit, by controlling a web browser process on a
local or remote machine. We discuss JsUnit in action in the section “JavaScript testing
with JsUnit.”
13.2.3
Service testing
Service testing verifies services provided by the server and accessed from JavaScript
XHR
objects. The
HTML
page and JavaScript aren't tested. Because
HTTP
is the stan-
dard used by
XHR
objects to communicate with the server, we can use any
HTTP
client
to test the service independently of
XHR
and the browser. Free, open source Java
5
6
7
8