Turning HtmlUnit Warnings off

2019-01-03 09:02发布

Do you know how can I turn Warnings, Notes, Errors in HtmlUnit off?

13条回答
Viruses.
2楼-- · 2019-01-03 09:41

Put this somewhere around the start of your code, it will shut its dirty mouth:

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

    webClient = new WebClient(bv);
    webClient.setCssEnabled(false);

    webClient.setIncorrectnessListener(new IncorrectnessListener() {

        @Override
        public void notify(String arg0, Object arg1) {
            // TODO Auto-generated method stub

        }
    });
    webClient.setCssErrorHandler(new ErrorHandler() {

        @Override
        public void warning(CSSParseException exception) throws CSSException {
            // TODO Auto-generated method stub

        }

        @Override
        public void fatalError(CSSParseException exception) throws CSSException {
            // TODO Auto-generated method stub

        }

        @Override
        public void error(CSSParseException exception) throws CSSException {
            // TODO Auto-generated method stub

        }
    });
    webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

        @Override
        public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void scriptException(HtmlPage arg0, ScriptException arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
            // TODO Auto-generated method stub

        }
    });
    webClient.setHTMLParserListener(new HTMLParserListener() {

        @Override
        public void warning(String arg0, URL arg1, int arg2, int arg3, String arg4) {
            // TODO Auto-generated method stub

        }

        @Override
        public void error(String arg0, URL arg1, int arg2, int arg3, String arg4) {
            // TODO Auto-generated method stub

        }
    });

    webClient.setThrowExceptionOnFailingStatusCode(false);
    webClient.setThrowExceptionOnScriptError(false);
查看更多
倾城 Initia
3楼-- · 2019-01-03 09:44

Have a look at the docs.

There is a sample log4 file used by the test suite, you can find it here, you can disable everything if you wish.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-03 09:50

try this:

  java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
查看更多
淡お忘
5楼-- · 2019-01-03 09:50

This worked for me

@Test
    public void homePage() throws Exception {
        final WebClient webClient = new WebClient();   
 webClient.setThrowExceptionOnScriptError(false);
    final HtmlPage page = webClient.getPage("http://localhost:8080/web/guest/home");
查看更多
不美不萌又怎样
6楼-- · 2019-01-03 09:52

Turn the loggers off. But that is not a good solution, since you might want to have some uncommon issues in the logs.

I know Htmlunit, produces a lot of unimportant exceptions, warnings etc.

You can suppress of few of those using :

client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);
查看更多
再贱就再见
7楼-- · 2019-01-03 09:53

Here you can get info on how to manipulate logging of HtmlUnit.

This is what I added to my log4j.properties in order to disable verbose debugging messages from HtmlUnit components:

# Set specific logger levels.
log4j.logger.org.mortbay.log=fatal
log4j.logger.org.apache.http=fatal
log4j.logger.org.apache.http.headers=fatal
log4j.logger.org.apache.http.wire=fatal
# For HttpClient 3, which is used by FirefoxDriver
log4j.logger.httpclient.wire=fatal
log4j.logger.org.apache.commons=fatal
log4j.logger.com.gargoylesoftware.htmlunit=fatal
log4j.logger.com.gargoylesoftware.htmlunit.WebTestCase=fatal
# Change this to TRACE when enabling the debugger.
log4j.logger.com.gargoylesoftware.htmlunit.javascript.DebugFrameImpl=fatal
查看更多
登录 后发表回答