Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript not being properly executed in HtmlUnit

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?

like image 437
Steve s. Avatar asked Dec 05 '25 13:12

Steve s.


1 Answers

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
like image 116
Mosty Mostacho Avatar answered Dec 07 '25 22:12

Mosty Mostacho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!