2012-11-02 13:03:09 +08:00
|
|
|
/*global window:false, alert:false, jasmine:false, Node:false, */
|
|
|
|
/*jshint curly:false*/
|
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var phantom = {};
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
if (window._phantom) {
|
|
|
|
console.log = function(){
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('console', Array.prototype.slice.apply(arguments).join(', '));
|
2013-02-06 06:13:10 +08:00
|
|
|
};
|
|
|
|
}
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
phantom.sendMessage = function() {
|
|
|
|
var args = [].slice.call( arguments );
|
|
|
|
var payload = JSON.stringify( args );
|
2013-01-09 03:28:26 +08:00
|
|
|
if (window._phantom) {
|
2013-02-06 06:13:10 +08:00
|
|
|
// alerts are the communication bridge to grunt
|
|
|
|
alert( payload );
|
2013-01-09 03:28:26 +08:00
|
|
|
}
|
2013-02-06 06:13:10 +08:00
|
|
|
};
|
2013-01-09 03:28:26 +08:00
|
|
|
|
2013-02-06 06:13:10 +08:00
|
|
|
(function(){
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
function PhantomReporter() {
|
|
|
|
this.started = false;
|
|
|
|
this.finished = false;
|
|
|
|
this.suites_ = [];
|
|
|
|
this.results_ = {};
|
|
|
|
this.buffer = '';
|
|
|
|
}
|
|
|
|
|
2013-10-26 08:45:39 +08:00
|
|
|
PhantomReporter.prototype.jasmineStarted = function() {
|
2012-11-02 13:03:09 +08:00
|
|
|
this.started = true;
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('jasmine.jasmineStarted');
|
2012-11-02 13:03:09 +08:00
|
|
|
};
|
|
|
|
|
2013-10-26 08:45:39 +08:00
|
|
|
PhantomReporter.prototype.specStarted = function(specMetadata) {
|
|
|
|
specMetadata.startTime = (new Date()).getTime();
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('jasmine.specStarted', specMetadata);
|
2012-11-02 13:03:09 +08:00
|
|
|
};
|
|
|
|
|
2013-10-26 08:45:39 +08:00
|
|
|
PhantomReporter.prototype.suiteStarted = function(suiteMetadata) {
|
|
|
|
suiteMetadata.startTime = (new Date()).getTime();
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('jasmine.suiteStarted', suiteMetadata);
|
|
|
|
};
|
2013-10-26 08:45:39 +08:00
|
|
|
|
|
|
|
PhantomReporter.prototype.jasmineDone = function() {
|
2012-11-02 13:03:09 +08:00
|
|
|
this.finished = true;
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('jasmine.jasmineDone');
|
2012-11-02 13:03:09 +08:00
|
|
|
phantom.sendMessage('jasmine.done.PhantomReporter');
|
|
|
|
};
|
|
|
|
|
2013-10-26 08:45:39 +08:00
|
|
|
PhantomReporter.prototype.suiteDone = function(suiteMetadata) {
|
|
|
|
suiteMetadata.duration = (new Date()).getTime() - suiteMetadata.startTime;
|
2014-01-29 09:12:51 +08:00
|
|
|
phantom.sendMessage('jasmine.suiteDone', suiteMetadata);
|
2012-11-02 13:03:09 +08:00
|
|
|
};
|
|
|
|
|
2014-01-30 00:33:47 +08:00
|
|
|
PhantomReporter.prototype.specDone = function(specMetadata) {
|
|
|
|
specMetadata.duration = (new Date()).getTime() - specMetadata.startTime;
|
|
|
|
this.results_[specMetadata.id] = specMetadata;
|
|
|
|
|
|
|
|
// Quick hack to alleviate cyclical object breaking JSONification.
|
|
|
|
for (var ii = 0; ii < specMetadata.failedExpectations.length; ii++) {
|
|
|
|
var item = specMetadata.failedExpectations[ii];
|
|
|
|
if (item.expected) {
|
|
|
|
item.expected = stringify(item.expected);
|
|
|
|
}
|
|
|
|
if (item.actual) {
|
|
|
|
item.actual = stringify(item.actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
phantom.sendMessage( 'jasmine.specDone', specMetadata);
|
|
|
|
};
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
function stringify(obj) {
|
|
|
|
if (typeof obj !== 'object') return obj;
|
|
|
|
|
2013-02-27 11:40:20 +08:00
|
|
|
var cache = [], keyMap = [], index;
|
2012-11-02 13:03:09 +08:00
|
|
|
|
|
|
|
var string = JSON.stringify(obj, function(key, value) {
|
|
|
|
// Let json stringify falsy values
|
|
|
|
if (!value) return value;
|
|
|
|
|
|
|
|
// If we're a node
|
2013-07-02 02:23:48 +08:00
|
|
|
if (typeof(Node) !== 'undefined' && value instanceof Node) return '[ Node ]';
|
2012-11-02 13:03:09 +08:00
|
|
|
|
2013-06-14 01:54:50 +08:00
|
|
|
// jasmine-given has expectations on Specs. We intercept to return a
|
|
|
|
// String to avoid stringifying the entire Jasmine environment, which
|
|
|
|
// results in exponential string growth
|
|
|
|
if (value instanceof jasmine.Spec) return '[ Spec: ' + value.description + ' ]';
|
|
|
|
|
2012-11-02 13:03:09 +08:00
|
|
|
// If we're a window (logic stolen from jQuery)
|
|
|
|
if (value.window && value.window === value.window.window) return '[ Window ]';
|
|
|
|
|
|
|
|
// Simple function reporting
|
|
|
|
if (typeof value === 'function') return '[ Function ]';
|
|
|
|
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
|
|
|
|
if (index = cache.indexOf(value) !== -1) {
|
|
|
|
// If we have it in cache, report the circle with the key we first found it in
|
|
|
|
return '[ Circular {' + (keyMap[index] || 'root') + '} ]';
|
|
|
|
}
|
|
|
|
cache.push(value);
|
|
|
|
keyMap.push(key);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
jasmine.getEnv().addReporter( new PhantomReporter() );
|
|
|
|
}());
|