Java Reference
In-Depth Information
public void
testSearch() {
// Same as before...
}
@Test
public void
testSearchOnErrSaveScreen()
throws
Throwable {
try
{
this
.testSearch();
}
catch
(Throwable t) {
this
.captureScreenshot(t);
}
}
We've added a new method called
captureScreenshot
b
, which takes a
Throwable
argument and calls the Selenium
captureScreenshot
method
C
. We refactored our
test method by creating a new method
testSearchOnErrSaveScreen
D
, removing
@Test
from
testSearch
, and adding it to the new method instead.
To avoid repeating this code pattern in every method that wants to capture a screen
shot on failure requires extending
JU
nit, which is beyond the scope of this section.
This concludes our Selenium survey; next, we contrast and compare HtmlUnit and
Selenium before presenting our chapter summary.
D
12.9
HtmlUnit versus Selenium
Here's a recap of the similarities and differences you'll find between HtmlUnit
and Selenium.
The similarities are that both are free and open source and both require Java 5 as
the minimum platform requirement.
The major difference between the two is that HtmlUnit emulates a specific web
browser, whereas Selenium drives a real web browser process. When using Sele-
nium, the browser itself provides support for JavaScript. In HtmlUnit 2.7, Mozilla's
Rhino
18
1.7 Release 2 engine provides JavaScript support, and specific browser
behavior is emulated.
Use HtmlUnit when
Use HtmlUnit when your application is independent of operating system features
and browser-specific implementations not accounted for by HtmlUnit, like Java-
Script, DOM, SCC, and so on.
Use Selenium when
Use Selenium when you require validation of specific browsers and operating sys-
tems, especially if the application takes advantage of or depends on a browser's
specific implementation of JavaScript, DOM, CSS, and the like.
18
Mozilla Rhino:
http://www.mozilla.org/rhino/