开发者

Detecting when jasmine tests complete

I am running jasmine tests like this;

   jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
   jasmine.getEnv().开发者_高级运维execute();

I would like to detect, using JavaScript, when the tests complete. How can I?


As @Xv. suggests, adding a reporter will work. You can do something as simple as:

jasmine.getEnv().addReporter({
    jasmineDone: function () {
        // the specs have finished!
    }
});

See http://jasmine.github.io/2.2/custom_reporter.html.


Some alternative ways:

A) Use the ConsoleRunner, that accepts an onComplete option. Older versions (1.2rc1) receive the complete callback as a standalone parameter.

Since you also supply the function that writes (options.print) you keep control about having the test reports written to the console.

You can have several reporters active at the same time jasmineEnv.addReporter().

B) Haven't tried, but you could create your own reporter, with empty implementations of every public method but jasmineDone()

C) Check an old post in the Jasmine google group, where the author saves and overrides jasmine.getEnv().currentRunner().finishCallback:

    var oldCallback = jasmineEnv.currentRunner().finishCallback;
    jasmineEnv.currentRunner().finishCallback = function () {
        oldCallback.apply(this, arguments);
        $("body").append( "<div id='_test_complete_signal_'></div" );   
    };
    jasmineEnv.execute();


I found two different ways to solve this issue. One is to hack jasmine to throw a custom event when it completes. Because I wanted to screen scrape after the test loaded, I inserted the event trigger into jasmine-html.js at the end of "reportRunnerResults"

$( 'body' ).trigger( "jasmine:complete" );

Then it's a matter of listening for the event:

$( 'body' ).bind("jasmine:complete", function(e) { ... }

In my case, I was running jasmine in an iFrame and wanted to pass the results to a parent window, so I trigger an event in the parent from my first bind:

$(window.parent).find('body').trigger("jasmine:complete");

It is also possible to do this without jquery. My strategy was to poll for text to be added to the "finished-at" span. In this example I poll every .5 seconds for 8 seconds.

var counter = 0;

function checkdone() {
    if ( $('#test-frame' ).contents().find('span.finished-at').text().length > 0) {
        ...
        clearInterval(timer);
    } else {
        counter += 500;
        if (counter > 8000) {
            ...
            clearInterval(timer);
        }
    }
}

var timer = setInterval( "checkdone()", 500 );


I'm running Jasmine 1.3.1 with the HtmlReporter. I ended up hooking in like this:

var orig_done = jasmineEnv.currentRunner_.finishCallback;
jasmineEnv.currentRunner_.finishCallback = function() {
    orig_done.call(this);
    // custom code here
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜