Java Reference
In-Depth Information
Listing 12.10
Custom alert handler
client.setAlertHandler( new AlertHandler() {
public void handleAlert( final Page page, final String message) {
fail("JavaScript alert: " + message);
}
});
You can apply the same principles to test JavaScript confirm calls by installing a con-
firm handler in the web client with setConfirmHandler .
12.3.15 Testing CSS
You can toggle CSS support on and off in a web client by calling setCssEnabled . By
default, HtmlUnit enables CSS support.
When calling API s, the standard HtmlUnit behavior is to throw an exception when
encountering a problem. In contrast, when HtmlUnit detects a CSS problem, it
doesn't throw an exception; instead, it reports problems to the log through the
Apache Commons Logging 11 library. You can customize this behavior in a WebClient
with an org.w3c.css.sac.ErrorHandler . Two ErrorHandler implementations are
provided with HtmlUnit:
DefaultCssErrorHandler is the default handler and logs all CSS problems.
SilentCssErrorHandler ignores all CSS problems.
To install an error handler, use the setCssErrorHandler method on a web client. For
example, the following causes all CSS problems to be ignored:
webClient.setCssErrorHandler( new SilentCssErrorHandler());
If you want any CSS problem to cause test failures, create an error handler that always
rethrows the CSSException it's given.
12.3.16 SSL errors
You'll find that many websites have expired or incorrectly configured SSL certifi-
cates. By default, the Java runtime throws exceptions if it detects errors. If this gets
in the way of your testing, you can call WebClient.setUseInsecureSSL(true) to
allow the test to proceed. Using this API causes HtmlUnit to use an insecure SSL han-
dler, which trusts everyone.
Now, that we've covered testing from the client point of view, let's go to the
server side and examine how HtmlUnit can be used for in-container testing with
the Cactus framework.
11
Apache Commons Logging: http://commons.apache.org/logging/
 
 
 
 
 
 
 
Search WWH ::




Custom Search