add some error handling & customizable poll frequency

This commit is contained in:
Román Jiménez 2017-08-28 19:03:14 +02:00
parent 7dcf2425de
commit 9aec3c2cd8

View File

@ -76,6 +76,7 @@ module.exports = function(grunt) {
var options = this.options({ var options = this.options({
browser: 'phantomjs', browser: 'phantomjs',
headless: false, headless: false,
chromePoll: 100,
version: '2.2.0', version: '2.2.0',
timeout: 10000, timeout: 10000,
styles: [], styles: [],
@ -157,6 +158,9 @@ module.exports = function(grunt) {
function chromeRunner(options, cb) { function chromeRunner(options, cb) {
var file = __fileOrHost(options); var file = __fileOrHost(options);
var intervalId;
var lastEvent;
var exitReason = null;
var message = 'Testing Jasmine specs via Chrome'; var message = 'Testing Jasmine specs via Chrome';
@ -171,43 +175,59 @@ module.exports = function(grunt) {
headless: options.headless, headless: options.headless,
}); });
var emitChromeEvent = function (e) {
phantomjs.emit(e.type, e.payload);
};
function consume (tab) { function consume (tab) {
return tab.evaluate(function () { return tab.evaluate('function () { return ChromeQueue.splice(0, ChromeQueue.length); }');
return ChromeQueue.splice(0, ChromeQueue.length); // jshint ignore:line }
});
function somethingWrong (what) {
cb(what.message, status);
} }
// All of this would be much more straightforward with async/await // All of this would be much more straightforward with async/await
browser.init() browser.init()
.then(function (browser) { return browser.newTab(); }) .then(function (browser) {
return browser.newTab()
.then(function (tab) { .then(function (tab) {
lastEvent = new Date();
return tab.goTo(file) return tab.goTo(file)
// Return a promise that resolves when we get the FINISHED event from our ChromeReporter // Return a promise that resolves when we get the FINISHED event from our ChromeReporter
.then(function () { .then(function () {
return new Promise(function (resolve) { return new Promise(function (resolve) {
// Periodically evaluate // Periodically evaluate
var interval = setInterval(function () { intervalId = setInterval(function () {
var date = new Date();
consume(tab) consume(tab)
.then(function (result) { .then(function (result) {
var events = result.result.value; var events = result.result.value;
events.forEach(function (e) { if (!events || events.length === 0) {
phantomjs.emit(e.type, e.payload); if (date - lastEvent > options.timeout) {
}); clearInterval(intervalId);
// Exit condition exitReason = 'Chrome has timed out';
if (events.length > 0 && events[events.length - 1].type === 'jasmine.jasmineDone') {
clearInterval(interval);
resolve(); resolve();
} }
} else {
lastEvent = date;
events.forEach(emitChromeEvent);
// Exit condition
if (events.length > 0 && events[events.length - 1].type === 'jasmine.jasmineDone') {
clearInterval(intervalId);
resolve();
}
}
}); });
}, 100); }, options.chromePoll);
}); });
}) })
.then(function () { .then(function () {
return browser.close(); return browser.close();
}) })
.then(function () { cb(false, status); }); .then(function () { cb(exitReason, status); });
} });
); }, somethingWrong);
} }
function phantomRunner(options, cb) { function phantomRunner(options, cb) {