How to ignore HTMLUnit warnings/errors related to jQuery?

青春壹個敷衍的年華 提交于 2019-12-06 17:43:02

问题


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.


回答1:


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

 webClient.setThrowExceptionOnScriptError(false);        



回答2:


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);



回答3:


Take a look at WebClient.setScriptPreProcessor. It will give you the opportunity to modify (or in your case, stop) a given script before it is executed.

Also, if it is just the warnings getting on your nerves I would suggest changing the log level.




回答4:


If you are interested in ignoring all warning log entries you can set the log level to INFO for com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument in the log4j.properties file.




回答5:


LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);

Insert this code.



来源:https://stackoverflow.com/questions/5196319/how-to-ignore-htmlunit-warnings-errors-related-to-jquery

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