Can I Use Htmlunit To Listen For Resource Loading Events?
I'm trying to use HtmlUnit to detect resources (scripts, images, stylesheets, etc) that fail to load on a webpage. I've tried new WebConnectionWrapper(webClient) { @Override
Solution 1:
- For AJAX, you have to wait as hinted here.
- HtmlUnit doesn't automatically loads HtmlLink and HtmlImage, for performance reasons.
- Please find below snippet that will print all. You can use .getByXPath() to get list of all elements.
public static void main(String[] args) throws Exception {
try (final WebClient webClient = new WebClient()) {
new WebConnectionWrapper(webClient) {
@Override
public WebResponse getResponse(WebRequest request) throws IOException {
WebResponse response = super.getResponse(request);
System.out.println(request.getUrl());
System.out.println(response.getStatusCode());
return response;
}
};
String url = "http://localhost/test.html";
HtmlPage page = webClient.getPage(url);
// to wait for AJAX
webClient.waitForBackgroundJavaScript(3000);
// to forcibly load the link
HtmlLink link = page.getFirstByXPath("//link");
link.getWebResponse(true);
// to forcibly load the image
HtmlImage image = page.getFirstByXPath("//img");
image.getImageReader();
}
}
Post a Comment for "Can I Use Htmlunit To Listen For Resource Loading Events?"