HtmlUnit禁止外部请求 [英] HtmlUnit forbid external requests
问题描述
我使用HtmlUnit对我的网站进行自动测试. 我的网站使用gmaps api-发送外部网站的请求要花费很多时间(我有数百个测试和数千个页面加载).
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).
我需要某种方法告诉HtmlUnit仅加载本地页面(存储在IIS express中),并强制加载外部资源以使测试运行更快.
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.
推荐答案
您可以阻止HTMLUnit使用WebConnectionWrapper
来访问某些URL:
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());
}
}
});
除非您需要对其进行测试,否则您可能需要考虑禁用JavaScript和CSS之类的项目,但我发现这样做也会加快速度.
Unless you need to test them you may want to consider disabling items like JavaScript and CSS, I find that also speeds it up.
这篇关于HtmlUnit禁止外部请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!