java - HtmlUnitDriver中提供浏览器版本有什么用

标签 java linux selenium selenium-webdriver htmlunit-driver

我正在尝试在 Linux 机器上运行 selenium webdriver。所以我尝试使用 HtmlUnitDriver 来实现这一点。

执行 jquery 时出现以下错误。

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

在没有任何浏览器版本的情况下使用 HtmlunitDriver 时会出现上述异常,如下所示,

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

此外,由于以下原因,junit 测试用例也失败了,

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

如果我通过如下浏览器版本,

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

测试用例正在通过,但在控制台中抛出异常,如

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

我有以下问题列表,

  1. 要在 HtmlUnitDriver 中使用 firefox 浏览器,我需要在 Linux 机器上安装 firefox 吗?
  2. 为什么会抛出上述异常?
  3. 是否有任何选项可以在没有 GUI 的情况下在 lunix 机器上运行 selenium?

我是 selenium 的新手,需要您对此提出宝贵意见。谢谢

最佳答案

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

不,您不需要安装任何真正的浏览器。

Why the above exceptions are thrown?

由于HtmlUnit JavaScript 支持有时不是很准确,请阅读here .

此外,请尽量提供完整的详细信息(URL 或小型示例项目),以便其他人可以看到相同的错误。

关于java - HtmlUnitDriver中提供浏览器版本有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827575/

相关文章:

使用 RobotFramework 运行测试用例时出现 Selenium 导入错误

java - spring中@ModelAttribute、model.addAttribute有什么区别?

java - 毫无意义地使用 PrintStream?

c++ - 为什么 string::append 操作表现奇怪?

linux - 使用 linux 脚本命令

更改浏览器后 Python Selenium 保留元素

java - BoneCP 支持 XA 事务吗?

java - Guice ThrowingProvider 问题

linux - linux 的 apache 是否以不同的用户身份处理 http 请求?

java - cucumber-Junit 断言失败 - 无法断言 '£' 符号