My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
TestFrameworkIntegration  
Using PhantomJS with QUnit, Jasmine, FuncUnit, etc
Updated Apr 2, 2012 by ariya.hi...@gmail.com

Jasmine

Jasmine is a test framework for JavaScript-based web application.

PhantomJS include a very simple example to drive Jasmine-based unit tests. However, for real-world use, it is highly recommended to use other full-featured PhantomJS and Jasmine integration, e.g Guard::Jasmine. For more tutorials and examples on how to leverage PhantomJS with Jasmine, check out some external articles.

The code below is the partial source for run-jasmine.js example (refer to the example source file for the full source), which given a URL (usually points to the web server hosting the tests), will execute the test and display the test result.

// This is code snippet and not self-contained. See examples/run-jasmine.js for details

page.open(phantom.args[0], function(status){
    if (status !== "success") {
        console.log("Unable to access network");
        phantom.exit();
    } else {
        waitFor(function(){
            return page.evaluate(function(){
                if (document.body.querySelector('.finished-at')) {
                    return true;
                }
                return false;
            });
        }, function(){
            page.evaluate(function(){
                console.log(document.body.querySelector('.description').innerText);
                list = document.body.querySelectorAll('div.jasmine_reporter > div.suite.failed');
                for (i = 0; i < list.length; ++i) {
                    el = list[i];
                    desc = el.querySelectorAll('.description');
                    console.log('');
                    for (j = 0; j < desc.length; ++j) {
                        console.log(desc[j].innerText);
                    }
                }
            });
            phantom.exit();
        });
    }
});

There is an asynchronous loop to allow the tests to run, and if a certain HTML element indicating Jasmine has finished is detected, then the result is shown.

Example output there is no failure at all:

2322 specs, 0 failures in 1.461s

QUnit Driver

QUnit is a JavaScript test library. PhantomJS can drive QUnit-based suite using the following `run-qunit.js` (shown here only the relevant part, refer to the example file for the full source):

// This is code snippet and not self-contained. See examples/run-qunit.js for details

page.open(phantom.args[0], function(status){
    if (status !== "success") {
        console.log("Unable to access network");
        phantom.exit();
    } else {
        waitFor(function(){
            return page.evaluate(function(){
                var el = document.getElementById('qunit-testresult');
                if (el && el.innerText.match('completed')) {
                    return true;
                }
                return false;
            });
        }, function(){
            var failedNum = page.evaluate(function(){
                var el = document.getElementById('qunit-testresult');
                console.log(el.innerText);
                try {
                    return el.getElementsByClassName('failed')[0].innerHTML;
                } catch (e) { }
                return 10000;
            });
            phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
        });
    }
});

Example running core jQuery tests:

phantomjs run-qunit.js http://swarm.jquery.org/git/jquery/test/?filter=core

gives the following report:

 Tests completed in 1528 milliseconds.
 1223 tests of 1223 passed, 0 failed.
 

FuncUnit

Read more about FuncUnit integration with PhantomJS.

YUI Test

Please check the project: github.com/metafeather/phantomjs-yuitest.

Powered by Google Project Hosting