-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathPuppeteerRunnerExtension.ts
499 lines (472 loc) · 13.5 KB
/
PuppeteerRunnerExtension.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import type {
Browser,
ElementHandle,
Frame,
LocatorEmittedEvents,
Page,
} from 'puppeteer';
import { Frame as InternalFrame } from 'puppeteer-core/internal/common/Frame.js';
import { CDPPage as InternalPage } from 'puppeteer-core/internal/common/Page.js';
import { RunnerExtension } from './RunnerExtension.js';
import {
AssertedEventType,
Selector,
Step,
StepType,
UserFlow,
WaitForElementStep,
} from './Schema.js';
import {
assertAllStepTypesAreHandled,
selectorToPElementSelector,
mouseButtonMap,
} from './SchemaUtils.js';
const comparators = {
'==': (a: number, b: number): boolean => a === b,
'>=': (a: number, b: number): boolean => a >= b,
'<=': (a: number, b: number): boolean => a <= b,
};
export class PuppeteerRunnerExtension extends RunnerExtension {
protected browser: Browser;
protected page: Page;
protected timeout: number;
constructor(browser: Browser, page: Page, opts?: { timeout?: number }) {
super();
this.browser = browser;
this.page = page;
this.timeout = opts?.timeout || 5000;
}
async #ensureAutomationEmulatation(pageOrFrame: Page | Frame) {
try {
await (pageOrFrame as unknown as InternalPage | InternalFrame)
._client()
.send('Emulation.setAutomationOverride', { enabled: true });
} catch {
// ignore errors as not all versions support this command.
}
}
#getTimeoutForStep(step: Step, flow?: UserFlow): number {
return step.timeout || flow?.timeout || this.timeout;
}
override async runStep(step: Step, flow?: UserFlow): Promise<void> {
const timeout = this.#getTimeoutForStep(step, flow);
const page = this.page;
const browser = this.browser;
const targetPage = await getTargetPageForStep(browser, page, step, timeout);
let targetFrame: Frame | null = null;
if (!targetPage && step.target) {
const frames = page.frames();
for (const f of frames) {
if (f.isOOPFrame() && f.url() === step.target) {
targetFrame = f;
break;
}
}
if (!targetFrame) {
targetFrame = await page.waitForFrame(step.target, { timeout });
}
}
const targetPageOrFrame = targetFrame || targetPage;
if (!targetPageOrFrame) {
throw new Error('Target is not found for step: ' + JSON.stringify(step));
}
await this.#ensureAutomationEmulatation(targetPageOrFrame);
const localFrame = await getFrame(targetPageOrFrame, step);
await this.runStepInFrame(
step,
page,
targetPageOrFrame,
localFrame,
timeout
);
}
/**
* @internal
*/
async runStepInFrame(
step: Step,
mainPage: Page,
targetPageOrFrame: Page | Frame,
localFrame: Frame,
timeout: number
): Promise<void> {
let assertedEventsPromise = null;
const startWaitingForEvents = () => {
assertedEventsPromise = waitForEvents(localFrame, step, timeout);
};
const locatorRace = (this.page as unknown as InternalPage).locatorRace;
switch (step.type) {
case StepType.DoubleClick:
await locatorRace(
step.selectors.map((selector) => {
return (localFrame as unknown as InternalFrame).locator(
selectorToPElementSelector(selector)
);
})
)
.setTimeout(timeout)
.on('action' as LocatorEmittedEvents.Action, () =>
startWaitingForEvents()
)
.click({
count: 2,
button: step.button && mouseButtonMap.get(step.button),
delay: step.duration,
offset: {
x: step.offsetX,
y: step.offsetY,
},
});
break;
case StepType.Click:
await locatorRace(
step.selectors.map((selector) => {
return (localFrame as unknown as InternalFrame).locator(
selectorToPElementSelector(selector)
);
})
)
.setTimeout(timeout)
.on('action' as LocatorEmittedEvents.Action, () =>
startWaitingForEvents()
)
.click({
delay: step.duration,
button: step.button && mouseButtonMap.get(step.button),
offset: {
x: step.offsetX,
y: step.offsetY,
},
});
break;
case StepType.Hover:
await locatorRace(
step.selectors.map((selector) => {
return (localFrame as unknown as InternalFrame).locator(
selectorToPElementSelector(selector)
);
})
)
.setTimeout(timeout)
.on('action' as LocatorEmittedEvents.Action, () =>
startWaitingForEvents()
)
.hover();
break;
case StepType.EmulateNetworkConditions:
{
startWaitingForEvents();
await mainPage.emulateNetworkConditions(step);
}
break;
case StepType.KeyDown:
{
startWaitingForEvents();
await mainPage.keyboard.down(step.key);
await mainPage.waitForTimeout(100);
}
break;
case StepType.KeyUp:
{
startWaitingForEvents();
await mainPage.keyboard.up(step.key);
await mainPage.waitForTimeout(100);
}
break;
case StepType.Close:
{
if ('close' in targetPageOrFrame) {
startWaitingForEvents();
await targetPageOrFrame.close();
}
}
break;
case StepType.Change:
await locatorRace(
step.selectors.map((selector) => {
return (localFrame as unknown as InternalFrame).locator(
selectorToPElementSelector(selector)
);
})
)
.on('action' as LocatorEmittedEvents.Action, () =>
startWaitingForEvents()
)
.setTimeout(timeout)
.fill(step.value);
break;
case StepType.SetViewport: {
if ('setViewport' in targetPageOrFrame) {
startWaitingForEvents();
await targetPageOrFrame.setViewport(step);
}
break;
}
case StepType.Scroll: {
if ('selectors' in step) {
await locatorRace(
step.selectors.map((selector) => {
return (localFrame as unknown as InternalFrame).locator(
selectorToPElementSelector(selector)
);
})
)
.on('action' as LocatorEmittedEvents.Action, () =>
startWaitingForEvents()
)
.setTimeout(timeout)
.scroll({
scrollLeft: step.x || 0,
scrollTop: step.y || 0,
});
} else {
startWaitingForEvents();
await localFrame.evaluate(
(x, y) => {
/* c8 ignore start */
window.scroll(x, y);
/* c8 ignore stop */
},
step.x || 0,
step.y || 0
);
}
break;
}
case StepType.Navigate: {
startWaitingForEvents();
await localFrame.goto(step.url);
break;
}
case StepType.WaitForElement: {
try {
startWaitingForEvents();
await waitForElement(step, localFrame, timeout);
} catch (err) {
if ((err as Error).message === 'Timed out') {
throw new Error(
'waitForElement timed out. The element(s) could not be found.'
);
} else {
throw err;
}
}
break;
}
case StepType.WaitForExpression: {
startWaitingForEvents();
await localFrame.waitForFunction(step.expression, {
timeout,
});
break;
}
case StepType.CustomStep: {
// TODO implement these steps
break;
}
default:
assertAllStepTypesAreHandled(step);
}
await assertedEventsPromise;
}
}
export class PuppeteerRunnerOwningBrowserExtension extends PuppeteerRunnerExtension {
override async afterAllSteps() {
await this.browser.close();
}
}
async function getFrame(pageOrFrame: Page | Frame, step: Step): Promise<Frame> {
let frame =
'mainFrame' in pageOrFrame ? pageOrFrame.mainFrame() : pageOrFrame;
if ('frame' in step && step.frame) {
for (const index of step.frame) {
frame = frame.childFrames()[index]!;
}
}
return frame;
}
async function getTargetPageForStep(
browser: Browser,
page: Page,
step: Step,
timeout: number
): Promise<Page | null> {
if (!step.target || step.target === 'main') {
return page;
}
const target = await browser.waitForTarget((t) => t.url() === step.target, {
timeout,
});
const targetPage = await target.page();
if (!targetPage) {
return null;
}
targetPage.setDefaultTimeout(timeout);
return targetPage;
}
async function waitForEvents(
pageOrFrame: Page | Frame,
step: Step,
timeout: number
): Promise<void> {
const promises: Promise<unknown>[] = [];
if (step.assertedEvents) {
for (const event of step.assertedEvents) {
switch (event.type) {
case AssertedEventType.Navigation: {
promises.push(
pageOrFrame.waitForNavigation({
timeout,
})
);
continue;
}
default:
throw new Error(`Event type ${event.type} is not supported`);
}
}
}
await Promise.all(promises);
}
async function waitForElement(
step: WaitForElementStep,
frame: Frame | Page,
timeout: number
): Promise<void> {
const {
count = 1,
operator = '>=',
visible = true,
properties,
attributes,
} = step;
const compFn = comparators[operator];
await waitForFunction(async () => {
const elements = await querySelectorsAll(step.selectors, frame);
let result = compFn(elements.length, count);
const elementsHandle = await frame.evaluateHandle((...elements) => {
return elements;
}, ...elements);
await Promise.all(elements.map((element) => element.dispose()));
if (result && (properties || attributes)) {
result = await elementsHandle.evaluate(
(elements, properties, attributes) => {
if (attributes) {
for (const element of elements) {
for (const [name, value] of Object.entries(attributes)) {
if (element.getAttribute(name) !== value) {
return false;
}
}
}
}
if (properties) {
for (const element of elements) {
if (!isDeepMatch(properties, element)) {
return false;
}
}
}
return true;
function isDeepMatch<T>(a: T, b: unknown): b is T {
if (a === b) {
return true;
}
if ((a && !b) || (!a && b)) {
return false;
}
if (!(a instanceof Object) || !(b instanceof Object)) {
return false;
}
for (const [key, value] of Object.entries(a)) {
if (!isDeepMatch(value, (b as Record<string, unknown>)[key])) {
return false;
}
}
return true;
}
},
properties,
attributes
);
}
await elementsHandle.dispose();
return result === visible;
}, timeout);
}
async function querySelectorsAll(
selectors: Selector[],
frame: Frame | Page
): Promise<ElementHandle<Element>[]> {
for (const selector of selectors) {
const result = await querySelectorAll(selector, frame);
if (result.length) {
return result;
}
}
return [];
}
async function querySelectorAll(
selector: Selector,
frame: Frame | Page
): Promise<ElementHandle<Element>[]> {
if (!Array.isArray(selector)) {
selector = [selector];
}
if (!selector.length) {
throw new Error('Empty selector provided to querySelectorAll');
}
let elementHandles = await frame.$$(selector[0]!);
if (!elementHandles.length) {
return [];
}
for (const part of selector.slice(1, selector.length)) {
elementHandles = (
await Promise.all(
elementHandles.map(async (handle) => {
const innerHandle = await handle.evaluateHandle((el) =>
el.shadowRoot ? el.shadowRoot : el
);
const elementHandles = await innerHandle.$$(part);
innerHandle.dispose();
handle.dispose();
return elementHandles;
})
)
).flat();
if (!elementHandles.length) {
return [];
}
}
return elementHandles;
}
async function waitForFunction(
fn: () => unknown,
timeout: number
): Promise<void> {
let isActive = true;
const timeoutId = setTimeout(() => {
isActive = false;
}, timeout);
while (isActive) {
const result = await fn();
if (result) {
clearTimeout(timeoutId);
return;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
throw new Error('Timed out');
}