forked from DevExpress/testcafe
/
index.ts
445 lines (335 loc) · 17.3 KB
/
index.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import debug from 'debug';
import browserTools from 'testcafe-browser-tools';
import OS from 'os-family';
import { dirname } from 'path';
import makeDir from 'make-dir';
import BrowserConnection, { BrowserCloseData } from '../connection';
import delay from '../../utils/delay';
import {
GET_IS_SERVICE_WORKER_ENABLED,
GET_TITLE_SCRIPT,
GET_WINDOW_DIMENSIONS_INFO_SCRIPT,
} from './utils/client-functions';
import WARNING_MESSAGE from '../../notifications/warning-message';
import { Dictionary } from '../../configuration/interfaces';
import { WindowDimentionsInfo } from '../interfaces';
import { CallsiteRecord } from 'callsite-record';
import { ExecuteClientFunctionCommand, ExecuteSelectorCommand } from '../../test-run/commands/observation';
import { SwitchToIframeCommand } from '../../test-run/commands/actions';
const DEBUG_LOGGER = debug('testcafe:browser:provider');
const BROWSER_OPENING_DELAY = 2000;
const RESIZE_DIFF_SIZE = {
width: 100,
height: 100,
};
interface Size {
width: number;
height: number;
}
interface LocalBrowserInfo {
windowDescriptor: null | string;
maxScreenSize: null | Size;
resizeCorrections: null | Size;
}
function sumSizes (sizeA: Size, sizeB: Size): Size {
return {
width: sizeA.width + sizeB.width,
height: sizeA.height + sizeB.height,
};
}
function subtractSizes (sizeA: Size, sizeB: Size): Size {
return {
width: sizeA.width - sizeB.width,
height: sizeA.height - sizeB.height,
};
}
export default class BrowserProvider {
private plugin: any;
private initPromise: Promise<any>;
private isMultiBrowser: boolean;
private readonly localBrowsersInfo: Dictionary<LocalBrowserInfo>;
public constructor (plugin: any) {
this.plugin = plugin;
this.initPromise = Promise.resolve(false);
this.isMultiBrowser = this.plugin.isMultiBrowser;
// HACK: The browser window has different border sizes in normal and maximized modes. So, we need to be sure that the window is
// not maximized before resizing it in order to keep the mechanism of correcting the client area size working. When browser is started,
// we are resizing it for the first time to switch the window to normal mode, and for the second time - to restore the client area size.
this.localBrowsersInfo = {};
}
private _ensureLocalBrowserInfo (browserId: string): void {
if (this.localBrowsersInfo[browserId])
return;
this.localBrowsersInfo[browserId] = {
windowDescriptor: null,
maxScreenSize: null,
resizeCorrections: null,
};
}
private async _findWindow (browserId: string): Promise<string> {
const pageTitle = this._getPageTitle(browserId);
return browserTools.findWindow(pageTitle);
}
private _getPageTitle (browserId: string): string {
if (this.plugin.getPageTitle)
return this.plugin.getPageTitle(browserId);
return browserId;
}
private _getWindowDescriptor (browserId: string): string | null {
if (this.plugin.getWindowDescriptor)
return this.plugin.getWindowDescriptor(browserId);
return this.localBrowsersInfo[browserId] && this.localBrowsersInfo[browserId].windowDescriptor;
}
private _setWindowDescriptor (browserId: string, windowDescriptor: string | null): void {
if (this.plugin.setWindowDescriptor) {
this.plugin.setWindowDescriptor(browserId, windowDescriptor);
return;
}
this.localBrowsersInfo[browserId].windowDescriptor = windowDescriptor;
}
private _getMaxScreenSize (browserId: string): Size | null {
return this.localBrowsersInfo[browserId] && this.localBrowsersInfo[browserId].maxScreenSize;
}
private _getResizeCorrections (browserId: string): Size | null {
return this.localBrowsersInfo[browserId] && this.localBrowsersInfo[browserId].resizeCorrections;
}
private _isBrowserIdle (browserId: string): boolean {
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
return connection.idle;
}
private async _calculateResizeCorrections (browserId: string): Promise<void> {
if (!this._isBrowserIdle(browserId))
return;
const title = await this.plugin.runInitScript(browserId, GET_TITLE_SCRIPT);
if (!await browserTools.isMaximized(title))
return;
const currentSize = await this.plugin.runInitScript(browserId, GET_WINDOW_DIMENSIONS_INFO_SCRIPT) as WindowDimentionsInfo;
const etalonSize = subtractSizes(currentSize, RESIZE_DIFF_SIZE);
await browserTools.resize(title, currentSize.width, currentSize.height, etalonSize.width, etalonSize.height);
let resizedSize = await this.plugin.runInitScript(browserId, GET_WINDOW_DIMENSIONS_INFO_SCRIPT) as WindowDimentionsInfo;
let correctionSize = subtractSizes(resizedSize, etalonSize);
await browserTools.resize(title, resizedSize.width, resizedSize.height, etalonSize.width, etalonSize.height);
resizedSize = await this.plugin.runInitScript(browserId, GET_WINDOW_DIMENSIONS_INFO_SCRIPT) as WindowDimentionsInfo;
correctionSize = sumSizes(correctionSize, subtractSizes(resizedSize, etalonSize));
if (this.localBrowsersInfo[browserId])
this.localBrowsersInfo[browserId].resizeCorrections = correctionSize;
await browserTools.maximize(title);
}
private async _calculateMacSizeLimits (browserId: string): Promise<void> {
if (!this._isBrowserIdle(browserId))
return;
const sizeInfo = await this.plugin.runInitScript(browserId, GET_WINDOW_DIMENSIONS_INFO_SCRIPT) as WindowDimentionsInfo;
if (this.localBrowsersInfo[browserId]) {
this.localBrowsersInfo[browserId].maxScreenSize = {
width: sizeInfo.availableWidth - (sizeInfo.outerWidth - sizeInfo.width),
height: sizeInfo.availableHeight - (sizeInfo.outerHeight - sizeInfo.height),
};
}
}
private async _ensureBrowserWindowDescriptor (browserId: string): Promise<void> {
if (this._getWindowDescriptor(browserId))
return;
await this._ensureLocalBrowserInfo(browserId);
// NOTE: delay to ensure the window finished the opening
await this.plugin.waitForConnectionReady(browserId);
await delay(BROWSER_OPENING_DELAY);
if (this.localBrowsersInfo[browserId]) {
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
let windowDescriptor = null;
try {
windowDescriptor = await this._findWindow(browserId);
}
catch (err) {
// NOTE: We can suppress the error here since we can just disable window manipulation functions
// when we cannot find a local window descriptor
DEBUG_LOGGER(err);
connection.addWarning(
WARNING_MESSAGE.cannotFindWindowDescriptorError,
connection.browserInfo.alias,
err.message
);
}
this._setWindowDescriptor(browserId, windowDescriptor);
}
}
private async _ensureBrowserWindowParameters (browserId: string): Promise<void> {
await this._ensureBrowserWindowDescriptor(browserId);
if (OS.win && !this._getResizeCorrections(browserId))
await this._calculateResizeCorrections(browserId);
else if (OS.mac && !this._getMaxScreenSize(browserId))
await this._calculateMacSizeLimits(browserId);
}
private async _closeLocalBrowser (browserId: string): Promise<void> {
if (this.plugin.needCleanUpBrowserInfo)
this.plugin.cleanUpBrowserInfo(browserId);
const windowDescriptor = this._getWindowDescriptor(browserId);
await browserTools.close(windowDescriptor);
}
private async _resizeLocalBrowserWindow (browserId: string, width: number, height: number, currentWidth: number, currentHeight: number): Promise<void> {
await this._ensureBrowserWindowDescriptor(browserId);
const resizeCorrections = this._getResizeCorrections(browserId);
if (resizeCorrections && await browserTools.isMaximized(this._getWindowDescriptor(browserId))) {
width -= resizeCorrections.width;
height -= resizeCorrections.height;
}
await browserTools.resize(this._getWindowDescriptor(browserId), currentWidth, currentHeight, width, height);
}
private async _takeLocalBrowserScreenshot (browserId: string, screenshotPath: string): Promise<void> {
await browserTools.screenshot(this._getWindowDescriptor(browserId), screenshotPath);
}
private async _canResizeLocalBrowserWindowToDimensions (browserId: string, width: number, height: number): Promise<boolean> {
if (!OS.mac)
return true;
const maxScreenSize = this._getMaxScreenSize(browserId) as Size;
return width <= maxScreenSize.width && height <= maxScreenSize.height;
}
private async _maximizeLocalBrowserWindow (browserId: string): Promise<void> {
await this._ensureBrowserWindowDescriptor(browserId);
await browserTools.maximize(this._getWindowDescriptor(browserId));
}
private async _ensureRetryTestPagesWarning (browserId: string): Promise<void> {
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
if (connection?.retryTestPages) {
const isServiceWorkerEnabled = await this.plugin.runInitScript(browserId, GET_IS_SERVICE_WORKER_ENABLED);
if (!isServiceWorkerEnabled)
connection.addWarning(WARNING_MESSAGE.retryTestPagesIsNotSupported, connection.browserInfo.alias, connection.browserInfo.alias);
}
}
public async canUseDefaultWindowActions (browserId: string): Promise<boolean> {
const isLocalBrowser = await this.plugin.isLocalBrowser(browserId);
const isHeadlessBrowser = await this.plugin.isHeadlessBrowser(browserId);
return isLocalBrowser && !isHeadlessBrowser;
}
public async init (): Promise<void> {
const initialized = await this.initPromise;
if (initialized)
return;
this.initPromise = this.plugin
.init()
.then(() => true);
try {
await this.initPromise;
}
catch (error) {
this.initPromise = Promise.resolve(false);
throw error;
}
}
public async dispose (): Promise<void> {
const initialized = await this.initPromise;
if (!initialized)
return;
this.initPromise = this.plugin
.dispose()
.then(() => false);
try {
await this.initPromise;
}
catch (error) {
this.initPromise = Promise.resolve(false);
throw error;
}
}
public async isLocalBrowser (browserId?: string, browserName?: string): Promise<boolean> {
return await this.plugin.isLocalBrowser(browserId, browserName);
}
public isHeadlessBrowser (browserId?: string, browserName?: string): Promise<boolean> {
return this.plugin.isHeadlessBrowser(browserId, browserName);
}
public async openBrowser (browserId: string, pageUrl: string, browserOption: unknown, disableMultipleWindows: boolean, proxyless: boolean): Promise<void> {
await this.plugin.openBrowser(browserId, pageUrl, browserOption, disableMultipleWindows, proxyless);
await this._ensureRetryTestPagesWarning(browserId);
if (await this.canUseDefaultWindowActions(browserId))
await this._ensureBrowserWindowParameters(browserId);
}
public async closeBrowser (browserId: string, data: BrowserCloseData): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomCloseBrowser = customActionsInfo.hasCloseBrowser;
const usePluginsCloseBrowser = hasCustomCloseBrowser || !canUseDefaultWindowActions;
if (usePluginsCloseBrowser)
await this.plugin.closeBrowser(browserId, data);
else
await this._closeLocalBrowser(browserId);
if (canUseDefaultWindowActions)
delete this.localBrowsersInfo[browserId];
}
public async getBrowserList (): Promise<string[]> {
return await this.plugin.getBrowserList();
}
public async isValidBrowserName (browserName: string): Promise<boolean> {
return await this.plugin.isValidBrowserName(browserName);
}
public async resizeWindow (browserId: string, width: number, height: number, currentWidth: number, currentHeight: number): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomResizeWindow = customActionsInfo.hasResizeWindow;
if (canUseDefaultWindowActions && !hasCustomResizeWindow) {
await this._resizeLocalBrowserWindow(browserId, width, height, currentWidth, currentHeight);
return;
}
await this.plugin.resizeWindow(browserId, width, height, currentWidth, currentHeight);
}
public async canResizeWindowToDimensions (browserId: string, width: number, height: number): Promise<boolean> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomCanResizeToDimensions = customActionsInfo.hasCanResizeWindowToDimensions;
if (canUseDefaultWindowActions && !hasCustomCanResizeToDimensions)
return await this._canResizeLocalBrowserWindowToDimensions(browserId, width, height);
return await this.plugin.canResizeWindowToDimensions(browserId, width, height);
}
public async maximizeWindow (browserId: string): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomMaximizeWindow = customActionsInfo.hasMaximizeWindow;
if (canUseDefaultWindowActions && !hasCustomMaximizeWindow)
return await this._maximizeLocalBrowserWindow(browserId);
return await this.plugin.maximizeWindow(browserId);
}
public async takeScreenshot (browserId: string, screenshotPath: string, pageWidth: number, pageHeight: number, fullPage: boolean): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomTakeScreenshot = customActionsInfo.hasTakeScreenshot;
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
const takeLocalBrowsersScreenshot = canUseDefaultWindowActions && !hasCustomTakeScreenshot;
const isLocalFullPageMode = takeLocalBrowsersScreenshot && fullPage;
if (isLocalFullPageMode) {
connection.addWarning(WARNING_MESSAGE.screenshotsFullPageNotSupported, connection.browserInfo.alias);
return;
}
await makeDir(dirname(screenshotPath));
if (takeLocalBrowsersScreenshot)
await this._takeLocalBrowserScreenshot(browserId, screenshotPath);
else
await this.plugin.takeScreenshot(browserId, screenshotPath, pageWidth, pageHeight, fullPage);
}
public async executeClientFunction (browserId: string, command: ExecuteClientFunctionCommand, callsite: CallsiteRecord): Promise<any> {
return this.plugin.executeClientFunction(browserId, command, callsite);
}
public async executeSelector (browserId: string, command: ExecuteSelectorCommand, callsite: CallsiteRecord, selectorTimeout: number): Promise<any> {
return this.plugin.executeSelector({ browserId, command, callsite, selectorTimeout });
}
public async switchToIframe (browserId: string, command: SwitchToIframeCommand, callsite: CallsiteRecord, selectorTimeout: number): Promise<void> {
return this.plugin.switchToIframe({ browserId, command, callsite, selectorTimeout });
}
public switchToMainWindow (browserId: string): Promise<void> {
return this.plugin.switchToMainWindow(browserId);
}
public async getVideoFrameData (browserId: string): Promise<any> {
return this.plugin.getVideoFrameData(browserId);
}
public async hasCustomActionForBrowser (browserId: string): Promise<any> {
return this.plugin.hasCustomActionForBrowser(browserId);
}
public async reportJobResult (browserId: string, status: string, data: any): Promise<void> {
await this.plugin.reportJobResult(browserId, status, data);
}
public getActiveWindowId (browserId: string): string | null {
if (!this.plugin.supportMultipleWindows)
return null;
return this.plugin.getActiveWindowId(browserId);
}
public setActiveWindowId (browserId: string, val: string): void {
this.plugin.setActiveWindowId(browserId, val);
}
}