How to tell htmlunit to ignore certain errors
Asked Answered
S

7

5

I am currently trying out HtmlUnit. I want to fill out an form and then submit it. But this results in an error, which seems to be caused by a file not being accessible, which results in a connection reset being thrown. How can I suppress this, as this is not crucial considering the loading process of the whole website? It doesn't seem to affect the program, though, I just want it to be silenced.

Apr 05, 2012 12:25:13 PM com.gargoylesoftware.htmlunit.html.HtmlPage loadExternalJavaScriptFile
Schwerwiegend(severe): Error loading JavaScript from [*link*].
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:152)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1358)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadJavaScriptFromUrl(HtmlPage.java:1008)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:950)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:349)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$1.execute(HtmlScript.java:230)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:642)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.access$400(JavaScriptEngine.java:79)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:590)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:499)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:474)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:870)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:302)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:368)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$1.execute(HtmlScript.java:230)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:240)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:598)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:556)
    at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1142)
    at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1044)
    at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206)
    at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3018)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2005)
    at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:908)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:789)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:225)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:179)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:221)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:106)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:433)
    at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2187)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:625)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:707)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1246)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1195)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1158)
    at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:138)
    at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:99)
    at com.tronicguard.discovery.WebsiteManipulatorImplementation.<init>(WebsiteManipulatorImplementation.java:71)
    at Main.main(Main.java:10)
Shelton answered 5/4, 2012 at 10:45 Comment(2)
HtmlUnit found a bug in your application: a JavaScript file is not available. If this file should be available, then make it available. If this file doesn't serve any purpose, then remove the reference to it. The point of testing is to detect and fix bugs, not to ignore them.Calm
I do known that, but at the moment I am just trying it out on different sites which I do not have remote access to.Shelton
F
4

Try the following

webClient.setThrowExceptionOnFailingStatusCode(false);

It should handle the 404 (Not found) error status.

Forum answered 5/4, 2012 at 11:23 Comment(5)
I guess it's a server error considering that its a connection reset. Why don't you have it inside the try catch block and handle it gracefully?Forum
can I even catch it, as my program continues perfectly after the stacktrace is printedShelton
In this case, IMO, it wouldn't make much of difference. Catching it would just help you handle logs much cleaner without the entire stack trace.Forum
if you put an example of how to catch it I will accept your answer as fitting This is the code that somehow causes it: next = (HtmlPage) submit.click();Shelton
submit is a button I extracted from the current form I am currently filling outShelton
G
2

I experienced a similar error while running the test harness of Jenkins after compiling with the jkd1.7.0_3. Moving back to the JDK1.6.0_30 solved the problem.

Grandmotherly answered 13/7, 2012 at 8:46 Comment(0)
N
2

INSTANTIATE THE htmlUnit DRIVER AS FOLLOWS:

driver = new SilentHtmlUnitDriver(capabilities);

ADD THE FOLLOWING AS A NESTED CLASS (in the same class you instantiated the driver):

         protected class SilentHtmlUnitDriver extends HtmlUnitDriver {
              SilentHtmlUnitDriver() {
                  super();
                  this.getWebClient().setCssErrorHandler(new SilentCssErrorHandler());
                  this.getWebClient().getOptions().setThrowExceptionOnFailingStatusCode(false);
                  this.getWebClient().getOptions().setThrowExceptionOnScriptError(false);

            }

            SilentHtmlUnitDriver(DesiredCapabilities capabilities) {
               super(capabilities);
               this.getWebClient().setCssErrorHandler(new SilentCssErrorHandler());
            }
}
Nodal answered 12/6, 2014 at 17:53 Comment(0)
B
1

HtmlPage.loadExternalJavaScriptFile() will always log this error depending on the settings for your logging. You would need to disable logging for the page, or change the threshold.

For example , if you're using log4j, you can set the property log4j.logger.com.gargoylesoftware.htmlunit.html.HtmlPage = FATAL

This will however hide the logging of all errors logged in HtmlPage.

Bleeding answered 10/4, 2012 at 23:44 Comment(0)
A
1

Try this to disable annoying warnings in HtmlUnit (sorry if the code isn't quite right - I "translated" it from Clojure, but I hope you get the main idea, at least) :

Import java.util.logging.Logger;
Import java.util.logging.Level;

Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

Take a look at Turning HtmlUnit Warnings off for more info. There's a more complicated solution on there, but this simple one worked for me.

Amygdaline answered 17/6, 2014 at 5:57 Comment(0)
O
1

Use this:

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);
Orpiment answered 12/2, 2015 at 18:51 Comment(0)
G
0

You can use HttpUnitOptions.setScriptingEnabled(true); to disable javascript in HttpUnit and also HttpUnitOptions.setExceptionsThrownOnScriptError(false);

Gorged answered 5/4, 2012 at 12:4 Comment(3)
this package is unknown, how do I get the HttpUnitOptions class/instance?Shelton
This are actually from the com.meterware.httpunit.HttpUnitOptions; so you need to download httpunit jar before calling HttpUnitOptions.setScriptingEnabled(true);Gorged
How this refers to HtmlUnit?Leeds

© 2022 - 2024 — McMap. All rights reserved.