-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
374 lines (319 loc) · 9.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
'use strict';
var fs = require('fs');
var path = require('path');
var gs = require('glob-stream');
var through = require('through2');
var gutil = require('gulp-util');
var SauceLabs = require('saucelabs');
var SauceTunnel = require('sauce-tunnel');
var selenium = require('selenium-standalone');
var webdriverio = require('webdriverio');
var webdriverjsAngular = require('webdriverjs-angular');
var http = require('http');
var async = require('async');
var merge = require('deepmerge');
var phantomjs = require('phantomjs-prebuilt');
var server = null;
var isSeleniumServerRunning = false;
var seleniumServer = null;
var tunnel = null;
var isSauceTunnelRunning = false;
var Jasmine = require('jasmine');
var jasmineReporters = require('jasmine-reporters');
var Reporter = require('jasmine-terminal-reporter');
var SpecReporter = require('jasmine-spec-reporter');
module.exports = function (args) {
args = typeof args === 'undefined' ? {configFile: undefined, args: {}} : args;
var configs = typeof args.configFile === 'undefined' ? {} : require(args.configFile).config;
var argsArgs = typeof args.args === 'undefined' ? {} : args.args;
var mergedOptions = merge(configs, argsArgs);
var options = mergedOptions || {},
sessionID = null,
seleniumOptions = merge((options.seleniumOptions || {}), {
host: 'localhost',
port: 4444,
role: 'hub',
path: '/wd/hub/status'
}),
seleniumInstallOptions = options.seleniumInstallOptions,
tunnelIdentifier = options.desiredCapabilities && options.desiredCapabilities['tunnel-identifier'] ? options.desiredCapabilities['tunnel-identifier'] : null,
tunnelFlags = options.desiredCapabilities && options.desiredCapabilities['tunnel-flags'] ? options.desiredCapabilities['tunnel-flags'] : [];
// Setup phantomjs binary path manually
if (typeof options.desiredCapabilities != 'undefined'
&& options.desiredCapabilities.browserName == 'phantomjs') {
options.desiredCapabilities['phantomjs.binary.path'] = phantomjs.path
}
/**
* initialise tunnel
*/
if (!tunnel && options.user && options.key && tunnelIdentifier) {
gutil.log('initialise test session using sauce tunnel from user ' + options.user);
tunnel = new SauceTunnel(options.user, options.key, tunnelIdentifier, true, tunnelFlags);
tunnel.on('verbose:debug', gutil.log);
seleniumOptions.host = undefined;
seleniumOptions.port = 4445;
}
/**
* initialize WebdriverIO
*/
gutil.log('run webdriverio with following capabilities: ' + JSON.stringify(options));
options.logLevel = options.quiet ? 'silent' : options.logLevel;
GLOBAL.browser = typeof options.ngRoot === 'undefined' ? webdriverio.remote(options) : webdriverjsAngular.remote(options);
/**
* initialize Jasmine
*/
var jasmine = new Jasmine();
if (options.timeout) {
jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = options.timeout;
}
var color = process.argv.indexOf('--no-color') === -1;
var reporter = options.reporter;
if (reporter) {
(Array.isArray(reporter) ? reporter : [reporter]).forEach(function (el) {
jasmine.addReporter(getReporter(el));
});
} else {
jasmine.addReporter(new Reporter({
isVerbose: options.verbose,
showColors: color,
includeStackTrace: options.includeStackTrace || false
}));
}
/**
* Returns relevant jasmine reporter instance
* @param type
* @returns {*}
*/
function getReporter(type) {
var reporter;
switch (type.name) {
case 'XUnit':
reporter = new jasmineReporters.JUnitXmlReporter(type.options);
break;
case 'Terminal':
reporter = new Reporter(type.options);
break;
case 'Spec':
reporter = new SpecReporter(type.options);
break;
case 'Dot':
reporter = new jasmineReporters.TerminalReporter(type.options);
break;
}
return reporter;
}
/**
* helper function for asyncjs
*/
var next = function (cb, param) {
return function () {
var args = Array.prototype.slice.call(arguments, 1);
if (typeof param !== 'undefined') {
args.unshift(param);
} else if (arguments.length === 1) {
args.unshift(arguments[0]);
}
args.unshift(null);
cb.apply(null, args);
};
};
/**
* check if selenium server is already running
*/
var pingSelenium = function (callback) {
if (tunnel) {
return callback(null);
}
gutil.log('checking if selenium is running');
return http.get(seleniumOptions, function (res) {
gutil.log('selenium is running');
isSeleniumServerRunning = true;
// Gulp seems to hang when HTTP requests are made, and that's how selenium is queried.
// https://github.com/gulpjs/gulp/issues/167
res.on('end', function () {
return callback(null);
});
res.resume();
}).on('error', function () {
gutil.log('selenium is not running');
isSeleniumServerRunning = false;
return callback(null);
});
};
/**
* install drivers if needed
*/
var installDrivers = function (callback) {
if(typeof seleniumInstallOptions === 'undefined') {
return callback(null);
}
if (tunnel || isSeleniumServerRunning) {
return callback(null);
}
gutil.log('installing driver if needed');
selenium.install(seleniumInstallOptions, function (err) {
if (err) {
return callback(err);
}
gutil.log('driver installed');
return callback(null);
});
};
/**
* start selenium server or sauce tunnel (if not already started)
*/
var startServer = function (callback) {
if (tunnel) {
if (isSauceTunnelRunning) {
gutil.log('sauce tunnel is already running');
return callback(null, isSauceTunnelRunning);
}
gutil.log('start sauce tunnel');
/**
* start sauce tunnel
*/
tunnel.start(function (hasTunnelStarted) {
// output here means if tunnel was created successfully
if (hasTunnelStarted === false) {
callback(new Error('Sauce-Tunnel couldn\'t created successfully'));
}
gutil.log('tunnel created successfully');
isSauceTunnelRunning = true;
return callback(null, isSauceTunnelRunning);
});
} else if (!server && !isSeleniumServerRunning && !options.nospawn) {
gutil.log('start selenium standalone server');
/**
* starts selenium standalone server if its not running
*/
server = selenium.start({seleniumArgs :seleniumOptions}, function (err, child) {
if (err) {
return callback(err);
}
gutil.log('selenium successfully started');
seleniumServer = child;
isSeleniumServerRunning = true;
return callback(null, true);
});
} else {
gutil.log('standalone server or sauce tunnel is running');
return callback(null, true);
}
};
var initWebdriver = function () {
var callback = arguments[arguments.length - 1];
gutil.log('init WebdriverIO instance');
GLOBAL.browser.init(function (err) {
/**
* Allow for adding custom commands
*/
if ('function' === typeof args.applyExtensions) {
args.applyExtensions.call(null, this);
}
/**
* gracefully kill process if init fails
*/
callback(err);
});
};
var runJasmine = function (callback) {
gutil.log('run jasmine tests');
/**
* save session ID
*/
sessionID = GLOBAL.browser.requestHandler.sessionID;
jasmine.onComplete(next(callback));
jasmine.execute();
};
var endSeleniumSession = function (callback) {
if (GLOBAL.browser) {
// Close Remote sessions if needed
return GLOBAL.browser.end(function () {
gutil.log('ended selenium session');
callback();
});
} else {
return callback();
}
};
/**
* destroy sauce tunnel if connected (once all tasks were executed)
*/
var killSauceTunnel = function (done) {
if (isSauceTunnelRunning) {
gutil.log('destroy sauce tunnel if connected (once all tasks were executed)');
return tunnel.stop(function () {
gutil.log('tunnel closed successfully');
done();
});
}
return done();
};
/**
* kill selenium server process if created
* @param callback
* @returns {*}
*/
var killSeleniumServer = function (callback) {
if (seleniumServer) {
gutil.log('killing selenium server');
seleniumServer.kill();
}
return callback();
};
/**
* update job on Sauce Labs
*/
var updateSauceJob = function (result) {
var callback = arguments[arguments.length - 1];
gutil.log('update job on Sauce Labs');
var sauceAccount = new SauceLabs({
username: options.user,
password: options.key
});
sauceAccount.updateJob(sessionID, {
passed: result,
public: true
}, next(callback, result));
};
var runWebdriverIOTests = function (callback) {
var stream = this;
var tasks = [
pingSelenium.bind(stream),
installDrivers.bind(stream),
startServer.bind(stream),
initWebdriver.bind(stream),
runJasmine.bind(stream)
];
if (options.updateSauceJob) {
tasks.push(updateSauceJob.bind(stream));
}
async.waterfall(tasks, function (err) {
//if no error happened and no test failures, we are good
if (err) {
stream.emit('error', new gutil.PluginError('gulp-jasmine-webdriverio', err));
}
async.waterfall([
endSeleniumSession.bind(stream),
killSauceTunnel.bind(stream),
killSeleniumServer.bind(stream)
], callback);
});
return stream;
};
function addFileToJasmine(file, enc, callback) {
this.push(file);
jasmine.addSpecFile(file.path);
callback();
}
var outputStream;
if(typeof options.specs === 'undefined' || options.specs == '' || options.specs.length === 0){
outputStream = through.obj(addFileToJasmine, runWebdriverIOTests);
}
else{
outputStream = gs.create(options.specs);
return outputStream
.pipe(through.obj(addFileToJasmine, runWebdriverIOTests));
}
return outputStream;
};