JavaScript not being properly executed in HtmlUnit

情到浓时终转凉″ 提交于 2019-12-17 21:10:06

问题


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 is throwing:

EcmaError, ReferenceError, "'crypto' is not defined."

I suppose HtmlUnit doesn't include crypto. Would it be possible to include a crypto library myself?


回答1:


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.

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.

My advice:

  • 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


来源:https://stackoverflow.com/questions/19646612/javascript-not-being-properly-executed-in-htmlunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!