forked from DevExpress/testcafe
/
base.js
138 lines (100 loc) · 4.46 KB
/
base.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
import { getBrowserInfo } from 'testcafe-browser-tools';
import getMaximizedHeadlessWindowSize from '../../utils/get-maximized-headless-window-size';
import { cropScreenshot } from '../../../../screenshots/crop';
import { readPng, writePng } from '../../../../utils/promisified-functions';
export default {
openedBrowsers: {},
isMultiBrowser: false,
supportMultipleWindows: true,
getActiveWindowId (browserId) {
return this.openedBrowsers[browserId].activeWindowId;
},
setActiveWindowId (browserId, val) {
this.openedBrowsers[browserId].activeWindowId = val;
},
getPageTitle (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
const isIdlePageShown = !Object.keys(runtimeInfo.windowDescriptors).length;
return isIdlePageShown ? browserId : runtimeInfo.activeWindowId;
},
getWindowDescriptor (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
return runtimeInfo.windowDescriptors[runtimeInfo.activeWindowId];
},
setWindowDescriptor (browserId, windowDescriptor) {
const runtimeInfo = this.openedBrowsers[browserId];
runtimeInfo.windowDescriptors[runtimeInfo.activeWindowId] = windowDescriptor;
},
getConfig () {
throw new Error('Not implemented');
},
_getBrowserProtocolClient (/* runtimeInfo */) {
throw new Error('Not implemented');
},
_getBrowserName () {
return this.providerName.replace(':', '');
},
async isValidBrowserName (browserName) {
const config = await this.getConfig(browserName);
const browserInfo = await getBrowserInfo(config.path || this._getBrowserName());
return !!browserInfo;
},
async isLocalBrowser () {
return true;
},
isHeadlessBrowser (browserId, browserName) {
if (browserId)
return this.openedBrowsers[browserId].config.headless;
const config = this.getConfig(browserName);
return !!config.headless;
},
_getCropDimensions (viewportWidth, viewportHeight) {
if (!viewportWidth || !viewportHeight)
return null;
return {
left: 0,
top: 0,
right: viewportWidth,
bottom: viewportHeight,
};
},
async takeScreenshot (browserId, path, viewportWidth, viewportHeight, fullPage) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
const binaryImage = await browserClient.getScreenshotData(fullPage);
const cropDimensions = this._getCropDimensions(viewportWidth, viewportHeight);
let pngImage = await readPng(binaryImage);
if (!fullPage)
pngImage = await cropScreenshot(pngImage, { path, cropDimensions }) || pngImage;
await writePng(path, pngImage);
},
async maximizeWindow (browserId) {
const maximumSize = getMaximizedHeadlessWindowSize();
await this.resizeWindow(browserId, maximumSize.width, maximumSize.height, maximumSize.width, maximumSize.height);
},
async executeClientFunction (browserId, command, callsite) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
return browserClient.executeClientFunction(command, callsite);
},
async executeSelector ({ browserId, command, callsite, selectorTimeout }) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
return browserClient.executeSelector(command, callsite, selectorTimeout);
},
async switchToIframe ({ browserId, command, callsite, selectorTimeout }) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
return browserClient.switchToIframe(command, callsite, selectorTimeout);
},
async switchToMainWindow (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
return browserClient.switchToMainWindow();
},
async closeBrowserChildWindow (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
return browserClient.closeBrowserChildWindow();
},
};