問(wèn)題描述
我目前正在使用 HtmlUnit 開(kāi)發(fā)一些測(cè)試.它正在加載一個(gè)包含 Braintree.js(他們的表單加密庫(kù))的頁(yè)面.我有一堆正在運(yùn)行,但我被困在它稱為加密的地方.有問(wèn)題的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 不包括加密.是否可以自己包含一個(gè)加密庫(kù)?
I suppose HtmlUnit doesn't include crypto. Would it be possible to include a crypto library myself?
推薦答案
根據(jù)您的評(píng)論,我不得不告訴您,HtmlUnit 在涉及到 JavaScript 時(shí)是一個(gè)令人頭疼的問(wèn)題.它會(huì)抱怨很多沒(méi)有定義的變量和未知的函數(shù)等等.
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.
真正的瀏覽器更靈活,例如:它們接受語(yǔ)法不正確的 JavaScript 片段.HtmlUnit 期望一切都是完美的,沒(méi)有任何錯(cuò)誤.此外,即使您沒(méi)有錯(cuò)過(guò)分號(hào),HtmlUnit 也可能會(huì)抱怨.
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 語(yǔ)法正確
- 避免使用復(fù)雜庫(kù)的用戶(jQuery 似乎得到了適當(dāng)?shù)闹С?
- 如果您可以使用非最小化版本的庫(kù),那么值得一試
- 盡量避免使用復(fù)雜的 jQuery 方法(例如:向元素動(dòng)態(tài)添加事件)
- 還有最重要的一點(diǎn):嘗試在不同的
BrowserVersions
之間切換.事實(shí)證明,Internet Explorer(具有諷刺意味的是)在解釋 JavaScript 時(shí)提供了最佳結(jié)果
- 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
這篇關(guān)于JavaScript 未在 HtmlUnit 中正確執(zhí)行的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!