Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore HTMLUnit warnings/errors related to jQuery?

Tags:

java

htmlunit

Is it possible to teach HTMLUnit to ignore certain javascript scripts/files on a web page? Some of them are just out of my control (like jQuery) and I can't do anything with them. Warnings are annoying, for example:

[WARN] com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument:
getElementById(script1299254732492) did a getElementByName for Internet Explorer

Actually I'm using JSFUnit and HTMLUnit works under it.

like image 780
yegor256 Avatar asked Mar 04 '11 16:03

yegor256


2 Answers

If you want to avoid exceptions because of any JavaScript errors:

 webClient.setThrowExceptionOnScriptError(false);        
like image 64
MrSmith42 Avatar answered Nov 02 '22 14:11

MrSmith42


Well I am yet to find a way for that but I have found an effective workaround. Try implementing FalsifyingWebConnection. Look at the example code below.

public class PinConnectionWrapper extends FalsifyingWebConnection {

    public PinConnectionWrapper(WebClient webClient)
            throws IllegalArgumentException {
        super(webClient);
    }

    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse res = super.getResponse(request);
        if(res.getWebRequest().getUrl().toString().endsWith("/toolbar.js")) {
            return createWebResponse(res.getWebRequest(), "",
"application/javascript", 200, "Ok");
        }
        return res;
    }

}

In the above code whenever HtmlUnit will request for toolbar.js my code will simply return a fake empty response. You can plug-in your above wrapper class into HtmlUnit as below.

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
new PinConnectionWrapper(webClient);
like image 1
AppleGrew Avatar answered Nov 02 '22 14:11

AppleGrew