问题描述
我目前正在使用 HtmlUnit 开发一些测试.它正在加载一个包含 Braintree.js(他们的表单加密库)的页面.我有一堆正在运行,但我被困在它称为加密的地方.有问题的JS是:
I'm currently developing some tests with HtmlUnit. It's loading a page that contains braintree.js (their form encryption library). I have a bunch running, but I'm stuck where it calls crypto. The JS in question is:
(function() {
try {
var ab = new Uint32Array(32);
crypto.getRandomValues(ab);
sjcl.random.addEntropy(ab, 1024, "crypto.getRandomValues");
} catch (e) {}
})();
HtmlUnit 正在抛出:
HtmlUnit is throwing:
EcmaError, ReferenceError, "'crypto' is not defined."
我想 HtmlUnit 不包括加密.是否可以自己包含一个加密库?
I suppose HtmlUnit doesn't include crypto. Would it be possible to include a crypto library myself?
推荐答案
根据您的评论,我不得不告诉您,HtmlUnit 在涉及到 JavaScript 时是一个令人头疼的问题.它会抱怨很多没有定义的变量和未知的函数等等.
Based on your comment, I have to tell you that HtmlUnit is a pain in the neck when it comes to JavaScript. It will complain a lot about variables not being defined and unknown functions and so on.
真正的浏览器更灵活,例如:它们接受语法不正确的 JavaScript 片段.HtmlUnit 期望一切都是完美的,没有任何错误.此外,即使您没有错过分号,HtmlUnit 也可能会抱怨.
Real browsers are more flexible, eg: they accept syntactically incorrect pieces of JavaScript. HtmlUnit expects everything to be perfect without any kind of error. Furthermore, even if you didn't miss a semicolon, HtmlUnit might complain.
我的建议:
- 确保您的 JavaScript 语法正确
- 避免使用复杂库的用户(jQuery 似乎得到了适当的支持)
- 如果您可以使用非最小化版本的库,那么值得一试
- 尽量避免使用复杂的 jQuery 方法(例如:向元素动态添加事件)
- 还有最重要的一点:尝试在不同的
BrowserVersions
之间切换.事实证明,Internet Explorer(具有讽刺意味的是)在解释 JavaScript 时提供了最佳结果
- Make sure your JavaScript is syntactically correct
- Avoid the user of complex libraries (jQuery seems to be properly supported)
- If you can use non-minimized versions of libraries it's worth giving it a try
- Try to avoid complex jQuery methods (eg: adding events dynamically to elements)
- And the most important one: Try switching between different
BrowserVersions
. Internet Explorer (ironically) has proven to provide the best results when it comes to interpreting JavaScript
这篇关于JavaScript 未在 HtmlUnit 中正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!