Java Reference
In-Depth Information
the particular state of a failure. For example, the exception
ElementNotFound-
Exception
contains specific information as to what exactly caused the failure: the
name of the element, the name of attribute, and the attribute value.
Although not explicitly documented in the
WebAssert
Javadoc,
WebAssert
meth-
ods will throw exceptions for unexpected conditions. Many
WebAssert
methods throw
ElementNotFoundException
.
J
AVA
S
CRIPT
AND
S
CRIPT
E
XCEPTION
By default, all JavaScript errors will throw a
ScriptException
and cause your unit test
to fail. This may not be acceptable, especially if you're testing integration with third-
party sites or if the exception is due to a shortcoming in the Mozilla JavaScript library
or in HtmlUnit itself. You can avoid aborting your unit test on a JavaScript error by
calling
setThrowExceptionOnScriptError
on a web client:
webClient.setThrowExceptionOnScriptError(false);
L
OGGING
You'll notice that HtmlUnit logs warnings on the console whenever it encounters
problems. To disable these messages, you need to tell the logger to skip warnings and
report only severe problems. The following example sets all HtmlUnit loggers to the
severe level:
@BeforeClass
public static void setUpLogging() {
Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.SEVERE);
}
HtmlUnit uses Apache Commons Logging to do its logging, which in turns uses the
JRE
logging facility by default. Our example reconfigures a
JRE
Logger
instance
directly. Apache Commons Logging doesn't allow you to reconfigure logs generically;
you must do so with the actual log implementation.
12.3.11 Application and internet navigation
You can navigate through an application and the web in general by getting an
HTML
page and then clicking a link or clicking a user interface element like a button. The
API
can perform all forms of navigation. Let's look at various types of navigation.
P
AGE
NAVIGATION
Getting a page is done with the
WebClient
getPage()
methods. You can get a page by
URL
or
URL
string, for example:
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = (HtmlPage) webClient.getPage("
http://www.google.com");
HtmlPage page2 = (HtmlPage) webClient.getPage
➥
(new URL("
http://www.yahoo.com"));
If a page is absent or isn't reachable, the
API
throws an exception. See section 12.3.10,
“Test failures and exceptions.”