@applitools/driver 1.4.17-beta.2 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,344 +1,347 @@
1
- "use strict";
2
- // @ts-nocheck
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.MockDriver = void 0;
5
- const { inspect } = require('util');
6
- const utils = require('@applitools/utils');
7
- const snippets = require('@applitools/snippets');
8
- const WELL_KNOWN_SCRIPTS = {
9
- 'dom-snapshot': script => /\/\* @applitools\/dom-snapshot@[\d.]+ \*\//.test(script),
10
- 'dom-capture': script => /\/\* @applitools\/dom-capture@[\d.]+ \*\//.test(script),
11
- };
12
- const DEFAULT_DESKTOP_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36';
13
- const DEFAULT_MOBILE_UA = 'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Mobile Safari/537.36';
14
- const DEFAULT_STYLES = {
15
- 'border-left-width': '0px',
16
- 'border-top-width': '0px',
17
- overflow: null,
18
- };
19
- const DEFAULT_PROPS = {
20
- clientWidth: 300,
21
- clientHeight: 400,
22
- overflow: null,
23
- };
24
- class MockDriver {
25
- constructor(options = {}) {
26
- const { viewport = { width: 1000, height: 1000 }, device, platform, browser, ua } = options;
27
- this._device = device;
28
- this._platform = platform;
29
- this._browser = browser;
30
- this._ua = ua;
31
- this._window = {
32
- title: 'Default Page Title',
33
- url: 'http://default.url',
34
- rect: Object.assign({ x: 0, y: 0 }, viewport),
35
- };
36
- this._methods = new Map();
37
- this._scripts = new Map();
38
- this._elements = new Map();
39
- this._contexts = new Map();
40
- this._contexts.set(null, {
41
- document: { id: Symbol('documentId') },
42
- state: {
43
- get name() {
44
- return null;
45
- },
46
- },
47
- });
48
- this._contextId = null;
49
- this.mockElement('html', { scrollPosition: { x: 0, y: 0 } });
50
- this.mockScript(snippets.getContextInfo, async () => {
51
- const context = this._contexts.get(this._contextId);
52
- const isRoot = !this._contextId;
53
- const isCORS = !isRoot && context.isCORS;
54
- const contentDocument = await this.findElement('html');
55
- const selector = !isCORS && !isRoot ? context.element.selector : null;
56
- return [contentDocument, selector, isRoot, isCORS];
57
- });
58
- this.mockScript(snippets.isEqualElements, async ([element1, element2]) => {
59
- return element1.id === element2.id;
60
- });
61
- this.mockScript(snippets.getChildFramesInfo, () => {
62
- return Array.from(this._contexts.values())
63
- .filter(frame => frame.parentId === this._contextId)
64
- .map(frame => [frame.element, frame.isCORS]);
65
- });
66
- this.mockScript(snippets.getElementRect, ([element]) => {
67
- return element.rect || { x: 0, y: 0, width: 100, height: 100 };
68
- });
69
- this.mockScript(snippets.getElementComputedStyleProperties, ([element, properties]) => {
70
- return properties.map(property => (element.styles || {})[property] || DEFAULT_STYLES[property]);
71
- });
72
- this.mockScript(snippets.getElementProperties, ([element, properties]) => {
73
- return properties.map(property => (element.props || {})[property] || DEFAULT_PROPS[property]);
74
- });
75
- this.mockScript(snippets.setElementStyleProperties, ([element, properties]) => {
76
- return Object.entries(properties).reduce((original, [name, value]) => {
77
- original[name] = { value: element.style[name], important: false };
78
- element.style[name] = value;
79
- return original;
80
- }, {});
81
- });
82
- this.mockScript(snippets.setElementAttributes, ([element, attributes]) => {
83
- return Object.entries(attributes).reduce((original, [name, value]) => {
84
- original[name] = element.attrs[name];
85
- element.attrs[name] = value;
86
- return original;
87
- }, {});
88
- });
89
- this.mockScript(snippets.scrollTo, ([element, offset]) => {
90
- let scrollingElement = element;
91
- if (!element) {
92
- scrollingElement = this.findElement('html');
93
- }
94
- scrollingElement.scrollPosition = offset;
95
- return [scrollingElement.scrollPosition.x, scrollingElement.scrollPosition.y];
96
- });
97
- this.mockScript(snippets.getElementScrollOffset, ([element]) => {
98
- let scrollingElement = element;
99
- if (!element) {
100
- scrollingElement = this.findElement('html');
101
- }
102
- if (!scrollingElement.scrollPosition) {
103
- scrollingElement.scrollPosition = { x: 0, y: 0 };
104
- }
105
- return { x: scrollingElement.scrollPosition.x, y: scrollingElement.scrollPosition.y };
106
- });
107
- this.mockScript(snippets.getElementInnerOffset, ([element]) => {
108
- let scrollingElement = element;
109
- if (!element) {
110
- scrollingElement = this.findElement('html');
111
- }
112
- if (!scrollingElement.scrollPosition) {
113
- scrollingElement.scrollPosition = { x: 0, y: 0 };
114
- }
115
- return { x: scrollingElement.scrollPosition.x, y: scrollingElement.scrollPosition.y };
116
- });
117
- this.mockScript(snippets.getPixelRatio, () => {
118
- return 1;
119
- });
120
- this.mockScript(snippets.getShadowRoot, ([element]) => {
121
- return element;
122
- });
123
- this.mockScript(snippets.getUserAgent, () => {
124
- if (this._ua !== undefined)
125
- return this._ua;
126
- return this.info.isMobile ? DEFAULT_MOBILE_UA : DEFAULT_DESKTOP_UA;
127
- });
128
- this.mockScript(snippets.getViewportSize, () => {
129
- return { width: this._window.rect.width, height: this._window.rect.height };
130
- });
131
- this.mockScript(snippets.getElementXpath, ([element]) => {
132
- if (element.xpath)
133
- return element.xpath;
134
- const elements = Array.from(this._elements.values()).reduce((elements, array) => elements.concat(array), []);
135
- const index = elements.findIndex(({ id }) => id === element.id);
136
- return index >= 0 ? `/HTML[1]/BODY[1]/DIV[${index + 1}]` : `//[data-fake-selector="${element.selector}"]`;
137
- });
138
- this.mockScript(snippets.blurElement, () => {
139
- return null;
140
- });
141
- this.mockScript(snippets.addElementIds, ([elements, ids]) => {
142
- const selectors = [];
143
- for (const [index, element] of elements.entries()) {
144
- const elementId = ids[index];
145
- element.attributes = element.attributes || [];
146
- element.attributes.push({ name: 'data-applitools-selector', value: elementId });
147
- const selector = `[data-applitools-selector~="${elementId}"]`;
148
- selectors.push([selector]);
149
- }
150
- return selectors;
151
- });
152
- this.mockScript(snippets.cleanupElementIds, ([elements]) => {
153
- for (const el of elements) {
154
- el.attributes.splice(el.attributes.findIndex(({ name }) => name === 'data-applitools-marker'), 1);
155
- }
156
- });
157
- this.mockScript(snippets.getElementContentSize, ([element]) => {
158
- return element.rect || { x: 0, y: 0, width: 100, height: 100 };
159
- });
160
- this.mockScript(snippets.getDocumentSize, () => {
161
- // TODO get window for context: `this.contexts.get(this._contextId)`
162
- return { width: this._window.rect.width, height: this._window.rect.height };
163
- });
164
- }
165
- mockScript(scriptMatcher, resultGenerator) {
166
- this._scripts.set(String(scriptMatcher), resultGenerator);
167
- }
168
- mockElement(selector, state) {
169
- const element = Object.assign({ id: Symbol('elementId' + Math.floor(Math.random() * 100)), attrs: {}, style: {}, selector, parentId: null, parentContextId: null, parentRootId: null }, state);
170
- if (element.shadow) {
171
- element.shadowRootId = Symbol('shadowId' + (element.name || Math.floor(Math.random() * 100)));
172
- }
173
- if (element.frame) {
174
- const contextId = Symbol('contextId' + (element.name || Math.floor(Math.random() * 100)));
175
- this._contexts.set(contextId, {
176
- id: contextId,
177
- parentId: state.parentContextId,
178
- isCORS: state.isCORS,
179
- element,
180
- document: { id: Symbol('documentId' + (element.name || Math.floor(Math.random() * 100))) },
181
- state: {
182
- get name() {
183
- return element.name || element.selector;
184
- },
185
- },
186
- });
187
- element.contextId = contextId;
188
- this.mockElement('html', {
189
- parentContextId: contextId,
190
- scrollPosition: { x: 0, y: 0 },
191
- });
192
- }
193
- this.mockSelector(selector, element);
194
- return element;
195
- }
196
- mockElements(nodes, { parentId = null, parentContextId = null, parentRootId = null } = {}) {
197
- for (const node of nodes) {
198
- const element = this.mockElement(node.selector, Object.assign(Object.assign({}, node), { parentId, parentContextId, parentRootId }));
199
- if (node.children) {
200
- this.mockElements(node.children, {
201
- parentId: element.frame ? null : element.id,
202
- parentContextId: element.frame ? element.contextId : parentContextId,
203
- parentRootId: element.shadow ? element.shadowRootId : parentRootId,
204
- });
205
- }
206
- }
207
- }
208
- mockSelector(selector, element) {
209
- let elements = this._elements.get(selector);
210
- if (!elements) {
211
- elements = [];
212
- this._elements.set(selector, elements);
213
- }
214
- elements.push(element);
215
- }
216
- wrapMethod(name, wrapper) {
217
- if (!this[name] || name.startsWith('_') || !utils.types.isFunction(this[name]))
218
- return;
219
- if (this._methods.has(name))
220
- this.unwrapMethod(name);
221
- this._methods.set(name, this[name]);
222
- this[name] = new Proxy(this[name], {
223
- apply: (method, driver, args) => wrapper(method, driver, args),
224
- });
225
- }
226
- unwrapMethod(name) {
227
- const method = this._methods.get(name);
228
- if (!method)
229
- return;
230
- this[name] = method;
231
- }
232
- get info() {
233
- return {
234
- isMobile: this._device ? Boolean(this._device.isMobile) : false,
235
- isNative: this._device ? Boolean(this._device.isNative) : false,
236
- deviceName: this._device ? this._device.name : null,
237
- platformName: this._platform ? this._platform.name : null,
238
- platformVersion: this._platform ? this._platform.version : null,
239
- browserName: this._browser ? this._browser.name : null,
240
- browserVersion: this._browser ? this._browser.version : null,
241
- };
242
- }
243
- async executeScript(script, args = []) {
244
- if (this.info.isNative)
245
- throw new Error("Native context doesn't support this method");
246
- let result = this._scripts.get(String(script));
247
- if (!result) {
248
- const name = Object.keys(WELL_KNOWN_SCRIPTS).find(name => WELL_KNOWN_SCRIPTS[name](script));
249
- if (!name)
250
- return null;
251
- result = this._scripts.get(name);
252
- }
253
- const { state } = this._contexts.get(this._contextId);
254
- return utils.types.isFunction(result) ? result.call(state, ...args) : result;
255
- }
256
- async findElement(selector, rootElement) {
257
- const elements = this._elements.get(selector);
258
- return elements
259
- ? elements.find(element => element.parentContextId === this._contextId &&
260
- element.parentRootId === ((rootElement || {}).shadowRootId || null))
261
- : null;
262
- }
263
- async findElements(selector, rootElement) {
264
- const elements = this._elements.get(selector);
265
- return elements
266
- ? elements.filter(element => element.parentContextId === this._contextId &&
267
- element.parentRootId === ((rootElement || {}).shadowRootId || null))
268
- : [];
269
- }
270
- async switchToFrame(reference) {
271
- if (reference === null) {
272
- this._contextId = null;
273
- return this;
274
- }
275
- if (utils.types.isString(reference)) {
276
- reference = await this.findElement(reference);
277
- }
278
- const frame = this._contexts.get(reference.contextId);
279
- if (frame && this._contextId === frame.parentId) {
280
- this._contextId = frame.id;
281
- return this;
282
- }
283
- else {
284
- throw new Error('Frame not found');
285
- }
286
- }
287
- async switchToParentFrame() {
288
- if (!this._contextId)
289
- return this;
290
- for (const frame of this._contexts.values()) {
291
- if (frame.id === this._contextId) {
292
- this._contextId = frame.parentId;
293
- return this;
294
- }
295
- }
296
- return this;
297
- }
298
- async getWindowRect() {
299
- return this._window.rect;
300
- }
301
- async setWindowRect(rect) {
302
- Object.assign(this._window.rect, rect);
303
- }
304
- async getUrl() {
305
- if (this.info.isNative)
306
- throw new Error("Native context doesn't support this method");
307
- return this._window.url;
308
- }
309
- async getTitle() {
310
- if (this.info.isNative)
311
- throw new Error("Native context doesn't support this method");
312
- return this._window.title;
313
- }
314
- async visit(url) {
315
- if (this.info.isNative)
316
- throw new Error("Native context doesn't support this method");
317
- this._window.url = url;
318
- }
319
- async takeScreenshot() {
320
- // const image = new png.Image({
321
- // width: this._window.rect.width,
322
- // height: this._window.rect.height,
323
- // })
324
- // const stream = image.pack()
325
- // return new Promise((resolve, reject) => {
326
- // let buffer = Buffer.from([])
327
- // stream.on('data', chunk => {
328
- // buffer = Buffer.concat([buffer, chunk])
329
- // })
330
- // stream.on('end', () => resolve(buffer))
331
- // stream.on('error', reject)
332
- // })
333
- }
334
- toString() {
335
- return '<MockDriver>';
336
- }
337
- toJSON() {
338
- return '<MockDriver>';
339
- }
340
- [inspect.custom]() {
341
- return '<MockDriver>';
342
- }
343
- }
344
- exports.MockDriver = MockDriver;
1
+ "use strict";
2
+ // @ts-nocheck
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.MockDriver = void 0;
5
+ const { inspect } = require('util');
6
+ const utils = require('@applitools/utils');
7
+ const snippets = require('@applitools/snippets');
8
+ const WELL_KNOWN_SCRIPTS = {
9
+ 'dom-snapshot': script => /\/\* @applitools\/dom-snapshot@[\d.]+ \*\//.test(script),
10
+ 'dom-capture': script => /\/\* @applitools\/dom-capture@[\d.]+ \*\//.test(script),
11
+ };
12
+ const DEFAULT_DESKTOP_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36';
13
+ const DEFAULT_MOBILE_UA = 'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Mobile Safari/537.36';
14
+ const DEFAULT_STYLES = {
15
+ 'border-left-width': '0px',
16
+ 'border-top-width': '0px',
17
+ overflow: null,
18
+ };
19
+ const DEFAULT_PROPS = {
20
+ clientWidth: 300,
21
+ clientHeight: 400,
22
+ overflow: null,
23
+ };
24
+ class MockDriver {
25
+ constructor(options = {}) {
26
+ const { viewport = { width: 1000, height: 1000 }, device, platform, browser, ua } = options;
27
+ this._device = device;
28
+ this._platform = platform;
29
+ this._browser = browser;
30
+ this._ua = ua;
31
+ this._window = {
32
+ title: 'Default Page Title',
33
+ url: 'http://default.url',
34
+ rect: Object.assign({ x: 0, y: 0 }, viewport),
35
+ };
36
+ this._methods = new Map();
37
+ this._scripts = new Map();
38
+ this._elements = new Map();
39
+ this._contexts = new Map();
40
+ this._contexts.set(null, {
41
+ document: { id: Symbol('documentId') },
42
+ state: {
43
+ get name() {
44
+ return null;
45
+ },
46
+ },
47
+ });
48
+ this._contextId = null;
49
+ this.mockElement('html', { scrollPosition: { x: 0, y: 0 } });
50
+ this.mockScript(snippets.getContextInfo, async () => {
51
+ const context = this._contexts.get(this._contextId);
52
+ const isRoot = !this._contextId;
53
+ const isCORS = !isRoot && context.isCORS;
54
+ const contentDocument = await this.findElement('html');
55
+ const selector = !isCORS && !isRoot ? context.element.selector : null;
56
+ return [contentDocument, selector, isRoot, isCORS];
57
+ });
58
+ this.mockScript(snippets.isEqualElements, async ([element1, element2]) => {
59
+ return element1.id === element2.id;
60
+ });
61
+ this.mockScript(snippets.getDocumentScrollingElement, async () => {
62
+ return 'html';
63
+ });
64
+ this.mockScript(snippets.getChildFramesInfo, () => {
65
+ return Array.from(this._contexts.values())
66
+ .filter(frame => frame.parentId === this._contextId)
67
+ .map(frame => [frame.element, frame.isCORS]);
68
+ });
69
+ this.mockScript(snippets.getElementRect, ([element]) => {
70
+ return element.rect || { x: 0, y: 0, width: 100, height: 100 };
71
+ });
72
+ this.mockScript(snippets.getElementComputedStyleProperties, ([element, properties]) => {
73
+ return properties.map(property => (element.styles || {})[property] || DEFAULT_STYLES[property]);
74
+ });
75
+ this.mockScript(snippets.getElementProperties, ([element, properties]) => {
76
+ return properties.map(property => (element.props || {})[property] || DEFAULT_PROPS[property]);
77
+ });
78
+ this.mockScript(snippets.setElementStyleProperties, ([element, properties]) => {
79
+ return Object.entries(properties).reduce((original, [name, value]) => {
80
+ original[name] = { value: element.style[name], important: false };
81
+ element.style[name] = value;
82
+ return original;
83
+ }, {});
84
+ });
85
+ this.mockScript(snippets.setElementAttributes, ([element, attributes]) => {
86
+ return Object.entries(attributes).reduce((original, [name, value]) => {
87
+ original[name] = element.attrs[name];
88
+ element.attrs[name] = value;
89
+ return original;
90
+ }, {});
91
+ });
92
+ this.mockScript(snippets.scrollTo, ([element, offset]) => {
93
+ let scrollingElement = element;
94
+ if (!element) {
95
+ scrollingElement = this.findElement('html');
96
+ }
97
+ scrollingElement.scrollPosition = offset;
98
+ return [scrollingElement.scrollPosition.x, scrollingElement.scrollPosition.y];
99
+ });
100
+ this.mockScript(snippets.getElementScrollOffset, ([element]) => {
101
+ let scrollingElement = element;
102
+ if (!element) {
103
+ scrollingElement = this.findElement('html');
104
+ }
105
+ if (!scrollingElement.scrollPosition) {
106
+ scrollingElement.scrollPosition = { x: 0, y: 0 };
107
+ }
108
+ return { x: scrollingElement.scrollPosition.x, y: scrollingElement.scrollPosition.y };
109
+ });
110
+ this.mockScript(snippets.getElementInnerOffset, ([element]) => {
111
+ let scrollingElement = element;
112
+ if (!element) {
113
+ scrollingElement = this.findElement('html');
114
+ }
115
+ if (!scrollingElement.scrollPosition) {
116
+ scrollingElement.scrollPosition = { x: 0, y: 0 };
117
+ }
118
+ return { x: scrollingElement.scrollPosition.x, y: scrollingElement.scrollPosition.y };
119
+ });
120
+ this.mockScript(snippets.getPixelRatio, () => {
121
+ return 1;
122
+ });
123
+ this.mockScript(snippets.getShadowRoot, ([element]) => {
124
+ return element;
125
+ });
126
+ this.mockScript(snippets.getUserAgent, () => {
127
+ if (this._ua !== undefined)
128
+ return this._ua;
129
+ return this.info.isMobile ? DEFAULT_MOBILE_UA : DEFAULT_DESKTOP_UA;
130
+ });
131
+ this.mockScript(snippets.getViewportSize, () => {
132
+ return { width: this._window.rect.width, height: this._window.rect.height };
133
+ });
134
+ this.mockScript(snippets.getElementXpath, ([element]) => {
135
+ if (element.xpath)
136
+ return element.xpath;
137
+ const elements = Array.from(this._elements.values()).reduce((elements, array) => elements.concat(array), []);
138
+ const index = elements.findIndex(({ id }) => id === element.id);
139
+ return index >= 0 ? `/HTML[1]/BODY[1]/DIV[${index + 1}]` : `//[data-fake-selector="${element.selector}"]`;
140
+ });
141
+ this.mockScript(snippets.blurElement, () => {
142
+ return null;
143
+ });
144
+ this.mockScript(snippets.addElementIds, ([elements, ids]) => {
145
+ const selectors = [];
146
+ for (const [index, element] of elements.entries()) {
147
+ const elementId = ids[index];
148
+ element.attributes = element.attributes || [];
149
+ element.attributes.push({ name: 'data-applitools-selector', value: elementId });
150
+ const selector = `[data-applitools-selector~="${elementId}"]`;
151
+ selectors.push([selector]);
152
+ }
153
+ return selectors;
154
+ });
155
+ this.mockScript(snippets.cleanupElementIds, ([elements]) => {
156
+ for (const el of elements) {
157
+ el.attributes.splice(el.attributes.findIndex(({ name }) => name === 'data-applitools-marker'), 1);
158
+ }
159
+ });
160
+ this.mockScript(snippets.getElementContentSize, ([element]) => {
161
+ return element.rect || { x: 0, y: 0, width: 100, height: 100 };
162
+ });
163
+ this.mockScript(snippets.getDocumentSize, () => {
164
+ // TODO get window for context: `this.contexts.get(this._contextId)`
165
+ return { width: this._window.rect.width, height: this._window.rect.height };
166
+ });
167
+ }
168
+ mockScript(scriptMatcher, resultGenerator) {
169
+ this._scripts.set(String(scriptMatcher), resultGenerator);
170
+ }
171
+ mockElement(selector, state) {
172
+ const element = Object.assign({ id: Symbol('elementId' + Math.floor(Math.random() * 100)), attrs: {}, style: {}, selector, parentId: null, parentContextId: null, parentRootId: null }, state);
173
+ if (element.shadow) {
174
+ element.shadowRootId = Symbol('shadowId' + (element.name || Math.floor(Math.random() * 100)));
175
+ }
176
+ if (element.frame) {
177
+ const contextId = Symbol('contextId' + (element.name || Math.floor(Math.random() * 100)));
178
+ this._contexts.set(contextId, {
179
+ id: contextId,
180
+ parentId: state.parentContextId,
181
+ isCORS: state.isCORS,
182
+ element,
183
+ document: { id: Symbol('documentId' + (element.name || Math.floor(Math.random() * 100))) },
184
+ state: {
185
+ get name() {
186
+ return element.name || element.selector;
187
+ },
188
+ },
189
+ });
190
+ element.contextId = contextId;
191
+ this.mockElement('html', {
192
+ parentContextId: contextId,
193
+ scrollPosition: { x: 0, y: 0 },
194
+ });
195
+ }
196
+ this.mockSelector(selector, element);
197
+ return element;
198
+ }
199
+ mockElements(nodes, { parentId = null, parentContextId = null, parentRootId = null } = {}) {
200
+ for (const node of nodes) {
201
+ const element = this.mockElement(node.selector, Object.assign(Object.assign({}, node), { parentId, parentContextId, parentRootId }));
202
+ if (node.children) {
203
+ this.mockElements(node.children, {
204
+ parentId: element.frame ? null : element.id,
205
+ parentContextId: element.frame ? element.contextId : parentContextId,
206
+ parentRootId: element.shadow ? element.shadowRootId : parentRootId,
207
+ });
208
+ }
209
+ }
210
+ }
211
+ mockSelector(selector, element) {
212
+ let elements = this._elements.get(selector);
213
+ if (!elements) {
214
+ elements = [];
215
+ this._elements.set(selector, elements);
216
+ }
217
+ elements.push(element);
218
+ }
219
+ wrapMethod(name, wrapper) {
220
+ if (!this[name] || name.startsWith('_') || !utils.types.isFunction(this[name]))
221
+ return;
222
+ if (this._methods.has(name))
223
+ this.unwrapMethod(name);
224
+ this._methods.set(name, this[name]);
225
+ this[name] = new Proxy(this[name], {
226
+ apply: (method, driver, args) => wrapper(method, driver, args),
227
+ });
228
+ }
229
+ unwrapMethod(name) {
230
+ const method = this._methods.get(name);
231
+ if (!method)
232
+ return;
233
+ this[name] = method;
234
+ }
235
+ get info() {
236
+ return {
237
+ isMobile: this._device ? Boolean(this._device.isMobile) : false,
238
+ isNative: this._device ? Boolean(this._device.isNative) : false,
239
+ deviceName: this._device ? this._device.name : null,
240
+ platformName: this._platform ? this._platform.name : null,
241
+ platformVersion: this._platform ? this._platform.version : null,
242
+ browserName: this._browser ? this._browser.name : null,
243
+ browserVersion: this._browser ? this._browser.version : null,
244
+ };
245
+ }
246
+ async executeScript(script, args = []) {
247
+ if (this.info.isNative)
248
+ throw new Error("Native context doesn't support this method");
249
+ let result = this._scripts.get(String(script));
250
+ if (!result) {
251
+ const name = Object.keys(WELL_KNOWN_SCRIPTS).find(name => WELL_KNOWN_SCRIPTS[name](script));
252
+ if (!name)
253
+ return null;
254
+ result = this._scripts.get(name);
255
+ }
256
+ const { state } = this._contexts.get(this._contextId);
257
+ return utils.types.isFunction(result) ? result.call(state, ...args) : result;
258
+ }
259
+ async findElement(selector, rootElement) {
260
+ const elements = this._elements.get(selector);
261
+ return elements
262
+ ? elements.find(element => element.parentContextId === this._contextId &&
263
+ element.parentRootId === ((rootElement || {}).shadowRootId || null))
264
+ : null;
265
+ }
266
+ async findElements(selector, rootElement) {
267
+ const elements = this._elements.get(selector);
268
+ return elements
269
+ ? elements.filter(element => element.parentContextId === this._contextId &&
270
+ element.parentRootId === ((rootElement || {}).shadowRootId || null))
271
+ : [];
272
+ }
273
+ async switchToFrame(reference) {
274
+ if (reference === null) {
275
+ this._contextId = null;
276
+ return this;
277
+ }
278
+ if (utils.types.isString(reference)) {
279
+ reference = await this.findElement(reference);
280
+ }
281
+ const frame = this._contexts.get(reference.contextId);
282
+ if (frame && this._contextId === frame.parentId) {
283
+ this._contextId = frame.id;
284
+ return this;
285
+ }
286
+ else {
287
+ throw new Error('Frame not found');
288
+ }
289
+ }
290
+ async switchToParentFrame() {
291
+ if (!this._contextId)
292
+ return this;
293
+ for (const frame of this._contexts.values()) {
294
+ if (frame.id === this._contextId) {
295
+ this._contextId = frame.parentId;
296
+ return this;
297
+ }
298
+ }
299
+ return this;
300
+ }
301
+ async getWindowRect() {
302
+ return this._window.rect;
303
+ }
304
+ async setWindowRect(rect) {
305
+ Object.assign(this._window.rect, rect);
306
+ }
307
+ async getUrl() {
308
+ if (this.info.isNative)
309
+ throw new Error("Native context doesn't support this method");
310
+ return this._window.url;
311
+ }
312
+ async getTitle() {
313
+ if (this.info.isNative)
314
+ throw new Error("Native context doesn't support this method");
315
+ return this._window.title;
316
+ }
317
+ async visit(url) {
318
+ if (this.info.isNative)
319
+ throw new Error("Native context doesn't support this method");
320
+ this._window.url = url;
321
+ }
322
+ async takeScreenshot() {
323
+ // const image = new png.Image({
324
+ // width: this._window.rect.width,
325
+ // height: this._window.rect.height,
326
+ // })
327
+ // const stream = image.pack()
328
+ // return new Promise((resolve, reject) => {
329
+ // let buffer = Buffer.from([])
330
+ // stream.on('data', chunk => {
331
+ // buffer = Buffer.concat([buffer, chunk])
332
+ // })
333
+ // stream.on('end', () => resolve(buffer))
334
+ // stream.on('error', reject)
335
+ // })
336
+ }
337
+ toString() {
338
+ return '<MockDriver>';
339
+ }
340
+ toJSON() {
341
+ return '<MockDriver>';
342
+ }
343
+ [inspect.custom]() {
344
+ return '<MockDriver>';
345
+ }
346
+ }
347
+ exports.MockDriver = MockDriver;