Browser-Based Test Harness

Although running tests from Rhino is tremendously useful, DOH also provides a harness that allows you to automate running tests from within a browser window. Basically, you just define a test as an ordinary HTML page and then load the test page into the DOH test runner using query string parameters in the test runner's URL; internally, JavaScript in the test runner examines the query string, pulls out configuration values such as testUrl and uses them to inject your test page into a frame.

Of course, you can still run your browser-based test without the DOH test runner, but you won't get a nice visual display with optional Homer Simpson sound effects if you're willing to read the test results as console output.

Browser Test Example

The following is an example test defined as an ordinary HTML page. Notice that the example uses a local installation of Dojo because as of version 1.1, DOH is not delivered via AOL's CDN:

<html>
    <head><title>Fun with DOH!</title>

        <script
        type="text/javascript"
        src="local/path/to/dojo/dojo.js">
        </script>

        <script type="text/javascript">
            dojo.require("doh.runner");

            dojo.addOnLoad(function(  ) {
                doh.register("fooTest", [
                    function foo(  ) {
                        var bar = [];
                        bar.push(1);
                        bar.push(2);
                        bar.push(3);

                        doh.is(bar.indexOf(1), 0); //not portable!
                    }
                ]);

                doh.run(  );
            });
        </script>

    </head>
    <body></body>
</html>

Asynchronous Browser Test Example

Almost any web application test suite worth its salt is going to involve a significant number of tests that depend upon asynchronous conditions such as waiting for an animation to happen, a server side callback to occur, and so on. Example 16-4 introduces how you can create asynchronous test with DOH. The key concept is that a doh.Deferred (pretty much an ordinary dojo.Deferred with some tweaks) except that it is internal to DOH and, as such, doesn't have external dependencies. Chapter 4 included an extensive discussion of Deferreds if you need a quick refresher.

Before the relevant code sample, here's the basic pattern at play for asynchronous testing with DOH:

  • Create a doh.Deferred that will be used to verify the results from asynchronous function (that returns back a dojo.Deferred)

  • Call whatever asynchronous function returns back the dojo.Deferred and save a reference to it

  • Add callbacks and errbacks to the dojo.Deferred that will simply pass the asynchronous function's results through to the doh.Deferred's own callbacks and errbacks

Example 16-4. Skeleton for an asynchronous test

doh.register("foo", [

    function(  ) {
        var dohDfd = new doh.Deferred();
        var expectedResult = "baz";

        var  dojoDfd = asynchronousBarFunction();
        dojoDfd.addBoth(function(response, io) {

            //reference the dohDfd as needed...
            if (response == expectedResult) {
                  dohDfd.callback(true);
            }
            else {
                  dohDfd.errback(new Error( /* ... */));
            }
        });

        //...and return back the dohDfd
        return dohDfd;
     }
]);

Depending on your specific test constraints, you might provide explicit timeout values to ensure that the asynchronous operations involved timeout according to your specific testing criteria. At any rate, the key takeaway is that asynchronous testing doesn't need to be terribly complicated; the Deferred abstraction simplifies most of that complexity, so you're left to focus on the task at hand.

Get Dojo: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.