-
Notifications
You must be signed in to change notification settings - Fork 404
/
browser_test_runner.js
309 lines (255 loc) · 8.1 KB
/
browser_test_runner.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
'use strict';
const log = require('npmlog');
const BrowserTapConsumer = require('../browser_tap_consumer');
const Bluebird = require('bluebird');
const LogEntry = require('../utils/log-entry');
const toResult = require('./to-result');
module.exports = class BrowserTestRunner {
constructor(launcher, reporter, index, singleRun, config) {
this.launcher = launcher;
this.reporter = reporter;
this.running = false;
this.config = config;
this.index = index;
this.launcherId = this.launcher.id;
this.singleRun = singleRun;
this.logs = [];
this.currentTestContext = {};
this.disconnectCount = 0;
this.pendingTimer = undefined;
this.onProcessExitTimer = undefined;
this.exitRequested = false;
}
start(onFinish) {
if (this.pending) {
return;
}
this.finished = false;
this.pending = true;
return new Bluebird.Promise(resolve => {
this.onFinish = resolve;
if (this.socket) {
this.socket.emit('start-tests');
} else {
this.launcher.start().then(browserProcess => {
this.process = browserProcess;
this.process.on('processExit', this.onProcessExit.bind(this));
this.process.on('processError', this.onProcessError.bind(this));
this.setupStartTimer();
}).catch(err => {
this.onProcessError(err);
});
}
}).asCallback(onFinish);
}
stop(cb) {
if (this.socket) {
this.socket.emit('stop-run');
}
return Bluebird.resolve().asCallback(cb);
}
exit() {
if (!this.process) {
return Bluebird.resolve();
}
// mark that somebody (generally app.js) has requested that the
// test runner exit. This is not an unexpected exit--this setting
// is to tell the processExit handler that.
this.exitRequested = true;
log.info(`Closing browser ${this.name()}.`);
return this.process.kill().then(() => {
this.process = null;
});
}
setupStartTimer() {
this.startTimer = setTimeout(() => {
if (this.finished || !this.pending) {
return;
}
let err = new Error(
`Browser failed to connect within ${this.launcher.config.get('browser_start_timeout')}s. testem.js not loaded?`
);
this.reportResults(err, 0);
}, this.launcher.config.get('browser_start_timeout') * 1000);
}
tryAttach(browser, id, socket) {
if (id !== this.launcherId) {
return false;
} else if (this.disconnectCount > this.launcher.config.get('browser_reconnect_limit')) {
log.info(`socket reconnection limit has been exceeded, not attaching new socket for browser ${browser} and id ${id}`);
return false;
}
log.info('tryAttach', browser, id);
if (this.startTimer) {
clearTimeout(this.startTimer);
}
if (this.pendingTimer) {
clearTimeout(this.pendingTimer);
}
this.pending = false;
this.socket = socket;
this.browser = browser;
this.logs = [];
this.onStart.call(this);
socket.on('tests-start', this.onTestsStart.bind(this));
socket.on('test-result', this.onTestResult.bind(this));
socket.on('test-metadata', this.onTestMetadata.bind(this));
socket.on('top-level-error', this.onGlobalError.bind(this));
socket.on('browser-console', function(/* ...args */) {
let args = Array.prototype.slice.call(arguments);
let type = args.shift();
let message = args.join(' ');
this.logs.push(
new LogEntry(type, message)
);
}.bind(this));
socket.on('disconnect', this.onDisconnect.bind(this));
socket.on('all-test-results', this.onAllTestResults.bind(this));
socket.on('after-tests-complete', this.onAfterTests.bind(this));
const customBrowserSocketEvents = this.launcher.config.get('custom_browser_socket_events');
if (customBrowserSocketEvents) {
Object.keys(customBrowserSocketEvents).forEach((key) => {
socket.on(key, customBrowserSocketEvents[key].bind(this));
});
}
let tap = new BrowserTapConsumer(socket);
tap.on('test-result', this.onTestResult.bind(this));
tap.on('all-test-results', this.onAllTestResults.bind(this));
tap.on('all-test-results', () => {
this.socket.emit('tap-all-test-results');
});
return true;
}
name() {
return this.launcher.name;
}
reportResults(err, code, browserProcess) {
browserProcess = browserProcess || this.process;
let result = toResult(this.launcherId, err, code, browserProcess, this.config, this.currentTestContext);
this.reporter.report(this.launcher.name, result);
this.finish();
}
onTestsStart(testData) {
if (testData) {
this.currentTestContext = testData;
this.currentTestContext.state = 'executing';
}
}
onTestResult(result) {
let errItems = (result.items || [])
.filter(item => !item.passed);
this.reporter.report(this.browser, {
passed: !result.failed && !result.skipped,
name: result.name,
skipped: result.skipped,
runDuration: result.runDuration,
logs: this.logs,
error: errItems[0],
launcherId: this.launcherId,
failed: result.failed,
pending: result.pending,
items: result.items,
originalResultObj: result
});
this.logs = [];
this.currentTestContext.state = 'complete';
}
onTestMetadata(tag, metadata) {
this.reporter.reportMetadata(tag, metadata);
}
onStart() {
this.reporter.onStart(this.browser, {
launcherId: this.launcherId
});
}
onEnd() {
this.reporter.onEnd(this.browser, {
launcherId: this.launcherId
});
}
onAllTestResults() {
log.info(`Browser ${this.name()} finished all tests.`, this.singleRun);
this.onEnd();
}
onAfterTests() {
this.finish();
}
onGlobalError(msg, url, line) {
let message = `${msg} at ${url}, line ${line}\n`;
this.logs.push(
new LogEntry('error', message, this.currentTestContext)
);
if (this.currentTestContext && this.currentTestContext.name) {
if (this.currentTestContext.state === 'executing') {
message = `Global error: ${msg} at ${url}, line ${line}\n While executing test: ${this.currentTestContext.name}\n`;
} else if (this.currentTestContext.state === 'complete') {
message = `Global error: ${msg} at ${url}, line ${line}\n After execution of test: ${this.currentTestContext.name}\n`;
}
} else {
message = `Global error: ${msg} at ${url}, line ${line}\n`;
}
let config = this.launcher.config;
if (config.get('bail_on_uncaught_error')) {
this.onTestResult.call(this, {
failed: 1,
name: message,
logs: [],
error: {}
});
this.onAllTestResults();
this.onEnd.call(this);
}
}
onDisconnect() {
this.socket = null;
if (this.finished) { return; }
this.pending = true;
this.disconnectCount += 1;
const timeout = this.launcher.config.get('browser_disconnect_timeout');
this.pendingTimer = setTimeout(() => {
if (this.finished) {
return;
}
this.reportResults(new Error(`Browser timeout exceeded: ${timeout}s`), 0);
}, timeout * 1000);
}
onProcessExit(code) {
let browserProcess = this.process;
this.process = null;
if (this.finished) { return; }
this.onProcessExitTimer = setTimeout(() => {
if (this.finished) {
return;
}
this.reportResults(new Error(this.exitRequested
? 'Browser exited on request from test driver'
: 'Browser exited unexpectedly'),
code,
browserProcess);
}, 1000);
}
onProcessError(err) {
let browserProcess = this.process;
this.process = null;
if (this.finished) { return; }
this.reportResults(err, 0, browserProcess);
}
finish() {
if (this.finished) { return; }
clearTimeout(this.pendingTimer);
clearTimeout(this.onProcessExitTimer);
this.finished = true;
if (!this.singleRun) {
if (this.onFinish) {
this.onFinish();
}
return;
}
return this.exit().then(() => {
// TODO: Not sure how this can happen, but sometimes onFinish is not defined
if (this.onFinish) {
this.onFinish();
}
});
}
};