3

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?

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
Steve s.
  • 301
  • 1
  • 4
  • 13
  • 1
    What do you mean by `HtmlUnit` doesn't include crypto? HtmlUnit fetches the page. You have to include the library in a `script` tag yourself. – Mosty Mostacho Oct 28 '13 at 23:00
  • 1
    Thanks, when the same page runs in a real browser, 'crypto' is resolved. But I get the unresolved error in HtmlUnit. I've been hunting for a implementation of getRandomValues in include, but haven't found one. – Steve s. Oct 28 '13 at 23:13

1 Answers1

4

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
Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
  • 1
    Thanks for the tips Mostly! I did get past this problem by creating my own little crypto.getRandomValues() method. However, now I've hit something a little deeper. I might have to give up on this whole idea, since the script is published by Braintree - I have no control. – Steve s. Oct 28 '13 at 23:53
  • 2
    Oh, I did try your idea: BrowserVersion.INTERNET_EXPLORER_10, but it doesn't include type, Uint32Array. It seems that only comes with Firefox and Chrome. – Steve s. Oct 28 '13 at 23:57