forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.ts
834 lines (719 loc) · 28.4 KB
/
helper.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert, AssertionError} from 'chai';
import type * as puppeteer from 'puppeteer-core';
import {AsyncScope} from '../conductor/async-scope.js';
import type {DevToolsFrontendReloadOptions} from '../conductor/frontend_tab.js';
import {getDevToolsFrontendHostname, reloadDevTools} from '../conductor/hooks.js';
import {platform} from '../conductor/mocha-interface-helpers.js';
import {getBrowserAndPages, getTestServerPort} from '../conductor/puppeteer-state.js';
export {platform} from '../conductor/mocha-interface-helpers.js';
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/naming-convention
__pendingEvents: Map<string, Event[]>;
// eslint-disable-next-line @typescript-eslint/naming-convention
__eventHandlers: WeakMap<Element, Map<string, Promise<void>>>;
// eslint-disable-next-line @typescript-eslint/naming-convention
__getRenderCoordinatorPendingFrames(): number;
}
}
// TODO: Remove once Chromium updates its version of Node.js to 12+.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const globalThis: any = global;
export interface ClickOptions {
root?: puppeteer.JSHandle;
clickOptions?: puppeteer.ClickOptions;
maxPixelsFromLeft?: number;
}
const CONTROL_OR_META = platform === 'mac' ? 'Meta' : 'Control';
export const withControlOrMetaKey = async (action: () => Promise<void>, root = getBrowserAndPages().frontend) => {
await waitForFunction(async () => {
await root.keyboard.down(CONTROL_OR_META);
try {
await action();
return true;
} finally {
await root.keyboard.up(CONTROL_OR_META);
}
});
};
export const click = async (selector: string, options?: ClickOptions) => {
return await performActionOnSelector(
selector, {root: options?.root}, element => element.click(options?.clickOptions));
};
export const hover = async (selector: string, options?: {root?: puppeteer.JSHandle}) => {
return await performActionOnSelector(selector, {root: options?.root}, element => element.hover());
};
type Action = (element: puppeteer.ElementHandle) => Promise<void>;
async function performActionOnSelector(
selector: string, options: {root?: puppeteer.JSHandle}, action: Action): Promise<puppeteer.ElementHandle> {
// TODO(crbug.com/1410168): we should refactor waitFor to be compatible with
// Puppeteer's syntax for selectors.
const queryHandlers = new Set([
'pierceShadowText',
'pierce',
'aria',
'xpath',
'text',
]);
let queryHandler = 'pierce';
for (const handler of queryHandlers) {
const prefix = handler + '/';
if (selector.startsWith(prefix)) {
queryHandler = handler;
selector = selector.substring(prefix.length);
break;
}
}
return waitForFunction(async () => {
const element = await waitFor(selector, options?.root, undefined, queryHandler);
try {
await action(element);
return element;
} catch {
// A bit of delay to not retry too often.
await new Promise(resolve => setTimeout(resolve, 50));
}
return undefined;
});
}
/**
* @deprecated This method is not able to recover from unstable DOM. Use click(selector) instead.
*/
export async function clickElement(element: puppeteer.ElementHandle, options?: ClickOptions): Promise<void> {
// Retries here just in case the element gets connected to DOM / becomes visible.
await waitForFunction(async () => {
try {
await element.click(options?.clickOptions);
return true;
} catch {
return false;
}
});
}
/**
* @deprecated This method is not able to recover from unstable DOM. Use hover(selector) instead.
*/
export async function hoverElement(element: puppeteer.ElementHandle): Promise<void> {
// Retries here just in case the element gets connected to DOM / becomes visible.
await waitForFunction(async () => {
try {
await element.hover();
return true;
} catch {
return false;
}
});
}
export const doubleClick =
async (selector: string, options?: {root?: puppeteer.JSHandle, clickOptions?: puppeteer.ClickOptions}) => {
const passedClickOptions = (options && options.clickOptions) || {};
const clickOptionsWithDoubleClick: puppeteer.ClickOptions = {
...passedClickOptions,
clickCount: 2,
};
return click(selector, {
...options,
clickOptions: clickOptionsWithDoubleClick,
});
};
export const typeText = async (text: string) => {
const {frontend} = getBrowserAndPages();
await frontend.keyboard.type(text);
};
export const pressKey =
async (key: puppeteer.KeyInput, modifiers?: {control?: boolean, alt?: boolean, shift?: boolean}) => {
const {frontend} = getBrowserAndPages();
if (modifiers) {
if (modifiers.control) {
if (platform === 'mac') {
// Use command key on mac
await frontend.keyboard.down('Meta');
} else {
await frontend.keyboard.down('Control');
}
}
if (modifiers.alt) {
await frontend.keyboard.down('Alt');
}
if (modifiers.shift) {
await frontend.keyboard.down('Shift');
}
}
await frontend.keyboard.press(key);
if (modifiers) {
if (modifiers.shift) {
await frontend.keyboard.up('Shift');
}
if (modifiers.alt) {
await frontend.keyboard.up('Alt');
}
if (modifiers.control) {
if (platform === 'mac') {
// Use command key on mac
await frontend.keyboard.up('Meta');
} else {
await frontend.keyboard.up('Control');
}
}
}
};
export const pasteText = async (text: string) => {
const {frontend} = getBrowserAndPages();
await frontend.keyboard.sendCharacter(text);
};
// Get a single element handle. Uses `pierce` handler per default for piercing Shadow DOM.
export const $ =
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle, handler = 'pierce') => {
const {frontend} = getBrowserAndPages();
const rootElement = root ? root as puppeteer.ElementHandle : frontend;
const element = await rootElement.$(`${handler}/${selector}`) as puppeteer.ElementHandle<ElementType>;
return element;
};
// Get multiple element handles. Uses `pierce` handler per default for piercing Shadow DOM.
export const $$ =
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle, handler = 'pierce') => {
const {frontend} = getBrowserAndPages();
const rootElement = root ? root.asElement() || frontend : frontend;
const elements = await rootElement.$$(`${handler}/${selector}`) as puppeteer.ElementHandle<ElementType>[];
return elements;
};
/**
* Search for an element based on its textContent.
*
* @param textContent The text content to search for.
* @param root The root of the search.
*/
export const $textContent = async (textContent: string, root?: puppeteer.JSHandle) => {
return $(textContent, root, 'pierceShadowText');
};
/**
* Search for all elements based on their textContent
*
* @param textContent The text content to search for.
* @param root The root of the search.
*/
export const $$textContent = async (textContent: string, root?: puppeteer.JSHandle) => {
return $$(textContent, root, 'pierceShadowText');
};
export const timeout = (duration: number) => new Promise(resolve => setTimeout(resolve, duration));
export const getTextContent =
async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle) => {
const text = await (await $<ElementType>(selector, root))?.evaluate(node => node.textContent);
return text ?? undefined;
};
export const getAllTextContents =
async(selector: string, root?: puppeteer.JSHandle, handler = 'pierce'): Promise<Array<string|null>> => {
const allElements = await $$(selector, root, handler);
return Promise.all(allElements.map(e => e.evaluate(e => e.textContent)));
};
/**
* Match multiple elements based on a selector and return their textContents, but only for those
* elements that are visible.
*
* @param selector jquery selector to match
* @returns array containing text contents from visible elements
*/
export const getVisibleTextContents = async (selector: string) => {
const allElements = await $$(selector);
const texts = await Promise.all(
allElements.map(el => el.evaluate(node => node.checkVisibility() ? node.textContent?.trim() : undefined)));
return texts.filter(content => typeof (content) === 'string');
};
export const waitFor = async<ElementType extends Element = Element>(
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
return await asyncScope.exec(() => waitForFunction(async () => {
const element = await $<ElementType>(selector, root, handler);
return (element || undefined);
}, asyncScope), `Waiting for element matching selector '${selector}'`);
};
export const waitForVisible = async<ElementType extends Element = Element>(
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
return await asyncScope.exec(() => waitForFunction(async () => {
const element = await $<ElementType>(selector, root, handler);
const visible = await element.evaluate(node => node.checkVisibility());
return visible ? element : undefined;
}, asyncScope), `Waiting for element matching selector '${selector}' to be visible`);
};
export const waitForMany = async (
selector: string, count: number, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
return await asyncScope.exec(() => waitForFunction(async () => {
const elements = await $$(selector, root, handler);
return elements.length >= count ? elements : undefined;
}, asyncScope), `Waiting for ${count} elements to match selector '${selector}'`);
};
export const waitForNone =
async (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => {
return await asyncScope.exec(() => waitForFunction(async () => {
const elements = await $$(selector, root, handler);
if (elements.length === 0) {
return true;
}
return false;
}, asyncScope), `Waiting for no elements to match selector '${selector}'`);
};
export const waitForAria = <ElementType extends Element = Element>(
selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
return waitFor<ElementType>(selector, root, asyncScope, 'aria');
};
export const waitForAriaNone = (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
return waitForNone(selector, root, asyncScope, 'aria');
};
export const waitForElementWithTextContent =
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
return waitFor(textContent, root, asyncScope, 'pierceShadowText');
};
export const waitForElementsWithTextContent =
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
return asyncScope.exec(() => waitForFunction(async () => {
const elems = await $$textContent(textContent, root);
if (elems && elems.length) {
return elems;
}
return undefined;
}, asyncScope), `Waiting for elements with textContent '${textContent}'`);
};
export const waitForNoElementsWithTextContent =
(textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => {
return asyncScope.exec(() => waitForFunction(async () => {
const elems = await $$textContent(textContent, root);
if (elems && elems.length === 0) {
return true;
}
return false;
}, asyncScope), `Waiting for no elements with textContent '${textContent}'`);
};
export const waitForFunction =
async<T>(fn: () => Promise<T|undefined>, asyncScope = new AsyncScope(), description?: string) => {
const innerFunction = async () => {
while (true) {
AsyncScope.abortSignal?.throwIfAborted();
const result = await fn();
AsyncScope.abortSignal?.throwIfAborted();
if (result) {
return result;
}
await timeout(100);
}
};
return await asyncScope.exec(innerFunction, description);
};
export const waitForFunctionWithTries = async<T>(
fn: () => Promise<T|undefined>, options: {tries: number} = {
tries: Number.MAX_SAFE_INTEGER,
},
asyncScope = new AsyncScope()) => {
return await asyncScope.exec(async () => {
let tries = 0;
while (tries++ < options.tries) {
const result = await fn();
if (result) {
return result;
}
await timeout(100);
}
return undefined;
});
};
export const waitForWithTries = async (
selector: string, root?: puppeteer.JSHandle, options: {tries: number} = {
tries: Number.MAX_SAFE_INTEGER,
},
asyncScope = new AsyncScope(), handler?: string) => {
return await asyncScope.exec(() => waitForFunctionWithTries(async () => {
const element = await $(selector, root, handler);
return (element || undefined);
}, options, asyncScope));
};
export const debuggerStatement = (frontend: puppeteer.Page) => {
return frontend.evaluate(() => {
// eslint-disable-next-line no-debugger
debugger;
});
};
export const logToStdOut = (msg: string) => {
if (!process.send) {
return;
}
process.send({
pid: process.pid,
details: msg,
});
};
export const logFailure = () => {
if (!process.send) {
return;
}
process.send({
pid: process.pid,
details: 'failure',
});
};
async function setExperimentEnabled(experiment: string, enabled: boolean, options?: DevToolsFrontendReloadOptions) {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(`(async () => {
const Root = await import('./core/root/root.js');
Root.Runtime.experiments.setEnabled('${experiment}', ${enabled});
})()`);
await reloadDevTools(options);
}
export const enableExperiment = (experiment: string, options?: DevToolsFrontendReloadOptions) =>
setExperimentEnabled(experiment, true, options);
export const disableExperiment = (experiment: string, options?: DevToolsFrontendReloadOptions) =>
setExperimentEnabled(experiment, false, options);
export const setDevToolsSettings = async (settings: Record<string, string>) => {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(settings => {
for (const name in settings) {
globalThis.InspectorFrontendHost.setPreference(name, JSON.stringify(settings[name]));
}
}, settings);
await reloadDevTools();
};
export function goToHtml(html: string): Promise<void> {
return goTo(`data:text/html;charset=utf-8,${encodeURIComponent(html)}`);
}
export const goTo = async (url: string, options: puppeteer.WaitForOptions = {}) => {
const {target} = getBrowserAndPages();
await target.goto(url, options);
};
export const overridePermissions = async (permissions: puppeteer.Permission[]) => {
const {browser} = getBrowserAndPages();
await browser.defaultBrowserContext().overridePermissions(`https://localhost:${getTestServerPort()}`, permissions);
};
export const clearPermissionsOverride = async () => {
const {browser} = getBrowserAndPages();
await browser.defaultBrowserContext().clearPermissionOverrides();
};
export const goToResource = async (path: string, options: puppeteer.WaitForOptions = {}) => {
await goTo(`${getResourcesPath()}/${path}`, options);
};
export const goToResourceWithCustomHost = async (host: string, path: string) => {
assert.isTrue(host.endsWith('.test'), 'Only custom hosts with a .test domain are allowed.');
await goTo(`${getResourcesPath(host)}/${path}`);
};
export const getResourcesPath = (host: string = 'localhost') => {
return `https://${host}:${getTestServerPort()}/test/e2e/resources`;
};
export const step = async (description: string, step: Function) => {
try {
return await step();
} catch (error) {
if (error instanceof AssertionError) {
throw new AssertionError(
`Unexpected Result in Step "${description}"
${error.message}`,
error);
} else {
error.message += ` in Step "${description}"`;
throw error;
}
}
};
export const waitForAnimationFrame = async () => {
const {frontend} = getBrowserAndPages();
await frontend.waitForFunction(() => {
return new Promise(resolve => {
requestAnimationFrame(resolve);
});
});
};
export const activeElement = async () => {
const {frontend} = getBrowserAndPages();
await waitForAnimationFrame();
return frontend.evaluateHandle(() => {
let activeElement = document.activeElement;
while (activeElement && activeElement.shadowRoot) {
activeElement = activeElement.shadowRoot.activeElement;
}
if (!activeElement) {
throw new Error('No active element found');
}
return activeElement;
});
};
export const activeElementTextContent = async () => {
const element = await activeElement();
return element.evaluate(node => node.textContent);
};
export const activeElementAccessibleName = async () => {
const element = await activeElement();
return element.evaluate(node => node.getAttribute('aria-label') || node.getAttribute('title'));
};
export const tabForward = async (page?: puppeteer.Page) => {
let targetPage: puppeteer.Page;
if (page) {
targetPage = page;
} else {
const {frontend} = getBrowserAndPages();
targetPage = frontend;
}
await targetPage.keyboard.press('Tab');
};
export const tabBackward = async (page?: puppeteer.Page) => {
let targetPage: puppeteer.Page;
if (page) {
targetPage = page;
} else {
const {frontend} = getBrowserAndPages();
targetPage = frontend;
}
await targetPage.keyboard.down('Shift');
await targetPage.keyboard.press('Tab');
await targetPage.keyboard.up('Shift');
};
type Awaitable<T> = T|PromiseLike<T>;
export const selectTextFromNodeToNode = async (
from: Awaitable<puppeteer.ElementHandle>, to: Awaitable<puppeteer.ElementHandle>, direction: 'up'|'down') => {
const {target} = getBrowserAndPages();
// The clipboard api does not allow you to copy, unless the tab is focused.
await target.bringToFront();
return target.evaluate(async (from, to, direction) => {
const selection = (from.getRootNode() as Document).getSelection();
const range = document.createRange();
if (direction === 'down') {
range.setStartBefore(from);
range.setEndAfter(to);
} else {
range.setStartBefore(to);
range.setEndAfter(from);
}
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
document.execCommand('copy');
return navigator.clipboard.readText();
}, await from, await to, direction);
};
export const clickMoreTabsButton = async (root?: puppeteer.ElementHandle<Element>) => {
await click('aria/More tabs', {root});
};
export const closePanelTab = async (panelTabSelector: string) => {
// Get close button from tab element
const selector = `${panelTabSelector} > .tabbed-pane-close-button`;
await click(selector);
await waitForNone(selector);
};
export const closeAllCloseableTabs = async () => {
// get all closeable tools by looking for the available x buttons on tabs
const selector = '.tabbed-pane-close-button';
const allCloseButtons = await $$(selector);
// Get all panel ids
const panelTabIds = await Promise.all(allCloseButtons.map(button => {
return button.evaluate(button => button.parentElement ? button.parentElement.id : '');
}));
// Close each tab
for (const tabId of panelTabIds) {
const selector = `#${tabId}`;
await closePanelTab(selector);
}
};
// Noisy! Do not leave this in your test but it may be helpful
// when debugging.
export const enableCDPLogging = async () => {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(() => {
globalThis.ProtocolClient.test.dumpProtocol = console.log; // eslint-disable-line no-console
});
};
export const enableCDPTracking = async () => {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(() => {
globalThis.__messageMapForTest = new Map();
globalThis.ProtocolClient.test.onMessageSent = (message: {method: string, id: number}) => {
globalThis.__messageMapForTest.set(message.id, message.method);
};
globalThis.ProtocolClient.test.onMessageReceived = (message: {id?: number}) => {
if (message.id) {
globalThis.__messageMapForTest.delete(message.id);
}
};
});
};
export const logOutstandingCDP = async () => {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(() => {
for (const entry of globalThis.__messageMapForTest) {
console.error(entry);
}
});
};
export const selectOption = async (select: puppeteer.ElementHandle<HTMLSelectElement>, value: string) => {
await select.evaluate(async (node: HTMLSelectElement, _value: string) => {
node.value = _value;
const event = document.createEvent('HTMLEvents');
event.initEvent('change', false, true);
node.dispatchEvent(event);
}, value);
};
export const scrollElementIntoView = async (selector: string, root?: puppeteer.JSHandle) => {
const element = await $(selector, root);
if (!element) {
throw new Error(`Unable to find element with selector "${selector}"`);
}
await element.evaluate(el => {
el.scrollIntoView();
});
};
export const installEventListener = function(frontend: puppeteer.Page, eventType: string) {
return frontend.evaluate(eventType => {
window.__pendingEvents = window.__pendingEvents || new Map();
window.addEventListener(eventType, (e: Event) => {
let events = window.__pendingEvents.get(eventType);
if (!events) {
events = [];
window.__pendingEvents.set(eventType, events);
}
events.push(e);
});
}, eventType);
};
export const getPendingEvents = function(frontend: puppeteer.Page, eventType: string): Promise<Event[]|undefined> {
return frontend.evaluate(eventType => {
if (!('__pendingEvents' in window)) {
return undefined;
}
const pendingEvents = window.__pendingEvents.get(eventType);
window.__pendingEvents.set(eventType, []);
return pendingEvents;
}, eventType);
};
export function prepareWaitForEvent(element: puppeteer.ElementHandle, eventType: string): Promise<void> {
return element.evaluate((element: Element, eventType: string) => {
window.__eventHandlers = window.__eventHandlers || new WeakMap();
const eventHandlers = (() => {
const eventHandlers = window.__eventHandlers.get(element);
if (eventHandlers) {
return eventHandlers;
}
const newMap = new Map<string, Promise<void>>();
window.__eventHandlers.set(element, newMap);
return newMap;
})();
if (eventHandlers.has(eventType)) {
throw new Error(`Event listener for ${eventType}' has already been installed.`);
}
eventHandlers.set(eventType, new Promise<void>(resolve => {
const handler = () => {
element.removeEventListener(eventType, handler);
resolve();
};
element.addEventListener(eventType, handler);
}));
}, eventType);
}
export function waitForEvent(element: puppeteer.ElementHandle, eventType: string): Promise<void> {
return element.evaluate((element: Element, eventType: string) => {
if (!('__eventHandlers' in window)) {
throw new Error(`Event listener for '${eventType}' has not been installed.`);
}
const handler = window.__eventHandlers.get(element)?.get(eventType);
if (!handler) {
throw new Error(`Event listener for '${eventType}' has not been installed.`);
}
return handler;
}, eventType);
}
export const hasClass = async (element: puppeteer.ElementHandle<Element>, classname: string) => {
return await element.evaluate((el, classname) => el.classList.contains(classname), classname);
};
export const waitForClass = async (element: puppeteer.ElementHandle<Element>, classname: string) => {
await waitForFunction(async () => {
return hasClass(element, classname);
});
};
/**
* This is useful to keep TypeScript happy in a test - if you have a value
* that's potentially `null` you can use this function to assert that it isn't,
* and satisfy TypeScript that the value is present.
*/
export function assertNotNullOrUndefined<T>(val: T): asserts val is NonNullable<T> {
if (val === null || val === undefined) {
throw new Error(`Expected given value to not be null/undefined but it was: ${val}`);
}
}
export {getBrowserAndPages, getDevToolsFrontendHostname, getTestServerPort, reloadDevTools};
export function matchString(actual: string, expected: string|RegExp): true|string {
if (typeof expected === 'string') {
if (actual !== expected) {
return `Expected item "${actual}" to equal "${expected}"`;
}
} else if (!expected.test(actual)) {
return `Expected item "${actual}" to match "${expected}"`;
}
return true;
}
export function matchArray<A, E>(
actual: A[], expected: E[], comparator: (actual: A, expected: E) => true | string): true|string {
if (actual.length !== expected.length) {
return `Expected [${actual.map(x => `"${x}"`).join(', ')}] to have length ${expected.length}`;
}
for (let i = 0; i < expected.length; ++i) {
const result = comparator(actual[i], expected[i]);
if (result !== true) {
return `Mismatch in row ${i}: ${result}`;
}
}
return true;
}
export function assertOk<Args extends unknown[]>(check: (...args: Args) => true | string) {
return (...args: Args) => {
const result = check(...args);
if (result !== true) {
throw new AssertionError(result);
}
};
}
export function matchTable<A, E>(
actual: A[][], expected: E[][], comparator: (actual: A, expected: E) => true | string) {
return matchArray(actual, expected, (actual, expected) => matchArray<A, E>(actual, expected, comparator));
}
export const matchStringArray = (actual: string[], expected: (string|RegExp)[]) =>
matchArray(actual, expected, matchString);
export const assertMatchArray = assertOk(matchStringArray);
export const matchStringTable = (actual: string[][], expected: (string|RegExp)[][]) =>
matchTable(actual, expected, matchString);
export async function renderCoordinatorQueueEmpty(): Promise<void> {
const {frontend} = getBrowserAndPages();
await frontend.evaluate(() => {
return new Promise<void>(resolve => {
const pendingFrames = globalThis.__getRenderCoordinatorPendingFrames();
if (pendingFrames < 1) {
resolve();
return;
}
globalThis.addEventListener('renderqueueempty', resolve, {once: true});
});
});
}
export async function setCheckBox(selector: string, wantChecked: boolean): Promise<void> {
const checkbox = await waitFor(selector);
const checked = await checkbox.evaluate(box => (box as HTMLInputElement).checked);
if (checked !== wantChecked) {
await click(`${selector} + label`);
}
assert.strictEqual(await checkbox.evaluate(box => (box as HTMLInputElement).checked), wantChecked);
}
export const summonSearchBox = async () => {
await pressKey('f', {control: true});
};
export const replacePuppeteerUrl = (value: string) => {
return value.replace(/pptr:.*:([0-9]+)$/, (_, match) => {
return `(index):${match}`;
});
};
export async function raf(page: puppeteer.Page): Promise<void> {
await page.evaluate(() => {
return new Promise(resolve => window.requestAnimationFrame(resolve));
});
}
export async function readClipboard() {
const {frontend, browser} = getBrowserAndPages();
await browser.defaultBrowserContext().overridePermissions(frontend.url(), ['clipboard-read']);
const clipboard = await frontend.evaluate(async () => navigator.clipboard.readText());
await browser.defaultBrowserContext().clearPermissionOverrides();
return clipboard;
}