HtmlUnit forbid external requests

一笑奈何 提交于 2019-11-28 05:34:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!