問題描述
我目前正在使用 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 中正確執行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!