3

I am using htmlunit 2.9 and on java script parsing I am getting script exception due to console in following exception

function debug(o){
  if (console && console.log){
    console.log(o)
  }
};

Stacktrace

EcmaError:
    lineNumber=[168]
    column=[0]
    lineSource=[null]
    name=[ReferenceError]
    sourceName=[script in http://localhost:808/mypage/ll.html from (154, 36) to (301, 14)]
    message=[ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)
         at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
         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.callFunction(JavaScriptEngine.java:545)
         at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
         at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
         at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
         at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:214)

if I try specified page on firefox it works fine, I have tried v 3.6 as well as 9.0.1.

i have tried also to set setThrowExceptionOnScriptError(false) in order to avoid exception but engine stops or do not parse javascript after getting an error.

Is there any way that javascript engine can understand console in javascript?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Rehman
  • 3,908
  • 6
  • 28
  • 29
  • Does it also stop if you use BrowserVersion.FIREFOX_3_6? – milan Dec 31 '11 at 22:00
  • Possibly helpful (see lower answers as well): http://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code – Jared Farrish Dec 31 '11 at 22:37

3 Answers3

2

https://sourceforge.net/tracker/index.php?func=detail&aid=3518475&group_id=47038&atid=448266 implies that support for console was just added to HtmlUnit.

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
2

Your if condition isn't properly structured:

if (console && console.log){

That first if will throw an error if its not set; accessing console in environments its not defined in is like accessing any undefined variable; it will throw a ReferenceError.

Try:

if( typeof console != "undefined" && console.log ) {

Or:

if(window.console && console.log) {

It doesn't throw in error in Firefox since Firefox implements the Firebug API, as do Chrome and Safari. But, by default, Internet Explorer does not, so, it's worth doing a proper feature check here, as it will throw a ReferenceError in browsers that don't implement this API.

Yahel
  • 37,023
  • 22
  • 103
  • 153
  • thanks, i am aware of that failure thats why the page do not function proberly on IE as well as other browsers except firefox. And is there any way with htmlunit that i can change only that portion of code ? – Rehman Jan 01 '12 at 00:09
  • The fix here is to just fix how you're feature testing (which is an actual error, not just a whole in the testing platform); not sure you can rewrite the testing platform to recognize the console object – Yahel Jan 01 '12 at 00:33
  • 1
    Erm, `"whole".replace("w","")`. – Yahel Jan 01 '12 at 00:47
  • actually i am not using htmlunit for testing but for parsing website that is reason i can not change anyone's code and more less interested in changing JS before parsing using htmlunit.If there is a way then please let me know or atleast that aforementioned code if (Console && console.log) should also work in htmlunit using BrowserVersion.FIREFOX_3 or some higher version because it works in local browser with same version without using firebug – Rehman Jan 01 '12 at 12:55
0

Your code does use the java script console object, and it's not supported till the current version, and it is promised to be supported in the next release as it is said here

Khafaga
  • 1,537
  • 4
  • 15
  • 24