问题
I use HtmlUnit for automated tests for my site. My site use gmaps api - and it takes a lot of time to send request for external site ( I have few hundreds of tests and few thousands of page loads).
I need some way to tell HtmlUnit to load only local pages (stored in IIS express), and forbit loading external resources to make my tests running more quickly.
回答1:
You can prevent HTMLUnit from accessing certain URL's using as WebConnectionWrapper
:
browser.setWebConnection(new WebConnectionWrapper(browser) {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if (<<CONDITION HERE(such as `request.getUrl().toString().contains("uq.edu.au")`)>>) {
return super.getResponse(request);
} else {
return new StringWebResponse("", request.getUrl());
}
}
});
Unless you need to test them you may want to consider disabling items like JavaScript and CSS, I find that also speeds it up.
来源:https://stackoverflow.com/questions/13978098/htmlunit-forbid-external-requests