4 /** Environment shortcut. */
7 if (env.TRAVIS_SECURE_ENV_VARS == 'false') {
8 console.log('Skipping Sauce Labs jobs; secure environment variables are unavailable');
12 /** Load Node.js modules. */
13 var EventEmitter = require('events').EventEmitter,
14 http = require('http'),
15 path = require('path'),
17 util = require('util');
19 /** Load other modules. */
20 var _ = require('../lodash.js'),
21 chalk = require('chalk'),
22 ecstatic = require('ecstatic'),
23 request = require('request'),
24 SauceTunnel = require('sauce-tunnel');
26 /** Used for Sauce Labs credentials. */
27 var accessKey = env.SAUCE_ACCESS_KEY,
28 username = env.SAUCE_USERNAME;
30 /** Used as the default maximum number of times to retry a job and tunnel. */
31 var maxJobRetries = 3,
34 /** Used as the static file server middleware. */
35 var mount = ecstatic({
40 /** Used as the list of ports supported by Sauce Connect. */
42 80, 443, 888, 2000, 2001, 2020, 2109, 2222, 2310, 3000, 3001, 3030, 3210,
43 3333, 4000, 4001, 4040, 4321, 4502, 4503, 4567, 5000, 5001, 5050, 5555, 5432,
44 6000, 6001, 6060, 6666, 6543, 7000, 7070, 7774, 7777, 8000, 8001, 8003, 8031,
45 8080, 8081, 8765, 8777, 8888, 9000, 9001, 9080, 9090, 9876, 9877, 9999, 49221,
49 /** Used by `logInline` to clear previously logged messages. */
52 /** Method shortcut. */
53 var push = Array.prototype.push;
55 /** Used to detect error messages. */
56 var reError = /(?:\be|E)rror\b/;
58 /** Used to detect valid job ids. */
59 var reJobId = /^[a-z0-9]{32}$/;
61 /** Used to display the wait throbber. */
62 var throbberDelay = 500,
66 * Used as Sauce Labs config values.
67 * See the [Sauce Labs documentation](https://docs.saucelabs.com/reference/test-configuration/)
70 var advisor = getOption('advisor', false),
71 build = getOption('build', (env.TRAVIS_COMMIT || '').slice(0, 10)),
72 commandTimeout = getOption('commandTimeout', 90),
73 compatMode = getOption('compatMode', null),
74 customData = Function('return {' + getOption('customData', '').replace(/^\{|}$/g, '') + '}')(),
75 deviceOrientation = getOption('deviceOrientation', 'portrait'),
76 framework = getOption('framework', 'qunit'),
77 idleTimeout = getOption('idleTimeout', 60),
78 jobName = getOption('name', 'unit tests'),
79 maxDuration = getOption('maxDuration', 180),
80 port = ports[Math.min(_.sortedIndex(ports, getOption('port', 9001)), ports.length - 1)],
81 publicAccess = getOption('public', true),
82 queueTimeout = getOption('queueTimeout', 240),
83 recordVideo = getOption('recordVideo', true),
84 recordScreenshots = getOption('recordScreenshots', false),
85 runner = getOption('runner', 'test/index.html').replace(/^\W+/, ''),
86 runnerUrl = getOption('runnerUrl', 'http://localhost:' + port + '/' + runner),
87 statusInterval = getOption('statusInterval', 5),
88 tags = getOption('tags', []),
89 throttled = getOption('throttled', 10),
90 tunneled = getOption('tunneled', true),
91 tunnelId = getOption('tunnelId', 'tunnel_' + (env.TRAVIS_JOB_ID || 0)),
92 tunnelTimeout = getOption('tunnelTimeout', 120),
93 videoUploadOnPass = getOption('videoUploadOnPass', false);
95 /** Used to convert Sauce Labs browser identifiers to their formal names. */
96 var browserNameMap = {
97 'googlechrome': 'Chrome',
98 'iehta': 'Internet Explorer',
101 'microsoftedge': 'Edge'
104 /** List of platforms to load the runner on. */
106 ['Linux', 'android', '5.1'],
107 ['Windows 10', 'chrome', '50'],
108 ['Windows 10', 'chrome', '49'],
109 ['Windows 10', 'firefox', '46'],
110 ['Windows 10', 'firefox', '45'],
111 ['Windows 10', 'microsoftedge', '13'],
112 ['Windows 10', 'internet explorer', '11'],
113 ['Windows 8', 'internet explorer', '10'],
114 ['Windows 7', 'internet explorer', '9'],
115 // ['OS X 10.10', 'ipad', '9.1'],
116 ['OS X 10.11', 'safari', '9'],
117 ['OS X 10.10', 'safari', '8']
120 /** Used to tailor the `platforms` array. */
121 var isAMD = _.includes(tags, 'amd'),
122 isBackbone = _.includes(tags, 'backbone'),
123 isModern = _.includes(tags, 'modern');
125 // The platforms to test IE compatibility modes.
128 ['Windows 10', 'internet explorer', '11'],
129 ['Windows 8', 'internet explorer', '10'],
130 ['Windows 7', 'internet explorer', '9'],
131 ['Windows 7', 'internet explorer', '8']
134 // The platforms for AMD tests.
136 platforms = _.filter(platforms, function(platform) {
137 var browser = browserName(platform[1]),
138 version = +platform[2];
141 case 'Android': return version >= 4.4;
142 case 'Opera': return version >= 10;
147 // The platforms for Backbone tests.
149 platforms = _.filter(platforms, function(platform) {
150 var browser = browserName(platform[1]),
151 version = +platform[2];
154 case 'Firefox': return version >= 4;
155 case 'Internet Explorer': return version >= 7;
156 case 'iPad': return version >= 5;
157 case 'Opera': return version >= 12;
162 // The platforms for modern builds.
164 platforms = _.filter(platforms, function(platform) {
165 var browser = browserName(platform[1]),
166 version = +platform[2];
169 case 'Android': return version >= 4.1;
170 case 'Firefox': return version >= 10;
171 case 'Internet Explorer': return version >= 9;
172 case 'iPad': return version >= 6;
173 case 'Opera': return version >= 12;
174 case 'Safari': return version >= 6;
180 /** Used as the default `Job` options object. */
183 'command-timeout': commandTimeout,
184 'custom-data': customData,
185 'device-orientation': deviceOrientation,
186 'framework': framework,
187 'idle-timeout': idleTimeout,
188 'max-duration': maxDuration,
190 'public': publicAccess,
191 'platforms': platforms,
192 'record-screenshots': recordScreenshots,
193 'record-video': recordVideo,
194 'sauce-advisor': advisor,
197 'video-upload-on-pass': videoUploadOnPass
200 if (publicAccess === true) {
201 jobOptions['public'] = 'public';
204 jobOptions['tunnel-identifier'] = tunnelId;
207 /*----------------------------------------------------------------------------*/
210 * Resolves the formal browser name for a given Sauce Labs browser identifier.
213 * @param {string} identifier The browser identifier.
214 * @returns {string} Returns the formal browser name.
216 function browserName(identifier) {
217 return browserNameMap[identifier] || _.startCase(identifier);
221 * Gets the value for the given option name. If no value is available the
222 * `defaultValue` is returned.
225 * @param {string} name The name of the option.
226 * @param {*} defaultValue The default option value.
227 * @returns {*} Returns the option value.
229 function getOption(name, defaultValue) {
230 var isArr = _.isArray(defaultValue);
231 return _.reduce(process.argv, function(result, value) {
233 value = optionToArray(name, value);
234 return _.isEmpty(value) ? result : value;
236 value = optionToValue(name, value);
238 return value == null ? result : value;
243 * Checks if `value` is a job ID.
246 * @param {*} value The value to check.
247 * @returns {boolean} Returns `true` if `value` is a job ID, else `false`.
249 function isJobId(value) {
250 return reJobId.test(value);
254 * Writes an inline message to standard output.
257 * @param {string} [text=''] The text to log.
259 function logInline(text) {
260 var blankLine = _.repeat(' ', _.size(prevLine));
261 prevLine = text = _.truncate(text, { 'length': 40 });
262 process.stdout.write(text + blankLine.slice(text.length) + '\r');
266 * Writes the wait throbber to standard output.
270 function logThrobber() {
271 logInline('Please wait' + _.repeat('.', (++waitCount % 3) + 1));
275 * Converts a comma separated option value into an array.
278 * @param {string} name The name of the option to inspect.
279 * @param {string} string The options string.
280 * @returns {Array} Returns the new converted array.
282 function optionToArray(name, string) {
283 return _.compact(_.invokeMap((optionToValue(name, string) || '').split(/, */), 'trim'));
287 * Extracts the option value from an option string.
290 * @param {string} name The name of the option to inspect.
291 * @param {string} string The options string.
292 * @returns {string|undefined} Returns the option value, else `undefined`.
294 function optionToValue(name, string) {
295 var result = string.match(RegExp('^' + name + '(?:=([\\s\\S]+))?$'));
297 result = _.result(result, 1);
298 result = result ? _.trim(result) : true;
300 if (result === 'false') {
303 return result || undefined;
306 /*----------------------------------------------------------------------------*/
309 * The `Job#remove` and `Tunnel#stop` callback used by `Jobs#restart`
310 * and `Tunnel#restart` respectively.
314 function onGenericRestart() {
315 this.restarting = false;
316 this.emit('restart');
321 * The `request.put` and `SauceTunnel#stop` callback used by `Jobs#stop`
322 * and `Tunnel#stop` respectively.
325 * @param {Object} [error] The error object.
327 function onGenericStop(error) {
328 this.running = this.stopping = false;
329 this.emit('stop', error);
333 * The `request.del` callback used by `Jobs#remove`.
337 function onJobRemove(error, res, body) {
338 this.id = this.taskId = this.url = null;
339 this.removing = false;
344 * The `Job#remove` callback used by `Jobs#reset`.
348 function onJobReset() {
350 this.failed = this.resetting = false;
351 this._pollerId = this.id = this.result = this.taskId = this.url = null;
356 * The `request.post` callback used by `Jobs#start`.
359 * @param {Object} [error] The error object.
360 * @param {Object} res The response data object.
361 * @param {Object} body The response body JSON object.
363 function onJobStart(error, res, body) {
364 this.starting = false;
369 var statusCode = _.result(res, 'statusCode'),
370 taskId = _.first(_.result(body, 'js tests'));
372 if (error || !taskId || statusCode != 200) {
373 if (this.attempts < this.retries) {
377 var na = 'unavailable',
378 bodyStr = _.isObject(body) ? '\n' + JSON.stringify(body) : na,
379 statusStr = _.isFinite(statusCode) ? statusCode : na;
382 console.error('Failed to start job; status: %s, body: %s', statusStr, bodyStr);
384 console.error(error);
387 this.emit('complete');
391 this.taskId = taskId;
392 this.timestamp = _.now();
398 * The `request.post` callback used by `Job#status`.
401 * @param {Object} [error] The error object.
402 * @param {Object} res The response data object.
403 * @param {Object} body The response body JSON object.
405 function onJobStatus(error, res, body) {
406 this.checking = false;
408 if (!this.running || this.stopping) {
411 var completed = _.result(body, 'completed', false),
412 data = _.first(_.result(body, 'js tests')),
413 elapsed = (_.now() - this.timestamp) / 1000,
414 jobId = _.result(data, 'job_id', null),
415 jobResult = _.result(data, 'result', null),
416 jobStatus = _.result(data, 'status', ''),
417 jobUrl = _.result(data, 'url', null),
418 expired = (elapsed >= queueTimeout && !_.includes(jobStatus, 'in progress')),
419 options = this.options,
420 platform = options.platforms[0];
422 if (_.isObject(jobResult)) {
423 var message = _.result(jobResult, 'message');
425 if (typeof jobResult == 'string') {
430 if (isJobId(jobId)) {
432 this.result = jobResult;
437 this.emit('status', jobStatus);
439 if (!completed && !expired) {
440 this._pollerId = _.delay(_.bind(this.status, this), this.statusInterval * 1000);
443 var description = browserName(platform[1]) + ' ' + platform[2] + ' on ' + _.startCase(platform[0]),
444 errored = !jobResult || !jobResult.passed || reError.test(message) || reError.test(jobStatus),
445 failures = _.result(jobResult, 'failed'),
446 label = options.name + ':',
447 tunnel = this.tunnel;
449 if (errored || failures) {
450 if (errored && this.attempts < this.retries) {
454 var details = 'See ' + jobUrl + ' for details.';
459 console.error(label + ' %s ' + chalk.red('failed') + ' %d test' + (failures > 1 ? 's' : '') + '. %s', description, failures, details);
461 else if (tunnel.attempts < tunnel.retries) {
466 if (typeof message == 'undefined') {
467 message = 'Results are unavailable. ' + details;
469 console.error(label, description, chalk.red('failed') + ';', message);
474 console.log(label, description, chalk.green('passed'));
476 this.running = false;
477 this.emit('complete');
481 * The `SauceTunnel#start` callback used by `Tunnel#start`.
484 * @param {boolean} success The connection success indicator.
486 function onTunnelStart(success) {
487 this.starting = false;
489 if (this._timeoutId) {
490 clearTimeout(this._timeoutId);
491 this._timeoutId = null;
494 if (this.attempts < this.retries) {
499 console.error('Failed to open Sauce Connect tunnel');
503 console.log('Sauce Connect tunnel opened');
505 var jobs = this.jobs;
506 push.apply(jobs.queue, jobs.all);
511 console.log('Starting jobs...');
515 /*----------------------------------------------------------------------------*/
518 * The Job constructor.
521 * @param {Object} [properties] The properties to initialize a job with.
523 function Job(properties) {
524 EventEmitter.call(this);
527 _.merge(this, properties);
528 _.defaults(this.options, _.cloneDeep(jobOptions));
531 this.checking = this.failed = this.removing = this.resetting = this.restarting = this.running = this.starting = this.stopping = false;
532 this._pollerId = this.id = this.result = this.taskId = this.url = null;
535 util.inherits(Job, EventEmitter);
541 * @param {Function} callback The function called once the job is removed.
542 * @param {Object} Returns the job instance.
544 Job.prototype.remove = function(callback) {
545 this.once('remove', _.iteratee(callback));
549 this.removing = true;
550 return this.stop(function() {
551 var onRemove = _.bind(onJobRemove, this);
556 request.del(_.template('https://saucelabs.com/rest/v1/${user}/jobs/${id}')(this), {
557 'auth': { 'user': this.user, 'pass': this.pass }
566 * @param {Function} callback The function called once the job is reset.
567 * @param {Object} Returns the job instance.
569 Job.prototype.reset = function(callback) {
570 this.once('reset', _.iteratee(callback));
571 if (this.resetting) {
574 this.resetting = true;
575 return this.remove(onJobReset);
582 * @param {Function} callback The function called once the job is restarted.
583 * @param {Object} Returns the job instance.
585 Job.prototype.restart = function(callback) {
586 this.once('restart', _.iteratee(callback));
587 if (this.restarting) {
590 this.restarting = true;
592 var options = this.options,
593 platform = options.platforms[0],
594 description = browserName(platform[1]) + ' ' + platform[2] + ' on ' + _.startCase(platform[0]),
595 label = options.name + ':';
598 console.log('%s %s restart %d of %d', label, description, ++this.attempts, this.retries);
600 return this.remove(onGenericRestart);
607 * @param {Function} callback The function called once the job is started.
608 * @param {Object} Returns the job instance.
610 Job.prototype.start = function(callback) {
611 this.once('start', _.iteratee(callback));
612 if (this.starting || this.running) {
615 this.starting = true;
616 request.post(_.template('https://saucelabs.com/rest/v1/${user}/js-tests')(this), {
617 'auth': { 'user': this.user, 'pass': this.pass },
619 }, _.bind(onJobStart, this));
625 * Checks the status of a job.
628 * @param {Function} callback The function called once the status is resolved.
629 * @param {Object} Returns the job instance.
631 Job.prototype.status = function(callback) {
632 this.once('status', _.iteratee(callback));
633 if (this.checking || this.removing || this.resetting || this.restarting || this.starting || this.stopping) {
636 this._pollerId = null;
637 this.checking = true;
638 request.post(_.template('https://saucelabs.com/rest/v1/${user}/js-tests/status')(this), {
639 'auth': { 'user': this.user, 'pass': this.pass },
640 'json': { 'js tests': [this.taskId] }
641 }, _.bind(onJobStatus, this));
650 * @param {Function} callback The function called once the job is stopped.
651 * @param {Object} Returns the job instance.
653 Job.prototype.stop = function(callback) {
654 this.once('stop', _.iteratee(callback));
658 this.stopping = true;
659 if (this._pollerId) {
660 clearTimeout(this._pollerId);
661 this._pollerId = null;
662 this.checking = false;
664 var onStop = _.bind(onGenericStop, this);
665 if (!this.running || !this.id) {
669 request.put(_.template('https://saucelabs.com/rest/v1/${user}/jobs/${id}/stop')(this), {
670 'auth': { 'user': this.user, 'pass': this.pass }
676 /*----------------------------------------------------------------------------*/
679 * The Tunnel constructor.
682 * @param {Object} [properties] The properties to initialize the tunnel with.
684 function Tunnel(properties) {
685 EventEmitter.call(this);
687 _.merge(this, properties);
692 var all = _.map(this.platforms, _.bind(function(platform) {
693 return new Job(_.merge({
697 'options': { 'platforms': [platform] }
707 _.invokeMap(all, 'on', 'complete', function() {
708 _.pull(active, this);
710 success = !this.failed;
712 if (++completed == total) {
713 tunnel.stop(_.partial(tunnel.emit, 'complete', success));
719 _.invokeMap(all, 'on', 'restart', function() {
720 if (!_.includes(restarted, this)) {
721 restarted.push(this);
723 // Restart tunnel if all active jobs have restarted.
724 var threshold = Math.min(all.length, _.isFinite(throttled) ? throttled : 3);
725 if (tunnel.attempts < tunnel.retries &&
726 active.length >= threshold && _.isEmpty(_.difference(active, restarted))) {
731 this.on('restart', function() {
734 restarted.length = 0;
737 this._timeoutId = null;
739 this.restarting = this.running = this.starting = this.stopping = false;
740 this.jobs = { 'active': active, 'all': all, 'queue': queue };
741 this.connection = new SauceTunnel(this.user, this.pass, this.id, this.tunneled, ['-P', '0']);
744 util.inherits(Tunnel, EventEmitter);
747 * Restarts the tunnel.
750 * @param {Function} callback The function called once the tunnel is restarted.
752 Tunnel.prototype.restart = function(callback) {
753 this.once('restart', _.iteratee(callback));
754 if (this.restarting) {
757 this.restarting = true;
760 console.log('Tunnel %s: restart %d of %d', this.id, ++this.attempts, this.retries);
762 var jobs = this.jobs,
763 active = jobs.active,
766 var reset = _.after(all.length, _.bind(this.stop, this, onGenericRestart)),
767 stop = _.after(active.length, _.partial(_.invokeMap, all, 'reset', reset));
769 if (_.isEmpty(active)) {
772 if (_.isEmpty(all)) {
775 _.invokeMap(active, 'stop', function() {
776 _.pull(active, this);
780 if (this._timeoutId) {
781 clearTimeout(this._timeoutId);
782 this._timeoutId = null;
791 * @param {Function} callback The function called once the tunnel is started.
792 * @param {Object} Returns the tunnel instance.
794 Tunnel.prototype.start = function(callback) {
795 this.once('start', _.iteratee(callback));
796 if (this.starting || this.running) {
799 this.starting = true;
802 console.log('Opening Sauce Connect tunnel...');
804 var onStart = _.bind(onTunnelStart, this);
806 this._timeoutId = _.delay(onStart, this.timeout * 1000, false);
808 this.connection.start(onStart);
813 * Removes jobs from the queue and starts them.
816 * @param {Object} Returns the tunnel instance.
818 Tunnel.prototype.dequeue = function() {
821 active = jobs.active,
823 throttled = this.throttled;
825 while (queue.length && (active.length < throttled)) {
826 var job = queue.shift();
828 _.delay(_.bind(job.start, job), ++count * 1000);
837 * @param {Function} callback The function called once the tunnel is stopped.
838 * @param {Object} Returns the tunnel instance.
840 Tunnel.prototype.stop = function(callback) {
841 this.once('stop', _.iteratee(callback));
845 this.stopping = true;
848 console.log('Shutting down Sauce Connect tunnel...');
850 var jobs = this.jobs,
851 active = jobs.active;
853 var stop = _.after(active.length, _.bind(function() {
854 var onStop = _.bind(onGenericStop, this);
856 this.connection.stop(onStop);
862 jobs.queue.length = 0;
863 if (_.isEmpty(active)) {
866 _.invokeMap(active, 'stop', function() {
867 _.pull(active, this);
871 if (this._timeoutId) {
872 clearTimeout(this._timeoutId);
873 this._timeoutId = null;
878 /*----------------------------------------------------------------------------*/
880 // Cleanup any inline logs when exited via `ctrl+c`.
881 process.on('SIGINT', function() {
886 // Create a web server for the current working directory.
887 http.createServer(function(req, res) {
888 // See http://msdn.microsoft.com/en-us/library/ff955275(v=vs.85).aspx.
889 if (compatMode && path.extname(url.parse(req.url).pathname) == '.html') {
890 res.setHeader('X-UA-Compatible', 'IE=' + compatMode);
895 // Setup Sauce Connect so we can use this server from Sauce Labs.
896 var tunnel = new Tunnel({
900 'job': { 'retries': maxJobRetries, 'statusInterval': statusInterval },
901 'platforms': platforms,
902 'retries': maxTunnelRetries,
903 'throttled': throttled,
904 'tunneled': tunneled,
905 'timeout': tunnelTimeout
908 tunnel.on('complete', function(success) {
909 process.exit(success ? 0 : 1);
914 setInterval(logThrobber, throbberDelay);