forked from DevExpress/testcafe
/
browser-job.ts
246 lines (196 loc) · 9.71 KB
/
browser-job.ts
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
import { pull as remove } from 'lodash';
import AsyncEventEmitter from '../utils/async-event-emitter';
import TestRunController from './test-run-controller';
import SessionController from '../test-run/session-controller';
import BrowserConnection from '../browser/connection';
import { Proxy } from 'testcafe-hammerhead';
import Test from '../api/structure/test';
import Screenshots from '../screenshots';
import WarningLog from '../notifications/warning-log';
import FixtureHookController from './fixture-hook-controller';
import { Dictionary } from '../configuration/interfaces';
import BrowserJobResult from './browser-job-result';
import CompilerService from '../services/compiler/host';
import { BrowserJobInit } from './interfaces';
import MessageBus from '../utils/message-bus';
import TestRunHookController from './test-run-hook-controller';
interface BrowserJobResultInfo {
status: BrowserJobResult;
data?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
enum StartStatus { none, starting, started }
export default class BrowserJob extends AsyncEventEmitter {
private _startStatus: StartStatus;
private _startTime: Date;
private _total: number;
private _passed: number;
private readonly _opts: Dictionary<OptionValue>;
private readonly _proxy: Proxy;
public readonly browserConnections: BrowserConnection[];
private readonly _screenshots: Screenshots;
public readonly warningLog: WarningLog;
public readonly fixtureHookController: FixtureHookController;
private _result: BrowserJobResultInfo | null;
private readonly _testRunControllerQueue: TestRunController[];
private readonly _reportsPending: TestRunController[];
private readonly _connectionErrorListener: (error: Error) => void;
private readonly _completionQueue: TestRunController[];
private _resolveWaitingLastTestInFixture: Function | null;
private readonly _messageBus: MessageBus;
private readonly _testRunHook: TestRunHookController;
public constructor ({
tests,
browserConnections,
proxy,
screenshots,
warningLog,
fixtureHookController,
opts,
compilerService,
messageBus,
}: BrowserJobInit) {
super();
this._startStatus = StartStatus.none;
this._startTime = new Date();
this._total = 0;
this._passed = 0;
this._opts = opts;
this._proxy = proxy;
this.browserConnections = browserConnections;
this._screenshots = screenshots;
this.warningLog = warningLog;
this.fixtureHookController = fixtureHookController;
this._result = null;
this._messageBus = messageBus;
this._testRunHook = new TestRunHookController(tests, (opts.hooks as GlobalHooks)?.testRun);
this._testRunControllerQueue = tests.map((test, index) => this._createTestRunController(test, index, compilerService));
this._completionQueue = [];
this._reportsPending = [];
this._connectionErrorListener = (error: Error) => this._setResult(BrowserJobResult.errored, error);
this._resolveWaitingLastTestInFixture = null;
this.browserConnections.map(bc => bc.once('error', this._connectionErrorListener));
}
private _createTestRunController (test: Test, index: number, compilerService?: CompilerService): TestRunController {
const testRunController = new TestRunController({
test,
index: index + 1,
proxy: this._proxy,
screenshots: this._screenshots,
warningLog: this.warningLog,
fixtureHookController: this.fixtureHookController,
opts: this._opts,
messageBus: this._messageBus,
compilerService,
testRunHook: this._testRunHook,
});
testRunController.on('test-run-create', async testRunInfo => {
await this.emit('test-run-create', testRunInfo);
});
testRunController.on('test-run-ready', async () => {
await this.emit('test-run-ready', testRunController);
});
testRunController.on('test-run-restart', async () => this._onTestRunRestart(testRunController));
testRunController.on('test-run-before-done', async () => {
await this.emit('test-run-before-done', testRunController);
});
testRunController.on('test-run-done', async () => this._onTestRunDone(testRunController));
testRunController.on('test-action-done', async args => {
await this.emit('test-action-done', args);
});
return testRunController;
}
private async _setResult (status: BrowserJobResult, data?: any): Promise<void> { // eslint-disable-line @typescript-eslint/no-explicit-any
if (this._result)
return;
this._result = { status, data };
this.browserConnections.forEach(bc => bc.removeListener('error', this._connectionErrorListener));
await Promise.all(this.browserConnections.map(bc => bc.reportJobResult((this._result as BrowserJobResultInfo).status, (this._result as BrowserJobResultInfo).data)));
}
private _addToCompletionQueue (testRunInfo: TestRunController): void {
this._completionQueue.push(testRunInfo);
}
private _removeFromCompletionQueue (testRunInfo: TestRunController): void {
remove(this._completionQueue, testRunInfo);
}
private async _onTestRunRestart (testRunController: TestRunController): Promise<void> {
this._removeFromCompletionQueue(testRunController);
this._testRunControllerQueue.unshift(testRunController);
await this.emit('test-run-restart', testRunController);
}
private async _onTestRunDone (testRunController: TestRunController): Promise<void> {
this._total++;
if (!testRunController.testRun.errs.length)
this._passed++;
while (this._completionQueue.length && this._completionQueue[0].done) {
testRunController = this._completionQueue.shift() as TestRunController;
await this.emit('test-run-done', testRunController.testRun);
remove(this._reportsPending, testRunController);
if (!this._reportsPending.length && this._resolveWaitingLastTestInFixture) {
this._resolveWaitingLastTestInFixture();
this._resolveWaitingLastTestInFixture = null;
}
}
if (!this._completionQueue.length && !this.hasQueuedTestRuns) {
if (!this._opts.live)
SessionController.closeSession(testRunController.testRun);
this
._setResult(BrowserJobResult.done, { total: this._total, passed: this._passed })
.then(() => this.emit('done'));
}
}
private async _isNextTestRunAvailable (testRunController: TestRunController): Promise<boolean> {
// NOTE: event task start is currently executing,
// so test run is temporary blocked
if (this._startStatus === StartStatus.starting)
return false;
// NOTE: before hook for test run fixture is currently
// executing, so test run is temporary blocked
const isBlocked = testRunController.blocked;
const isConcurrency = this._opts.concurrency as number > 1;
const hasIncompleteTestRuns = this._completionQueue.some(controller => !controller.done);
const needWaitLastTestInFixture = this._reportsPending.some(controller => controller.test.fixture !== testRunController.test.fixture);
if (isBlocked || (hasIncompleteTestRuns || needWaitLastTestInFixture) && !isConcurrency) {
const disablePageReloads = testRunController.test.disablePageReloads ||
this._opts.disablePageReloads && testRunController.test.disablePageReloads !== false;
if (!needWaitLastTestInFixture || !disablePageReloads)
return false;
// NOTE: if we have `disablePageReloads` enabled and the next test is from next
// fixture, then we need to wait until all reporters finished to prevent
// redirecting to the `idle` page
await new Promise(resolve => {
this._resolveWaitingLastTestInFixture = resolve;
});
}
return true;
}
// API
public get hasQueuedTestRuns (): boolean {
return !!this._testRunControllerQueue.length;
}
public async popNextTestRunUrl (connection: BrowserConnection): Promise<string | null> {
while (this._testRunControllerQueue.length) {
const testRunController = this._testRunControllerQueue[0];
const isNextTestRunAvailable = await this._isNextTestRunAvailable(testRunController);
if (!isNextTestRunAvailable)
break;
this._reportsPending.push(testRunController);
this._testRunControllerQueue.shift();
this._addToCompletionQueue(testRunController);
if (this._startStatus === StartStatus.none) {
this._startStatus = StartStatus.starting;
this._startTime = new Date();
await this.emit('start', this._startTime);
this._startStatus = StartStatus.started;
}
const testRunUrl = await testRunController.start(connection, this._startTime);
if (testRunUrl)
return testRunUrl;
}
return null;
}
public abort (): void {
this.clearListeners();
this._setResult(BrowserJobResult.aborted);
this.browserConnections.map(bc => bc.removeJob(this));
}
}