What is the use of providing browser version in HtmlUnitDriver

2.6k views Asked by At

I am trying to run selenium webdriver in Linux machine. So i tried to use HtmlUnitDriver to achieve this.

I am getting the following error while executing the jquery.

TypeError: Cannot find function addEventListener in object [object HTMLDocument]. (http://localhost/xxx/js_lib/jquery2/jquery-2.1.0.min.js#2)

The above exception is occured when the HtmlunitDriver is used without any browser version as below,

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

Also the junit test case is getting failed with following reason,

org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: abcde

If i pass the Browser version as below,

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
driver.setJavascriptEnabled(true);

The test case is getting pass, but exception is thrown in the console like,

SEVERE: Job run failed with unexpected RuntimeException: Exception invoking Window.getComputedStyle() with arguments [Text, String]
======= EXCEPTION START ========
Exception class=[java.lang.IllegalArgumentException]
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking Window.getComputedStyle() with arguments [Text, String]
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:847)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:779)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.setState(XMLHttpRequest.java:233)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(XMLHttpRequest.java:762)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.access$000(XMLHttpRequest.java:104)
    at com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest$1.run(XMLHttpRequest.java:623)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
    at com.gargoylesoftware.htmlunit.javascript.background.JavascriptXMLHttpRequestJob.run(JavascriptXMLHttpRequestJob.java:36)
    at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:415)
    at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:167)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: Exception invoking Window.getComputedStyle() with arguments [Text, String]
    at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:193)
    at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:449)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1536)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3286)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:772)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:832)
    ... 13 more
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@4d3015e7
    at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:153)
    ... 22 more

I have the following list of questions,

  1. To use firefox browser in HtmlUnitDriver, Do i need to install firefox in Linux machine ?
  2. why the above exceptions are thrown?
  3. Is there any option to run selenium in lunix machine without GUI?

I am starter in selenium and need your valuable thoughts on this. thanks

1

There are 1 answers

0
Ahmed Ashour On

To use firefox browser in HtmlUnitDriver, Do i need to install firefox in Linux machine ?

No, you don't need to install any real browsers.

Why the above exceptions are thrown?

Because HtmlUnit JavaScript support is not very accurate sometimes, please read here.

Also, please try to provide complete details (URL or small sample project), so others can see the same errors.