forked from angular/protractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.js
More file actions
309 lines (274 loc) · 9.31 KB
/
launcher.js
File metadata and controls
309 lines (274 loc) · 9.31 KB
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
/**
* The launcher is responsible for parsing the capabilities from the
* input configuration and launching test runners.
*/
'use strict';
var child = require('child_process'),
ConfigParser = require('./configParser');
var launcherPrefix = '[launcher] ';
var log_ = function(stuff) {
console.log(launcherPrefix + stuff);
};
var noLineLog_ = function(stuff) {
process.stdout.write(launcherPrefix + stuff);
};
/**
* Initialize and run the tests.
*
* @param {string=} configFile
* @param {Object=} additionalConfig
*/
var init = function(configFile, additionalConfig) {
// capabilities = array that holds one array per selected capability (Chrome, FF, Canary, ...)
// This array holds all the instances of the capability
var capabilities = [],
launcherExitCode = 0,
allSpecs,
excludes;
var configParser = new ConfigParser();
if (configFile) {
configParser.addFileConfig(configFile);
}
if (additionalConfig) {
configParser.addConfig(additionalConfig);
}
var config = configParser.getConfig();
var countDriverInstances = function() {
var count = 0;
capabilities.forEach(function(capabilityDriverInstances) {
count += capabilityDriverInstances.length;
});
return count;
};
var countRunningDriverInstances = function() {
var count = 0;
capabilities.forEach(function(capabilityDriverInstances) {
capabilityDriverInstances.forEach(function(driverInstance) {
if (!driverInstance.done) {
count += 1;
}
});
});
return count;
};
var listRemainingForks = function() {
var remaining = countRunningDriverInstances();
if (remaining) {
noLineLog_(remaining + ' instance(s) of WebDriver still running');
}
};
var logSummary = function() {
capabilities.forEach(function(capabilityDriverInstances) {
capabilityDriverInstances.forEach(function(driverInstance) {
var shortChildName = driverInstance.capability.browserName +
(driverInstance.runNumber ? ' #' + driverInstance.runNumber : '');
if (driverInstance.failedCount) {
log_(shortChildName + ' failed ' + driverInstance.failedCount + ' test(s)');
} else {
log_(shortChildName + ' passed');
}
});
});
};
if (config.multiCapabilities.length) {
if (config.debug) {
throw new Error('Cannot run in debug mode with multiCapabilities');
}
log_('Running using config.multiCapabilities - ' +
'config.capabilities will be ignored');
}
// Use capabilities if multiCapabilities is empty.
if (!config.multiCapabilities.length) {
config.multiCapabilities = [config.capabilities];
}
var Fork = function(configFile, additionalConfig, capability, specs, runNumber, single) {
var silent = single ? false: true;
this.configFile = configFile;
this.additionalConfig = additionalConfig;
this.capability = capability;
this.runNumber = runNumber;
this.single = single;
this.specs = specs;
this.process = child.fork(
__dirname + '/runFromLauncher.js',
process.argv.slice(2),{
cwd: process.cwd(),
silent: silent
}
);
};
Fork.prototype.reportHeader_ = function() {
var capability = this.capability;
var eol = require('os').EOL;
var outputHeader = eol + '------------------------------------' + eol;
outputHeader += 'PID: ' + this.process.pid + ' (capability: ';
outputHeader += (capability.browserName) ?
capability.browserName : '';
outputHeader += (capability.version) ?
capability.version : '';
outputHeader += (capability.platform) ?
capability.platform : '';
outputHeader += (this.runNumber) ?
' #' + this.runNumber : '';
outputHeader += ')' + eol;
if (config.splitTestsBetweenCapabilities) {
outputHeader += 'Specs: '+ this.specs.toString() + eol;
}
outputHeader += '------------------------------------' + eol;
console.log(outputHeader);
};
// If we're launching multiple runners, aggregate output until completion.
// Otherwise, there is a single runner, let's pipe the output straight
// through to maintain realtime reporting.
Fork.prototype.addEventHandlers = function(testsDoneCallback) {
var self = this;
if (this.single) {
this.process.on('error', function(err) {
log_('Runner Process(' + self.process.pid + ') Error: ' + err);
});
this.process.on('message', function(m) {
switch (m.event) {
case 'testsDone':
this.failedCount = m.failedCount;
break;
}
});
this.process.on('exit', function(code) {
if (code) {
log_('Runner Process Exited With Error Code: ' + code);
launcherExitCode = 1;
}
});
} else {
// Multiple capabilities and/or instances
this.output = '';
// stdin pipe
this.process.stdout.on('data', function(chunk) {
self.output += chunk;
});
// stderr pipe
this.process.stderr.on('data', function(chunk) {
self.output += chunk;
});
this.process.on('message', function(m) {
switch (m.event) {
case 'testPass':
process.stdout.write('.');
break;
case 'testFail':
process.stdout.write('F');
break;
case 'testsDone':
self.failedCount = m.failedCount;
if (typeof testsDoneCallback === 'function') {
testsDoneCallback();
}
break;
}
});
// err handler
this.process.on('error', function(err) {
log_('Runner Process(' + self.process.pid + ') Error: ' + err);
});
// exit handlers
this.process.on('exit', function(code) {
if (code) {
log_('Runner Process Exited With Error Code: ' + code);
launcherExitCode = 1;
}
self.reportHeader_();
console.log(self.output);
self.done = true;
listRemainingForks();
});
}
};
Fork.prototype.run = function() {
this.process.send({
command: 'run',
configFile: this.configFile,
additionalConfig: this.additionalConfig,
capability: this.capability,
specs: this.specs
});
};
excludes = ConfigParser.resolveFilePatterns( config.exclude, true, config.configDir);
allSpecs = ConfigParser.resolveFilePatterns(
ConfigParser.getSpecs(config), false, config.configDir).filter(function(path) {
return excludes.indexOf(path) < 0;
});
// If there is a single capability with a single instance, avoid starting a separate process
// and print output directly.
// Otherwise, if we're launching multiple runners, aggregate output until
// completion.
if (config.multiCapabilities.length === 1
&& (config.multiCapabilities[0].count === 1 || !config.multiCapabilities[0].count)) {
var Runner = require('./runner');
capabilities[0] = [{
capability: config.multiCapabilities[0],
runNumber: 0
}];
config.capabilities = capabilities[0][0].capability;
config.specs = allSpecs;
var runner = new Runner(config);
runner.run().then(function(exitCode) {
process.exit(exitCode);
}).catch(function(err) {
log_('Error: ' + err.message);
process.exit(1);
});
runner.on('testsDone', function(failedCount) {
capabilities[0][0].failedCount = failedCount;
});
} else {
// Loop over different capabilities in config file
// Make array for each of them in capabilities array
// Push driverInstances into this array, each with one spec (file!)
// When a driverInstance finishes it's spec, it will create a new driver (Fork)
// with the next spec in line
config.multiCapabilities.forEach(function(capability, index) {
var forksCounter = 0;
capability.count = capability.count || 1;
capabilities[index] = []; // Matrix: Dim 1: Capabilities, Dim 2: Instances of capability
if (allSpecs.length < capability.count && config.splitTestsBetweenCapabilities) {
// When we split the specs over multiple instances,
// we can have maximum as many instances as we have specs
capability.count = allSpecs.length;
}
while(forksCounter < capability.count) {
if (config.splitTestsBetweenCapabilities) {
var createAndRunPartialSpecFork = function() {
var specs = allSpecs[forksCounter];
if (!specs) {
return;
}
var fork = new Fork(configFile,additionalConfig,
capability,[specs],forksCounter+1,false);
capabilities[index].push(fork);
fork.run();
fork.addEventHandlers(createAndRunPartialSpecFork);
forksCounter++;
};
createAndRunPartialSpecFork();
} else {
var fork = new Fork(configFile, additionalConfig,
capability, allSpecs, forksCounter+1, false);
capabilities[index].push(fork);
fork.run();
fork.addEventHandlers();
forksCounter++;
}
}
});
}
noLineLog_('Running ' + countDriverInstances() +
' instances of WebDriver');
process.on('exit', function(code) {
if (code) {
launcherExitCode = code;
}
logSummary();
process.exit(launcherExitCode);
});
};
exports.init = init;