UNPKG

qunit-harness

Version:

A library for running qunit tests on a local machine and in the SauceLabs environment.

259 lines (195 loc) 8.47 kB
'use strict'; var _createClass = require('babel-runtime/helpers/create-class').default; var _classCallCheck = require('babel-runtime/helpers/class-call-check').default; var _regeneratorRuntime = require('babel-runtime/regenerator').default; var _Promise = require('babel-runtime/core-js/promise').default; exports.__esModule = true; var _testcafeBrowserTools = require('testcafe-browser-tools'); var _child_process = require('child_process'); var MAX_JOB_RESTART_COUNT = 3; //Job var Job = (function () { function Job(options, browser, testServer) { _classCallCheck(this, Job); this.options = options; this.options.timeout *= 1e3; this.id = browser.browserName; this.browserInfo = browser.browserInfo; this.testServer = testServer; this.status = Job.STATUSES.INITIALIZED; this.restartCount = 0; this.startTestsTime = null; } Job.prototype._getTestResult = function _getTestResult() { return _regeneratorRuntime.async(function _getTestResult$(context$2$0) { var _this = this; while (1) switch (context$2$0.prev = context$2$0.next) { case 0: return context$2$0.abrupt('return', new _Promise(function (resolve) { _this.testServer.on('taskDone', function callee$3$0(results) { return _regeneratorRuntime.async(function callee$3$0$(context$4$0) { while (1) switch (context$4$0.prev = context$4$0.next) { case 0: if (results.id === this.sessionID) resolve(results); case 1: case 'end': return context$4$0.stop(); } }, null, _this); }); })); case 1: case 'end': return context$2$0.stop(); } }, null, this); }; Job.prototype._getJobResult = function _getJobResult() { var testResult; return _regeneratorRuntime.async(function _getJobResult$(context$2$0) { while (1) switch (context$2$0.prev = context$2$0.next) { case 0: this.status = Job.STATUSES.IN_PROGRESS; this.startTestsTime = new Date(); context$2$0.next = 4; return _regeneratorRuntime.awrap(this._getTestResult()); case 4: testResult = context$2$0.sent; this.status = Job.STATUSES.COMPLETED; return context$2$0.abrupt('return', { platform: this.id, result: testResult, job_id: this.sessionID }); case 7: case 'end': return context$2$0.stop(); } }, null, this); }; Job.prototype._reportError = function _reportError(error) { console.log('The task (' + this.id + ') failed: ' + error); }; Job.prototype._runWorker = function _runWorker() { var promiseResolver; return _regeneratorRuntime.async(function _runWorker$(context$2$0) { var _this2 = this; while (1) switch (context$2$0.prev = context$2$0.next) { case 0: promiseResolver = null; context$2$0.prev = 1; this.testServer.on('startedWorker', function (browserName, id) { if (_this2.id === browserName) _this2.sessionID = id; promiseResolver(); }); context$2$0.next = 5; return _regeneratorRuntime.awrap(_testcafeBrowserTools.open(this.browserInfo, this.options.startUrl[0] + '?browserName=' + encodeURIComponent(this.id))); case 5: return context$2$0.abrupt('return', new _Promise(function (resolve) { promiseResolver = resolve; })); case 8: context$2$0.prev = 8; context$2$0.t0 = context$2$0['catch'](1); console.log(context$2$0.t0); case 11: case 'end': return context$2$0.stop(); } }, null, this, [[1, 8]]); }; Job.prototype.run = function run() { var jobResult, jobFailed; return _regeneratorRuntime.async(function run$(context$2$0) { while (1) switch (context$2$0.prev = context$2$0.next) { case 0: jobResult = null; jobFailed = false; this.status = Job.STATUSES.INIT_BROWSER; context$2$0.prev = 3; context$2$0.next = 6; return _regeneratorRuntime.awrap(this._runWorker()); case 6: context$2$0.next = 12; break; case 8: context$2$0.prev = 8; context$2$0.t0 = context$2$0['catch'](3); this._reportError('An error occurred while the browser was being initialized: ' + context$2$0.t0); jobFailed = true; case 12: if (jobFailed) { context$2$0.next = 31; break; } context$2$0.prev = 13; context$2$0.next = 16; return _regeneratorRuntime.awrap(this._getJobResult()); case 16: jobResult = context$2$0.sent; context$2$0.next = 23; break; case 19: context$2$0.prev = 19; context$2$0.t1 = context$2$0['catch'](13); this._reportError(context$2$0.t1); jobFailed = true; case 23: context$2$0.prev = 23; context$2$0.next = 26; return _regeneratorRuntime.awrap(_testcafeBrowserTools.close('QUnitReport#' + this.sessionID)); case 26: context$2$0.next = 31; break; case 28: context$2$0.prev = 28; context$2$0.t2 = context$2$0['catch'](23); this._reportError('An error occurred while the browser was being closed: ' + context$2$0.t2); case 31: if (!jobFailed) { context$2$0.next = 41; break; } if (!(++this.restartCount < MAX_JOB_RESTART_COUNT)) { context$2$0.next = 39; break; } console.log('Attempt ' + this.restartCount + ' to restart the task(' + this.id + ')'); context$2$0.next = 36; return _regeneratorRuntime.awrap(this.run()); case 36: jobResult = context$2$0.sent; context$2$0.next = 41; break; case 39: jobResult = { platform: this.id, job_id: this.sessionID }; this.status = Job.STATUSES.FAILED; case 41: return context$2$0.abrupt('return', jobResult); case 42: case 'end': return context$2$0.stop(); } }, null, this, [[3, 8], [13, 19], [23, 28]]); }; Job.prototype.getStatus = function getStatus() { return this.status; }; _createClass(Job, null, [{ key: 'STATUSES', value: { INIT_BROWSER: 'init browser', INITIALIZED: 'initialized', IN_PROGRESS: 'in progress', COMPLETED: 'completed', FAILED: 'failed' }, enumerable: true }]); return Job; })(); exports.default = Job; module.exports = exports.default;