gpt4 book ai didi

jquery - htmlunit 和 document.addEventListener

转载 作者:行者123 更新时间:2023-12-01 01:45:44 24 4
gpt4 key购买 nike

我是 HtmlUnit 的新手。如果这个问题已经被问过并得到回答,请原谅。

我在使 HtmlUnit 处理使用 JQuery 的页面时遇到问题。更准确地说,我无法让 HtmlUnit 处理任何使用 document.addEventListener() 的 JS 代码,例如 JQuery。例如,以下页面可以在浏览器中运行,但不能在 HtmlUnit 中运行:

<html><head>
<script>
function atest() {}
document.addEventListener( "load", atest, false );
</script>
</head>
<body>This page will load in a browser, but not HtmlUnit.</body></html>

产生的错误是:

======= EXCEPTION START ========
EcmaError: lineNumber=[5] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[script in http://myhost:8080/mpt/dummy from (3, 9) to (6, 10)] message= [TypeError: Cannot find function addEventListener in object [object]. (script in http://myhost:8080/mpt/dummy from (3, 9) to (6, 10)#5)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function addEventListener in object [object]. (script in http://myhost:8080/mpt/dummy from (3, 9) to (6, 10)#5)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:705)

我尝试了几个不同版本的 JQuery 和 HtmlUnit。我确信 HtmlUnit 可以与 JQuery 一起使用(不是吗?),但可能有一个我不知道的问题。所以提示表示赞赏。我的 JUnit 代码(如果相关)是:

@Test
public void test() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
WebClient webClient = new WebClient();
webClient.getPage("http://myhost:8080/mpt/dummy");
}

谢谢

最佳答案

是的,似乎 html 单元有一个错误。

https://github.com/searls/jasmine-maven-plugin/issues/135

最后,我定制了 Jasmin maven 插件,以使用 phantom js headless 浏览器运行我的 javascript 测试。

http://searls.github.io/jasmine-maven-plugin/phantomjs.html

如果你使用的maven低于3.1,我现在使用的是3.0.4,你无法使用自动为你安装phanom js浏览器的maven插件。这种情况下,就到Web端手动下载。

将类似的内容放入您的 Maven 配置中。

<webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
<webDriverCapabilities>
<capability>
<name>phantomjs.binary.path</name>
<value>D:\phantomjs-1.9.8\phantomjs.exe</value>
</capability>
</webDriverCapabilities>

看看在没有 html 单元的情况下您的测试是否会更成功。

您还可以尝试将 html 单元升级到最新版本,或者使用他们的浏览器模拟。就我而言,错误仍然存​​在,我换成了幽灵。

干杯。

关于jquery - htmlunit 和 document.addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24790826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com