如何正确初始化HtmlUnit驱动程序以使用javascript

时间:2013-11-19 20:24:43

标签: java javascript selenium-webdriver htmlunit htmlunit-driver

我正在尝试使用Webdriver和无头浏览器,并遇到了一些困难。遗憾的是,我不能分享我的所有代码,但我可以展示我是如何设置它的。因此,让我先说一下我尝试实现的基本用例,我使用chrome驱动程序来确保它不是像一个糟糕的xpath那样平凡,而且脚本运行得很好。下面是我正在初始化htmlunit的方法,其中有一些是出现的许多错误

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME)   
driver.setJavascriptEnabled(true)

在我的登录页面上使用htmlunit驱动程序就像一个冠军,一旦我到达应用程序的主页但事情就会崩溃。

SEVERE: Job run failed with unexpected RuntimeException: TypeError: Cannot call method     "add" of undefined (http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js#12)
======= EXCEPTION START ========
EcmaError: lineNumber=[12] column=[0] lineSource=[null] name=[TypeError] sourceName=    [http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js] message=[TypeError: Cannot     call method "add" of undefined (http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js#12)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call method "add" of     undefined (http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js#12)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:684)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:616)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:591)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:985)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptFunctionJob.runJavaScript(JavaScriptFunctionJob.java:53)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutionJob.run(JavaScriptExecutionJob.java:102)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)
at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)
at java.lang.Thread.run(Unknown Source)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot call method "add" of undefined (http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js#12)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3608)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3624)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.undefCallError(ScriptRuntime.java:3638)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2195)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2189)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333)
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:405)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:609)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)
... 10 more
Enclosed exception: 
net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot call method     "add" of undefined (http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js#12)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603)
at     net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3608)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3624)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.undefCallError(ScriptRuntime.java:3638)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2195)
at    net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2189)
at     net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333)
at script(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:12)
at script.Og(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:105)
at script.Pg(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:105)
at script(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:115)
at script(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:108)
at script.Tg(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:105)
at script(http://s.ytimg.com/yts/jsbin/www-embed-player-vfl2yUXMY.js:117)
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:405)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:609)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:616)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:591)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:985)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptFunctionJob.runJavaScript(JavaScriptFunctionJob.java:53)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutionJob.run(JavaScriptExecutionJob.java:102)
at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)
at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)
at java.lang.Thread.run(Unknown Source)
== CALLING JAVASCRIPT ==

 function () {
      Tg(d, b);
  }

除此之外,我收到了大量的警告:

  

2013年11月19日下午1:51:42 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler警告

我怀疑由于所有这些错误,我的javascript实际上并没有加载。为了验证这一点,当我使用Chrome运行测试时,我在主页面上获取了页面源代码,然后使用HtmlUnit做了同样的事情,这证实了我的怀疑。没有javascipts生成的html存在。我们的网站使用JQuery,Knockout和其他几个插件,所有库都是相当常见的地方,没有一个是chrome驱动程序的问题,或者在我运行应用程序时在dev工具控制台中引发任何类型的错误。任何帮助将不胜感激。

0 个答案:

没有答案