JavaScript 未在 HtmlUnit 中正确执行

JavaScript not being properly executed in HtmlUnit(JavaScript 未在 HtmlUnit 中正确执行)
本文介绍了JavaScript 未在 HtmlUnit 中正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在使用 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 中正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)