Skip to content Skip to sidebar Skip to footer

Htmlunit Forbid External Requests

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 thousand

Solution 1:

You can prevent HTMLUnit from accessing certain URL's using as WebConnectionWrapper:

browser.setWebConnection(newWebConnectionWrapper(browser) {
  @Overridepublic WebResponse getResponse(final WebRequest request)throws IOException {
    if (<<CONDITION HERE(such as `request.getUrl().toString().contains("uq.edu.au")`)>>) {
      returnsuper.getResponse(request);
    } else {
      returnnewStringWebResponse("", 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.

Post a Comment for "Htmlunit Forbid External Requests"