Paul Lewis | b8b3801 | 2020-01-22 17:18:47 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Rudenko | d659413 | 2021-02-04 11:58:06 | [diff] [blame] | 5 | import {assert, AssertionError} from 'chai'; |
Tim van der Lippe | 869374b | 2020-04-20 10:12:31 | [diff] [blame] | 6 | import * as os from 'os'; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 7 | import type * as puppeteer from 'puppeteer-core'; |
Tim van der Lippe | 869374b | 2020-04-20 10:12:31 | [diff] [blame] | 8 | |
Benedikt Meurer | 8a7915a | 2022-10-20 05:43:29 | [diff] [blame] | 9 | import {type DevToolsFrontendReloadOptions} from '../conductor/frontend_tab.js'; |
Simon Zünd | e3cdd37 | 2022-02-16 09:36:55 | [diff] [blame] | 10 | import {getDevToolsFrontendHostname, reloadDevTools} from '../conductor/hooks.js'; |
Jack Franklin | 3b3a782 | 2020-12-03 10:12:46 | [diff] [blame] | 11 | import {getBrowserAndPages, getTestServerPort} from '../conductor/puppeteer-state.js'; |
Philip Pfaffe | 9401ae1 | 2022-08-16 09:17:16 | [diff] [blame] | 12 | |
Takuto Ikuta | a36c4e8 | 2022-01-17 07:23:54 | [diff] [blame] | 13 | import {AsyncScope} from './async-scope.js'; |
Paul Lewis | 36122ad | 2020-02-28 12:30:00 | [diff] [blame] | 14 | |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 15 | declare global { |
Tim van der Lippe | 75c2c9c | 2020-12-01 12:50:53 | [diff] [blame] | 16 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 17 | interface Window { |
Sigurd Schneider | 123977a | 2021-02-15 14:17:46 | [diff] [blame] | 18 | // eslint-disable-next-line @typescript-eslint/naming-convention |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 19 | __pendingEvents: Map<string, Event[]>; |
Jack Franklin | a3f2d16 | 2021-10-01 11:30:21 | [diff] [blame] | 20 | |
| 21 | // eslint-disable-next-line @typescript-eslint/naming-convention |
Philip Pfaffe | 7fb6a83 | 2022-11-14 13:39:47 | [diff] [blame] | 22 | __eventHandlers: WeakMap<Element, Map<string, Promise<void>>>; |
| 23 | |
| 24 | // eslint-disable-next-line @typescript-eslint/naming-convention |
Jack Franklin | a3f2d16 | 2021-10-01 11:30:21 | [diff] [blame] | 25 | __getRenderCoordinatorPendingFrames(): number; |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
Alex Rudenko | 27d1203 | 2021-02-18 13:07:33 | [diff] [blame] | 29 | export type Platform = 'mac'|'win32'|'linux'; |
| 30 | export let platform: Platform; |
Paul Lewis | 36122ad | 2020-02-28 12:30:00 | [diff] [blame] | 31 | switch (os.platform()) { |
| 32 | case 'darwin': |
| 33 | platform = 'mac'; |
| 34 | break; |
| 35 | |
| 36 | case 'win32': |
| 37 | platform = 'win32'; |
| 38 | break; |
| 39 | |
| 40 | default: |
| 41 | platform = 'linux'; |
| 42 | break; |
| 43 | } |
Paul Lewis | b8b3801 | 2020-01-22 17:18:47 | [diff] [blame] | 44 | |
Mathias Bynens | bb54eb5 | 2020-01-28 11:24:27 | [diff] [blame] | 45 | // TODO: Remove once Chromium updates its version of Node.js to 12+. |
Paul Lewis | 839037f | 2020-07-21 12:25:19 | [diff] [blame] | 46 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Mathias Bynens | bb54eb5 | 2020-01-28 11:24:27 | [diff] [blame] | 47 | const globalThis: any = global; |
| 48 | |
Jesper van den Ende | 0113884 | 2021-11-22 23:59:39 | [diff] [blame] | 49 | export interface ClickOptions { |
| 50 | root?: puppeteer.JSHandle; |
Randolf | 18fb49b | 2023-01-30 08:38:58 | [diff] [blame] | 51 | clickOptions?: puppeteer.ClickOptions; |
Jesper van den Ende | 0113884 | 2021-11-22 23:59:39 | [diff] [blame] | 52 | maxPixelsFromLeft?: number; |
| 53 | } |
| 54 | |
Randolf | 18fb49b | 2023-01-30 08:38:58 | [diff] [blame] | 55 | const CONTROL_OR_META = platform === 'mac' ? 'Meta' : 'Control'; |
| 56 | export const withControlOrMetaKey = async (action: () => Promise<void>, root = getBrowserAndPages().frontend) => { |
| 57 | await waitForFunction(async () => { |
| 58 | await root.keyboard.down(CONTROL_OR_META); |
| 59 | try { |
| 60 | await action(); |
| 61 | return true; |
| 62 | } finally { |
| 63 | await root.keyboard.up(CONTROL_OR_META); |
| 64 | } |
| 65 | }); |
| 66 | }; |
Sigurd Schneider | a7d1bb1 | 2021-04-30 06:45:37 | [diff] [blame] | 67 | |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 68 | export const click = async (selector: string, options?: ClickOptions) => { |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 69 | return await performActionOnSelector( |
| 70 | selector, {root: options?.root}, element => element.click(options?.clickOptions)); |
| 71 | }; |
| 72 | |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 73 | export const hover = async (selector: string, options?: {root?: puppeteer.JSHandle}) => { |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 74 | return await performActionOnSelector(selector, {root: options?.root}, element => element.hover()); |
| 75 | }; |
| 76 | |
| 77 | type Action = (element: puppeteer.ElementHandle) => Promise<void>; |
| 78 | |
| 79 | async function performActionOnSelector( |
| 80 | selector: string, options: {root?: puppeteer.JSHandle}, action: Action): Promise<puppeteer.ElementHandle> { |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 81 | // TODO(crbug.com/1410168): we should refactor waitFor to be compatible with |
| 82 | // Puppeteer's syntax for selectors. |
| 83 | const queryHandlers = new Set([ |
| 84 | 'pierceShadowText', |
| 85 | 'pierce', |
| 86 | 'aria', |
| 87 | 'xpath', |
| 88 | 'text', |
| 89 | ]); |
| 90 | let queryHandler = 'pierce'; |
| 91 | for (const handler of queryHandlers) { |
| 92 | const prefix = handler + '/'; |
| 93 | if (selector.startsWith(prefix)) { |
| 94 | queryHandler = handler; |
| 95 | selector = selector.substring(prefix.length); |
| 96 | break; |
| 97 | } |
Tim van der Lippe | 2ab3956 | 2020-02-05 16:47:11 | [diff] [blame] | 98 | } |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 99 | return waitForFunction(async () => { |
| 100 | const element = await waitFor(selector, options?.root, undefined, queryHandler); |
| 101 | try { |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 102 | await action(element); |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 103 | return element; |
| 104 | } catch (err) { |
| 105 | // A bit of delay to not retry too often. |
| 106 | await new Promise(resolve => setTimeout(resolve, 50)); |
| 107 | } |
| 108 | return undefined; |
| 109 | }); |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 110 | } |
Tim van der Lippe | 2ab3956 | 2020-02-05 16:47:11 | [diff] [blame] | 111 | |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 112 | /** |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 113 | * @deprecated This method is not able to recover from unstable DOM. Use click(selector) instead. |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 114 | */ |
| 115 | export async function clickElement(element: puppeteer.ElementHandle, options?: ClickOptions): Promise<void> { |
Alex Rudenko | 1356402 | 2023-01-31 12:16:44 | [diff] [blame] | 116 | // Retries here just in case the element gets connected to DOM / becomes visible. |
| 117 | await waitForFunction(async () => { |
| 118 | try { |
| 119 | await element.click(options?.clickOptions); |
| 120 | return true; |
| 121 | } catch { |
| 122 | return false; |
| 123 | } |
| 124 | }); |
Alex Rudenko | e92fe9d | 2023-01-30 13:12:23 | [diff] [blame] | 125 | } |
| 126 | |
Alex Rudenko | afbc6c6 | 2023-01-31 10:49:03 | [diff] [blame] | 127 | /** |
| 128 | * @deprecated This method is not able to recover from unstable DOM. Use hover(selector) instead. |
| 129 | */ |
| 130 | export async function hoverElement(element: puppeteer.ElementHandle): Promise<void> { |
| 131 | // Retries here just in case the element gets connected to DOM / becomes visible. |
| 132 | await waitForFunction(async () => { |
| 133 | try { |
| 134 | await element.hover(); |
| 135 | return true; |
| 136 | } catch { |
| 137 | return false; |
| 138 | } |
| 139 | }); |
| 140 | } |
| 141 | |
Jack Franklin | 60f529d | 2020-03-26 16:58:09 | [diff] [blame] | 142 | export const doubleClick = |
Sigurd Schneider | 1576186 | 2021-02-04 08:05:36 | [diff] [blame] | 143 | async (selector: string, options?: {root?: puppeteer.JSHandle, clickOptions?: puppeteer.ClickOptions}) => { |
Johan Bay | 00fc92a | 2020-08-14 20:06:20 | [diff] [blame] | 144 | const passedClickOptions = (options && options.clickOptions) || {}; |
Jack Franklin | 60f529d | 2020-03-26 16:58:09 | [diff] [blame] | 145 | const clickOptionsWithDoubleClick: puppeteer.ClickOptions = { |
| 146 | ...passedClickOptions, |
| 147 | clickCount: 2, |
| 148 | }; |
| 149 | return click(selector, { |
| 150 | ...options, |
| 151 | clickOptions: clickOptionsWithDoubleClick, |
| 152 | }); |
| 153 | }; |
| 154 | |
Peter Marshall | 3181426 | 2020-03-11 13:31:09 | [diff] [blame] | 155 | export const typeText = async (text: string) => { |
Tim van der Lippe | 869374b | 2020-04-20 10:12:31 | [diff] [blame] | 156 | const {frontend} = getBrowserAndPages(); |
Peter Marshall | 3181426 | 2020-03-11 13:31:09 | [diff] [blame] | 157 | await frontend.keyboard.type(text); |
| 158 | }; |
| 159 | |
Tim van der Lippe | d79e3c5 | 2021-09-01 13:31:24 | [diff] [blame] | 160 | export const pressKey = |
| 161 | async (key: puppeteer.KeyInput, modifiers?: {control?: boolean, alt?: boolean, shift?: boolean}) => { |
Eric Leese | 6634255 | 2020-07-06 16:43:35 | [diff] [blame] | 162 | const {frontend} = getBrowserAndPages(); |
| 163 | if (modifiers) { |
| 164 | if (modifiers.control) { |
| 165 | if (platform === 'mac') { |
| 166 | // Use command key on mac |
| 167 | await frontend.keyboard.down('Meta'); |
| 168 | } else { |
| 169 | await frontend.keyboard.down('Control'); |
| 170 | } |
| 171 | } |
| 172 | if (modifiers.alt) { |
| 173 | await frontend.keyboard.down('Alt'); |
| 174 | } |
| 175 | if (modifiers.shift) { |
| 176 | await frontend.keyboard.down('Shift'); |
| 177 | } |
| 178 | } |
Tim van der Lippe | d79e3c5 | 2021-09-01 13:31:24 | [diff] [blame] | 179 | await frontend.keyboard.press(key); |
Eric Leese | 6634255 | 2020-07-06 16:43:35 | [diff] [blame] | 180 | if (modifiers) { |
| 181 | if (modifiers.shift) { |
| 182 | await frontend.keyboard.up('Shift'); |
| 183 | } |
| 184 | if (modifiers.alt) { |
| 185 | await frontend.keyboard.up('Alt'); |
| 186 | } |
| 187 | if (modifiers.control) { |
| 188 | if (platform === 'mac') { |
| 189 | // Use command key on mac |
| 190 | await frontend.keyboard.up('Meta'); |
| 191 | } else { |
| 192 | await frontend.keyboard.up('Control'); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | }; |
| 197 | |
Almothana Athamneh | f55afd7 | 2020-05-28 11:26:46 | [diff] [blame] | 198 | export const pasteText = async (text: string) => { |
| 199 | const {frontend} = getBrowserAndPages(); |
Almothana Athamneh | f55afd7 | 2020-05-28 11:26:46 | [diff] [blame] | 200 | await frontend.keyboard.sendCharacter(text); |
| 201 | }; |
| 202 | |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 203 | // Get a single element handle. Uses `pierce` handler per default for piercing Shadow DOM. |
Jack Franklin | fda44a1 | 2021-08-23 11:04:21 | [diff] [blame] | 204 | export const $ = |
| 205 | async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle, handler = 'pierce') => { |
Tim van der Lippe | 869374b | 2020-04-20 10:12:31 | [diff] [blame] | 206 | const {frontend} = getBrowserAndPages(); |
Johan Bay | e824571 | 2020-08-04 13:32:10 | [diff] [blame] | 207 | const rootElement = root ? root as puppeteer.ElementHandle : frontend; |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 208 | const element = await rootElement.$(`${handler}/${selector}`) as puppeteer.ElementHandle<ElementType>; |
Simon Zünd | 9e2c759 | 2023-01-17 07:59:41 | [diff] [blame] | 209 | return element; |
Mathias Bynens | bb54eb5 | 2020-01-28 11:24:27 | [diff] [blame] | 210 | }; |
Paul Lewis | b8b3801 | 2020-01-22 17:18:47 | [diff] [blame] | 211 | |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 212 | // Get multiple element handles. Uses `pierce` handler per default for piercing Shadow DOM. |
Jack Franklin | fda44a1 | 2021-08-23 11:04:21 | [diff] [blame] | 213 | export const $$ = |
| 214 | async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle, handler = 'pierce') => { |
Tim van der Lippe | 869374b | 2020-04-20 10:12:31 | [diff] [blame] | 215 | const {frontend} = getBrowserAndPages(); |
Johan Bay | e824571 | 2020-08-04 13:32:10 | [diff] [blame] | 216 | const rootElement = root ? root.asElement() || frontend : frontend; |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 217 | const elements = await rootElement.$$(`${handler}/${selector}`) as puppeteer.ElementHandle<ElementType>[]; |
Simon Zünd | 9e2c759 | 2023-01-17 07:59:41 | [diff] [blame] | 218 | return elements; |
Tim van der Lippe | a848518 | 2020-02-06 17:27:39 | [diff] [blame] | 219 | }; |
| 220 | |
Paul Lewis | ab0c65c | 2020-03-27 10:25:09 | [diff] [blame] | 221 | /** |
| 222 | * Search for an element based on its textContent. |
| 223 | * |
| 224 | * @param textContent The text content to search for. |
| 225 | * @param root The root of the search. |
| 226 | */ |
| 227 | export const $textContent = async (textContent: string, root?: puppeteer.JSHandle) => { |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 228 | return $(textContent, root, 'pierceShadowText'); |
Paul Lewis | ab0c65c | 2020-03-27 10:25:09 | [diff] [blame] | 229 | }; |
| 230 | |
Jack Franklin | f56bc38 | 2020-09-11 14:00:36 | [diff] [blame] | 231 | /** |
| 232 | * Search for all elements based on their textContent |
| 233 | * |
| 234 | * @param textContent The text content to search for. |
| 235 | * @param root The root of the search. |
| 236 | */ |
| 237 | export const $$textContent = async (textContent: string, root?: puppeteer.JSHandle) => { |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 238 | return $$(textContent, root, 'pierceShadowText'); |
Jack Franklin | f56bc38 | 2020-09-11 14:00:36 | [diff] [blame] | 239 | }; |
| 240 | |
Jack Franklin | e245d1a | 2020-02-13 15:25:13 | [diff] [blame] | 241 | export const timeout = (duration: number) => new Promise(resolve => setTimeout(resolve, duration)); |
Tim van der Lippe | 011fa18 | 2020-02-12 17:12:27 | [diff] [blame] | 242 | |
Charles Vazac | bfbdafa | 2024-01-18 16:22:50 | [diff] [blame] | 243 | export const getTextContent = |
| 244 | async<ElementType extends Element = Element>(selector: string, root?: puppeteer.JSHandle) => { |
| 245 | const text = await (await $<ElementType>(selector, root))?.evaluate(node => node.textContent); |
PhistucK | 257ad69 | 2022-08-29 14:36:19 | [diff] [blame] | 246 | return text ?? undefined; |
| 247 | }; |
| 248 | |
Kim-Anh Tran | 429e8b7 | 2024-03-20 09:12:02 | [diff] [blame] | 249 | export const getAllTextContents = |
| 250 | async(selector: string, root?: puppeteer.JSHandle, handler = 'pierce'): Promise<Array<string|null>> => { |
| 251 | const allElements = await $$(selector, root, handler); |
| 252 | return Promise.all(allElements.map(e => e.evaluate(e => e.textContent))); |
| 253 | }; |
| 254 | |
Eric Leese | 88fd441 | 2023-05-16 11:22:05 | [diff] [blame] | 255 | /** |
| 256 | * Match multiple elements based on a selector and return their textContents, but only for those |
| 257 | * elements that are visible. |
| 258 | * |
| 259 | * @param selector jquery selector to match |
| 260 | * @returns array containing text contents from visible elements |
| 261 | */ |
| 262 | export const getVisibleTextContents = async (selector: string) => { |
| 263 | const allElements = await $$(selector); |
| 264 | const texts = await Promise.all( |
| 265 | allElements.map(el => el.evaluate(node => node.checkVisibility() ? node.textContent?.trim() : undefined))); |
| 266 | return texts.filter(content => typeof (content) === 'string'); |
| 267 | }; |
| 268 | |
Jack Franklin | fda44a1 | 2021-08-23 11:04:21 | [diff] [blame] | 269 | export const waitFor = async<ElementType extends Element = Element>( |
| 270 | selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => { |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 271 | return await asyncScope.exec(() => waitForFunction(async () => { |
Jack Franklin | fda44a1 | 2021-08-23 11:04:21 | [diff] [blame] | 272 | const element = await $<ElementType>(selector, root, handler); |
Johan Bay | 504a6f1 | 2020-08-04 13:32:53 | [diff] [blame] | 273 | return (element || undefined); |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 274 | }, asyncScope), `Waiting for element matching selector '${selector}'`); |
Tim van der Lippe | 68efc70 | 2020-03-03 17:27:45 | [diff] [blame] | 275 | }; |
| 276 | |
Eric Leese | 88fd441 | 2023-05-16 11:22:05 | [diff] [blame] | 277 | export const waitForVisible = async<ElementType extends Element = Element>( |
| 278 | selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => { |
| 279 | return await asyncScope.exec(() => waitForFunction(async () => { |
| 280 | const element = await $<ElementType>(selector, root, handler); |
| 281 | const visible = await element.evaluate(node => node.checkVisibility()); |
| 282 | return visible ? element : undefined; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 283 | }, asyncScope), `Waiting for element matching selector '${selector}' to be visible`); |
Eric Leese | 88fd441 | 2023-05-16 11:22:05 | [diff] [blame] | 284 | }; |
| 285 | |
Philip Pfaffe | 7426b00 | 2020-11-30 15:59:22 | [diff] [blame] | 286 | export const waitForMany = async ( |
| 287 | selector: string, count: number, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => { |
| 288 | return await asyncScope.exec(() => waitForFunction(async () => { |
| 289 | const elements = await $$(selector, root, handler); |
| 290 | return elements.length >= count ? elements : undefined; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 291 | }, asyncScope), `Waiting for ${count} elements to match selector '${selector}'`); |
Philip Pfaffe | 7426b00 | 2020-11-30 15:59:22 | [diff] [blame] | 292 | }; |
| 293 | |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 294 | export const waitForNone = |
| 295 | async (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope(), handler?: string) => { |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 296 | return await asyncScope.exec(() => waitForFunction(async () => { |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 297 | const elements = await $$(selector, root, handler); |
Johan Bay | e824571 | 2020-08-04 13:32:10 | [diff] [blame] | 298 | if (elements.length === 0) { |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 299 | return true; |
| 300 | } |
| 301 | return false; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 302 | }, asyncScope), `Waiting for no elements to match selector '${selector}'`); |
Peter Marshall | 3181426 | 2020-03-11 13:31:09 | [diff] [blame] | 303 | }; |
| 304 | |
Johan Bay | f04b99c | 2020-11-03 15:38:41 | [diff] [blame] | 305 | export const waitForAria = (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => { |
| 306 | return waitFor(selector, root, asyncScope, 'aria'); |
| 307 | }; |
| 308 | |
Kateryna Prokopenko | 227106d | 2021-10-11 15:38:16 | [diff] [blame] | 309 | export const waitForAriaNone = (selector: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => { |
| 310 | return waitForNone(selector, root, asyncScope, 'aria'); |
| 311 | }; |
| 312 | |
Peter Marshall | c059be1 | 2020-07-10 14:17:17 | [diff] [blame] | 313 | export const waitForElementWithTextContent = |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 314 | (textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => { |
Johan Bay | b01fc26 | 2020-11-02 12:38:46 | [diff] [blame] | 315 | return waitFor(textContent, root, asyncScope, 'pierceShadowText'); |
Peter Marshall | c059be1 | 2020-07-10 14:17:17 | [diff] [blame] | 316 | }; |
Paul Lewis | ab0c65c | 2020-03-27 10:25:09 | [diff] [blame] | 317 | |
Jack Franklin | f56bc38 | 2020-09-11 14:00:36 | [diff] [blame] | 318 | export const waitForElementsWithTextContent = |
| 319 | (textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => { |
| 320 | return asyncScope.exec(() => waitForFunction(async () => { |
| 321 | const elems = await $$textContent(textContent, root); |
| 322 | if (elems && elems.length) { |
| 323 | return elems; |
| 324 | } |
| 325 | |
| 326 | return undefined; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 327 | }, asyncScope), `Waiting for elements with textContent '${textContent}'`); |
Jack Franklin | f56bc38 | 2020-09-11 14:00:36 | [diff] [blame] | 328 | }; |
| 329 | |
Jack Lynch | d529e88 | 2020-11-03 19:17:27 | [diff] [blame] | 330 | export const waitForNoElementsWithTextContent = |
| 331 | (textContent: string, root?: puppeteer.JSHandle, asyncScope = new AsyncScope()) => { |
| 332 | return asyncScope.exec(() => waitForFunction(async () => { |
| 333 | const elems = await $$textContent(textContent, root); |
| 334 | if (elems && elems.length === 0) { |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | return false; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 339 | }, asyncScope), `Waiting for no elements with textContent '${textContent}'`); |
Jack Lynch | d529e88 | 2020-11-03 19:17:27 | [diff] [blame] | 340 | }; |
| 341 | |
Andres Olivares | 9ef8a01 | 2024-01-16 11:41:03 | [diff] [blame] | 342 | export const TIMEOUT_ERROR_MESSAGE = 'Test timed out'; |
| 343 | |
Eric Leese | 23e6a0b | 2023-08-14 12:26:32 | [diff] [blame] | 344 | export const waitForFunction = |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 345 | async<T>(fn: () => Promise<T|undefined>, asyncScope = new AsyncScope(), description?: string) => { |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 346 | const innerFunction = async () => { |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 347 | while (true) { |
Philip Pfaffe | d097e51 | 2021-06-22 09:56:46 | [diff] [blame] | 348 | if (asyncScope.isCanceled()) { |
Andres Olivares | 9ef8a01 | 2024-01-16 11:41:03 | [diff] [blame] | 349 | throw new Error(TIMEOUT_ERROR_MESSAGE); |
Philip Pfaffe | d097e51 | 2021-06-22 09:56:46 | [diff] [blame] | 350 | } |
Philip Pfaffe | 6d143ae | 2020-07-31 11:32:22 | [diff] [blame] | 351 | const result = await fn(); |
| 352 | if (result) { |
| 353 | return result; |
| 354 | } |
Johan Bay | 00fc92a | 2020-08-14 20:06:20 | [diff] [blame] | 355 | await timeout(100); |
Paul Lewis | 11cc3e8 | 2020-02-04 13:42:56 | [diff] [blame] | 356 | } |
Eric Leese | 23e6a0b | 2023-08-14 12:26:32 | [diff] [blame] | 357 | }; |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 358 | return await asyncScope.exec(innerFunction, description); |
Jack Franklin | e245d1a | 2020-02-13 15:25:13 | [diff] [blame] | 359 | }; |
Paul Lewis | 11cc3e8 | 2020-02-04 13:42:56 | [diff] [blame] | 360 | |
Sigurd Schneider | c3f5d19 | 2021-05-18 11:40:45 | [diff] [blame] | 361 | export const waitForFunctionWithTries = async<T>( |
| 362 | fn: () => Promise<T|undefined>, options: {tries: number} = { |
| 363 | tries: Number.MAX_SAFE_INTEGER, |
| 364 | }, |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 365 | asyncScope = new AsyncScope()) => { |
Sigurd Schneider | c3f5d19 | 2021-05-18 11:40:45 | [diff] [blame] | 366 | return await asyncScope.exec(async () => { |
| 367 | let tries = 0; |
| 368 | while (tries++ < options.tries) { |
| 369 | const result = await fn(); |
| 370 | if (result) { |
| 371 | return result; |
| 372 | } |
| 373 | await timeout(100); |
| 374 | } |
| 375 | return undefined; |
| 376 | }); |
| 377 | }; |
| 378 | |
| 379 | export const waitForWithTries = async ( |
| 380 | selector: string, root?: puppeteer.JSHandle, options: {tries: number} = { |
| 381 | tries: Number.MAX_SAFE_INTEGER, |
| 382 | }, |
| 383 | asyncScope = new AsyncScope(), handler?: string) => { |
| 384 | return await asyncScope.exec(() => waitForFunctionWithTries(async () => { |
| 385 | const element = await $(selector, root, handler); |
| 386 | return (element || undefined); |
| 387 | }, options, asyncScope)); |
| 388 | }; |
| 389 | |
Tim van der Lippe | 58c5bcf | 2020-02-04 15:26:08 | [diff] [blame] | 390 | export const debuggerStatement = (frontend: puppeteer.Page) => { |
| 391 | return frontend.evaluate(() => { |
Jack Franklin | e245d1a | 2020-02-13 15:25:13 | [diff] [blame] | 392 | // eslint-disable-next-line no-debugger |
Tim van der Lippe | 58c5bcf | 2020-02-04 15:26:08 | [diff] [blame] | 393 | debugger; |
| 394 | }); |
| 395 | }; |
| 396 | |
Paul Lewis | 417c5c0 | 2020-03-27 10:49:06 | [diff] [blame] | 397 | export const logToStdOut = (msg: string) => { |
| 398 | if (!process.send) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | process.send({ |
| 403 | pid: process.pid, |
| 404 | details: msg, |
| 405 | }); |
| 406 | }; |
| 407 | |
Paul Lewis | 7108574 | 2020-04-01 11:18:31 | [diff] [blame] | 408 | export const logFailure = () => { |
| 409 | if (!process.send) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | process.send({ |
| 414 | pid: process.pid, |
| 415 | details: 'failure', |
| 416 | }); |
| 417 | }; |
| 418 | |
Benedikt Meurer | 8a7915a | 2022-10-20 05:43:29 | [diff] [blame] | 419 | async function setExperimentEnabled(experiment: string, enabled: boolean, options?: DevToolsFrontendReloadOptions) { |
Songtao Xia | 67f65da | 2020-06-11 17:58:39 | [diff] [blame] | 420 | const {frontend} = getBrowserAndPages(); |
Simon Zünd | 5583b99 | 2023-08-30 10:58:10 | [diff] [blame] | 421 | await frontend.evaluate(`(async () => { |
| 422 | const Root = await import('./core/root/root.js'); |
| 423 | Root.Runtime.experiments.setEnabled('${experiment}', ${enabled}); |
| 424 | })()`); |
Songtao Xia | 67f65da | 2020-06-11 17:58:39 | [diff] [blame] | 425 | await reloadDevTools(options); |
Benedikt Meurer | 8a7915a | 2022-10-20 05:43:29 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | export const enableExperiment = (experiment: string, options?: DevToolsFrontendReloadOptions) => |
| 429 | setExperimentEnabled(experiment, true, options); |
| 430 | |
| 431 | export const disableExperiment = (experiment: string, options?: DevToolsFrontendReloadOptions) => |
| 432 | setExperimentEnabled(experiment, false, options); |
Songtao Xia | 67f65da | 2020-06-11 17:58:39 | [diff] [blame] | 433 | |
Simon Zünd | 0b58205 | 2022-02-16 08:09:40 | [diff] [blame] | 434 | export const setDevToolsSettings = async (settings: Record<string, string>) => { |
| 435 | const {frontend} = getBrowserAndPages(); |
| 436 | await frontend.evaluate(settings => { |
| 437 | for (const name in settings) { |
| 438 | globalThis.InspectorFrontendHost.setPreference(name, JSON.stringify(settings[name])); |
| 439 | } |
| 440 | }, settings); |
| 441 | await reloadDevTools(); |
| 442 | }; |
| 443 | |
Philip Pfaffe | fdf153a | 2024-04-05 06:53:27 | [diff] [blame] | 444 | export function goToHtml(html: string): Promise<void> { |
| 445 | return goTo(`data:text/html;charset=utf-8,${encodeURIComponent(html)}`); |
| 446 | } |
| 447 | |
Al Muthanna Athamina | 61c9938 | 2023-04-06 10:17:13 | [diff] [blame] | 448 | export const goTo = async (url: string, options: puppeteer.WaitForOptions = {}) => { |
Patrick Brosset | 0805f4a | 2020-06-11 09:34:32 | [diff] [blame] | 449 | const {target} = getBrowserAndPages(); |
Al Muthanna Athamina | 61c9938 | 2023-04-06 10:17:13 | [diff] [blame] | 450 | await target.goto(url, options); |
Patrick Brosset | 0805f4a | 2020-06-11 09:34:32 | [diff] [blame] | 451 | }; |
| 452 | |
Jack Franklin | 5017c09 | 2021-02-12 11:40:43 | [diff] [blame] | 453 | export const overridePermissions = async (permissions: puppeteer.Permission[]) => { |
Maksim Sadym | 4d2e095 | 2020-08-06 08:34:53 | [diff] [blame] | 454 | const {browser} = getBrowserAndPages(); |
Jack Franklin | 3b3a782 | 2020-12-03 10:12:46 | [diff] [blame] | 455 | await browser.defaultBrowserContext().overridePermissions(`https://localhost:${getTestServerPort()}`, permissions); |
Maksim Sadym | 4d2e095 | 2020-08-06 08:34:53 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | export const clearPermissionsOverride = async () => { |
| 459 | const {browser} = getBrowserAndPages(); |
| 460 | await browser.defaultBrowserContext().clearPermissionOverrides(); |
| 461 | }; |
| 462 | |
Al Muthanna Athamina | 61c9938 | 2023-04-06 10:17:13 | [diff] [blame] | 463 | export const goToResource = async (path: string, options: puppeteer.WaitForOptions = {}) => { |
| 464 | await goTo(`${getResourcesPath()}/${path}`, options); |
Peter Marshall | c16cedd | 2020-07-07 15:11:17 | [diff] [blame] | 465 | }; |
| 466 | |
Simon Zünd | 5fd7506 | 2021-02-12 12:49:45 | [diff] [blame] | 467 | export const goToResourceWithCustomHost = async (host: string, path: string) => { |
| 468 | assert.isTrue(host.endsWith('.test'), 'Only custom hosts with a .test domain are allowed.'); |
| 469 | await goTo(`${getResourcesPath(host)}/${path}`); |
| 470 | }; |
| 471 | |
| 472 | export const getResourcesPath = (host: string = 'localhost') => { |
Philip Pfaffe | 9d80aef | 2024-07-09 16:59:30 | [diff] [blame] | 473 | return `https://${host}:${getTestServerPort()}/test/e2e/resources`; |
Patrick Brosset | 0805f4a | 2020-06-11 09:34:32 | [diff] [blame] | 474 | }; |
| 475 | |
Eric Leese | 23e6a0b | 2023-08-14 12:26:32 | [diff] [blame] | 476 | export const step = async (description: string, step: Function) => { |
| 477 | try { |
Philip Pfaffe | bafb48f | 2023-09-15 09:51:42 | [diff] [blame] | 478 | return await step(); |
Al Muthanna Athamina | 889cecf | 2020-04-22 13:46:37 | [diff] [blame] | 479 | } catch (error) { |
| 480 | if (error instanceof AssertionError) { |
| 481 | throw new AssertionError( |
| 482 | `Unexpected Result in Step "${description}" |
| 483 | ${error.message}`, |
| 484 | error); |
| 485 | } else { |
| 486 | error.message += ` in Step "${description}"`; |
| 487 | throw error; |
| 488 | } |
| 489 | } |
| 490 | }; |
| 491 | |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 492 | export const waitForAnimationFrame = async () => { |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 493 | const {frontend} = getBrowserAndPages(); |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 494 | |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 495 | await frontend.waitForFunction(() => { |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 496 | return new Promise(resolve => { |
| 497 | requestAnimationFrame(resolve); |
| 498 | }); |
| 499 | }); |
| 500 | }; |
| 501 | |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 502 | export const activeElement = async () => { |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 503 | const {frontend} = getBrowserAndPages(); |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 504 | |
| 505 | await waitForAnimationFrame(); |
| 506 | |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 507 | return frontend.evaluateHandle(() => { |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 508 | let activeElement = document.activeElement; |
| 509 | |
| 510 | while (activeElement && activeElement.shadowRoot) { |
| 511 | activeElement = activeElement.shadowRoot.activeElement; |
| 512 | } |
| 513 | |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 514 | if (!activeElement) { |
| 515 | throw new Error('No active element found'); |
| 516 | } |
| 517 | |
Simon Zünd | 9e2c759 | 2023-01-17 07:59:41 | [diff] [blame] | 518 | return activeElement; |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 519 | }); |
| 520 | }; |
| 521 | |
| 522 | export const activeElementTextContent = async () => { |
| 523 | const element = await activeElement(); |
| 524 | return element.evaluate(node => node.textContent); |
| 525 | }; |
| 526 | |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 527 | export const activeElementAccessibleName = async () => { |
| 528 | const element = await activeElement(); |
Kateryna Prokopenko | 17cba89 | 2024-07-26 13:33:56 | [diff] [blame^] | 529 | return element.evaluate(node => node.getAttribute('aria-label') || node.getAttribute('title')); |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 530 | }; |
| 531 | |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 532 | export const tabForward = async (page?: puppeteer.Page) => { |
| 533 | let targetPage: puppeteer.Page; |
| 534 | if (page) { |
| 535 | targetPage = page; |
| 536 | } else { |
| 537 | const {frontend} = getBrowserAndPages(); |
| 538 | targetPage = frontend; |
| 539 | } |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 540 | |
Tim van der Lippe | 72f2d4b | 2021-07-06 16:02:22 | [diff] [blame] | 541 | await targetPage.keyboard.press('Tab'); |
| 542 | }; |
| 543 | |
| 544 | export const tabBackward = async (page?: puppeteer.Page) => { |
| 545 | let targetPage: puppeteer.Page; |
| 546 | if (page) { |
| 547 | targetPage = page; |
| 548 | } else { |
| 549 | const {frontend} = getBrowserAndPages(); |
| 550 | targetPage = frontend; |
| 551 | } |
| 552 | |
| 553 | await targetPage.keyboard.down('Shift'); |
| 554 | await targetPage.keyboard.press('Tab'); |
| 555 | await targetPage.keyboard.up('Shift'); |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 556 | }; |
| 557 | |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 558 | type Awaitable<T> = T|PromiseLike<T>; |
| 559 | |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 560 | export const selectTextFromNodeToNode = async ( |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 561 | from: Awaitable<puppeteer.ElementHandle>, to: Awaitable<puppeteer.ElementHandle>, direction: 'up'|'down') => { |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 562 | const {target} = getBrowserAndPages(); |
| 563 | |
| 564 | // The clipboard api does not allow you to copy, unless the tab is focused. |
| 565 | await target.bringToFront(); |
| 566 | |
| 567 | return target.evaluate(async (from, to, direction) => { |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 568 | const selection = (from.getRootNode() as Document).getSelection(); |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 569 | const range = document.createRange(); |
| 570 | if (direction === 'down') { |
| 571 | range.setStartBefore(from); |
| 572 | range.setEndAfter(to); |
| 573 | } else { |
| 574 | range.setStartBefore(to); |
| 575 | range.setEndAfter(from); |
| 576 | } |
| 577 | |
Randolf | cc89254 | 2023-01-27 23:44:07 | [diff] [blame] | 578 | if (selection) { |
| 579 | selection.removeAllRanges(); |
| 580 | selection.addRange(range); |
| 581 | } |
Tim van der Lippe | e9b8e99 | 2020-09-22 13:26:37 | [diff] [blame] | 582 | |
| 583 | document.execCommand('copy'); |
| 584 | |
| 585 | return navigator.clipboard.readText(); |
| 586 | }, await from, await to, direction); |
| 587 | }; |
| 588 | |
Kateryna Prokopenko | a2016cc | 2024-02-26 14:45:13 | [diff] [blame] | 589 | export const clickMoreTabsButton = async (root?: puppeteer.ElementHandle<Element>) => { |
| 590 | await click('aria/More tabs', {root}); |
| 591 | }; |
| 592 | |
Jose Leal Chapa | 50e7bb5 | 2020-06-19 17:03:27 | [diff] [blame] | 593 | export const closePanelTab = async (panelTabSelector: string) => { |
Jose Leal Chapa | dd4d881 | 2020-05-21 16:45:00 | [diff] [blame] | 594 | // Get close button from tab element |
Jose Leal Chapa | 50e7bb5 | 2020-06-19 17:03:27 | [diff] [blame] | 595 | const selector = `${panelTabSelector} > .tabbed-pane-close-button`; |
Jose Leal Chapa | dd4d881 | 2020-05-21 16:45:00 | [diff] [blame] | 596 | await click(selector); |
Jose Leal Chapa | 50e7bb5 | 2020-06-19 17:03:27 | [diff] [blame] | 597 | await waitForNone(selector); |
Jose Leal Chapa | dd4d881 | 2020-05-21 16:45:00 | [diff] [blame] | 598 | }; |
| 599 | |
| 600 | export const closeAllCloseableTabs = async () => { |
| 601 | // get all closeable tools by looking for the available x buttons on tabs |
| 602 | const selector = '.tabbed-pane-close-button'; |
| 603 | const allCloseButtons = await $$(selector); |
| 604 | |
| 605 | // Get all panel ids |
Johan Bay | e824571 | 2020-08-04 13:32:10 | [diff] [blame] | 606 | const panelTabIds = await Promise.all(allCloseButtons.map(button => { |
| 607 | return button.evaluate(button => button.parentElement ? button.parentElement.id : ''); |
| 608 | })); |
Jose Leal Chapa | dd4d881 | 2020-05-21 16:45:00 | [diff] [blame] | 609 | |
| 610 | // Close each tab |
Jose Leal Chapa | 50e7bb5 | 2020-06-19 17:03:27 | [diff] [blame] | 611 | for (const tabId of panelTabIds) { |
| 612 | const selector = `#${tabId}`; |
| 613 | await closePanelTab(selector); |
Jose Leal Chapa | dd4d881 | 2020-05-21 16:45:00 | [diff] [blame] | 614 | } |
| 615 | }; |
| 616 | |
Eric Leese | 6634255 | 2020-07-06 16:43:35 | [diff] [blame] | 617 | // Noisy! Do not leave this in your test but it may be helpful |
| 618 | // when debugging. |
| 619 | export const enableCDPLogging = async () => { |
| 620 | const {frontend} = getBrowserAndPages(); |
| 621 | await frontend.evaluate(() => { |
| 622 | globalThis.ProtocolClient.test.dumpProtocol = console.log; // eslint-disable-line no-console |
| 623 | }); |
| 624 | }; |
| 625 | |
Eric Leese | a6533f0 | 2022-07-19 15:33:04 | [diff] [blame] | 626 | export const enableCDPTracking = async () => { |
| 627 | const {frontend} = getBrowserAndPages(); |
| 628 | await frontend.evaluate(() => { |
| 629 | globalThis.__messageMapForTest = new Map(); |
| 630 | globalThis.ProtocolClient.test.onMessageSent = (message: {method: string, id: number}) => { |
| 631 | globalThis.__messageMapForTest.set(message.id, message.method); |
| 632 | }; |
| 633 | globalThis.ProtocolClient.test.onMessageReceived = (message: {id?: number}) => { |
| 634 | if (message.id) { |
| 635 | globalThis.__messageMapForTest.delete(message.id); |
| 636 | } |
| 637 | }; |
| 638 | }); |
| 639 | }; |
| 640 | |
| 641 | export const logOutstandingCDP = async () => { |
| 642 | const {frontend} = getBrowserAndPages(); |
| 643 | await frontend.evaluate(() => { |
| 644 | for (const entry of globalThis.__messageMapForTest) { |
| 645 | console.error(entry); |
| 646 | } |
| 647 | }); |
| 648 | }; |
| 649 | |
Jack Franklin | 60cc8ba | 2021-02-10 12:14:26 | [diff] [blame] | 650 | export const selectOption = async (select: puppeteer.ElementHandle<HTMLSelectElement>, value: string) => { |
| 651 | await select.evaluate(async (node: HTMLSelectElement, _value: string) => { |
Maksim Sadym | 4d2e095 | 2020-08-06 08:34:53 | [diff] [blame] | 652 | node.value = _value; |
| 653 | const event = document.createEvent('HTMLEvents'); |
| 654 | event.initEvent('change', false, true); |
| 655 | node.dispatchEvent(event); |
| 656 | }, value); |
| 657 | }; |
| 658 | |
Jesus David Garcia Gomez | 9103d0e | 2020-10-12 20:28:40 | [diff] [blame] | 659 | export const scrollElementIntoView = async (selector: string, root?: puppeteer.JSHandle) => { |
| 660 | const element = await $(selector, root); |
| 661 | |
| 662 | if (!element) { |
| 663 | throw new Error(`Unable to find element with selector "${selector}"`); |
| 664 | } |
| 665 | |
| 666 | await element.evaluate(el => { |
| 667 | el.scrollIntoView(); |
| 668 | }); |
| 669 | }; |
| 670 | |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 671 | export const installEventListener = function(frontend: puppeteer.Page, eventType: string) { |
| 672 | return frontend.evaluate(eventType => { |
Jack Franklin | 2204d75 | 2022-11-21 14:10:10 | [diff] [blame] | 673 | window.__pendingEvents = window.__pendingEvents || new Map(); |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 674 | window.addEventListener(eventType, (e: Event) => { |
| 675 | let events = window.__pendingEvents.get(eventType); |
| 676 | if (!events) { |
| 677 | events = []; |
| 678 | window.__pendingEvents.set(eventType, events); |
| 679 | } |
| 680 | events.push(e); |
| 681 | }); |
| 682 | }, eventType); |
| 683 | }; |
| 684 | |
Kim-Anh Tran | 159f3de | 2022-05-18 10:54:28 | [diff] [blame] | 685 | export const getPendingEvents = function(frontend: puppeteer.Page, eventType: string): Promise<Event[]|undefined> { |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 686 | return frontend.evaluate(eventType => { |
| 687 | if (!('__pendingEvents' in window)) { |
Kim-Anh Tran | 159f3de | 2022-05-18 10:54:28 | [diff] [blame] | 688 | return undefined; |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 689 | } |
| 690 | const pendingEvents = window.__pendingEvents.get(eventType); |
| 691 | window.__pendingEvents.set(eventType, []); |
Kim-Anh Tran | 159f3de | 2022-05-18 10:54:28 | [diff] [blame] | 692 | return pendingEvents; |
Philip Pfaffe | 1e8f608 | 2020-10-26 11:55:57 | [diff] [blame] | 693 | }, eventType); |
| 694 | }; |
| 695 | |
Philip Pfaffe | 7fb6a83 | 2022-11-14 13:39:47 | [diff] [blame] | 696 | export function prepareWaitForEvent(element: puppeteer.ElementHandle, eventType: string): Promise<void> { |
| 697 | return element.evaluate((element: Element, eventType: string) => { |
Jack Franklin | 2204d75 | 2022-11-21 14:10:10 | [diff] [blame] | 698 | window.__eventHandlers = window.__eventHandlers || new WeakMap(); |
Philip Pfaffe | 7fb6a83 | 2022-11-14 13:39:47 | [diff] [blame] | 699 | |
| 700 | const eventHandlers = (() => { |
| 701 | const eventHandlers = window.__eventHandlers.get(element); |
| 702 | if (eventHandlers) { |
| 703 | return eventHandlers; |
| 704 | } |
| 705 | const newMap = new Map<string, Promise<void>>(); |
| 706 | window.__eventHandlers.set(element, newMap); |
| 707 | return newMap; |
| 708 | })(); |
| 709 | |
| 710 | if (eventHandlers.has(eventType)) { |
| 711 | throw new Error(`Event listener for ${eventType}' has already been installed.`); |
| 712 | } |
| 713 | eventHandlers.set(eventType, new Promise<void>(resolve => { |
| 714 | const handler = () => { |
| 715 | element.removeEventListener(eventType, handler); |
| 716 | resolve(); |
| 717 | }; |
| 718 | element.addEventListener(eventType, handler); |
| 719 | })); |
| 720 | }, eventType); |
| 721 | } |
| 722 | |
| 723 | export function waitForEvent(element: puppeteer.ElementHandle, eventType: string): Promise<void> { |
| 724 | return element.evaluate((element: Element, eventType: string) => { |
| 725 | if (!('__eventHandlers' in window)) { |
| 726 | throw new Error(`Event listener for '${eventType}' has not been installed.`); |
| 727 | } |
| 728 | const handler = window.__eventHandlers.get(element)?.get(eventType); |
| 729 | if (!handler) { |
| 730 | throw new Error(`Event listener for '${eventType}' has not been installed.`); |
| 731 | } |
| 732 | return handler; |
| 733 | }, eventType); |
| 734 | } |
| 735 | |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 736 | export const hasClass = async (element: puppeteer.ElementHandle<Element>, classname: string) => { |
Sigurd Schneider | bec4aa4 | 2021-04-21 08:28:57 | [diff] [blame] | 737 | return await element.evaluate((el, classname) => el.classList.contains(classname), classname); |
| 738 | }; |
| 739 | |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 740 | export const waitForClass = async (element: puppeteer.ElementHandle<Element>, classname: string) => { |
Sigurd Schneider | 286466e | 2021-01-28 10:09:42 | [diff] [blame] | 741 | await waitForFunction(async () => { |
Sigurd Schneider | bec4aa4 | 2021-04-21 08:28:57 | [diff] [blame] | 742 | return hasClass(element, classname); |
Sigurd Schneider | 286466e | 2021-01-28 10:09:42 | [diff] [blame] | 743 | }); |
| 744 | }; |
| 745 | |
Wolfgang Beyer | be0727c | 2021-07-30 07:16:04 | [diff] [blame] | 746 | /** |
| 747 | * This is useful to keep TypeScript happy in a test - if you have a value |
| 748 | * that's potentially `null` you can use this function to assert that it isn't, |
| 749 | * and satisfy TypeScript that the value is present. |
| 750 | */ |
| 751 | export function assertNotNullOrUndefined<T>(val: T): asserts val is NonNullable<T> { |
| 752 | if (val === null || val === undefined) { |
| 753 | throw new Error(`Expected given value to not be null/undefined but it was: ${val}`); |
| 754 | } |
Alex Rudenko | d659413 | 2021-02-04 11:58:06 | [diff] [blame] | 755 | } |
| 756 | |
Randolf | ad06e71 | 2023-02-03 06:44:47 | [diff] [blame] | 757 | export {getBrowserAndPages, getDevToolsFrontendHostname, getTestServerPort, reloadDevTools}; |
Sigurd Schneider | 2ca0006 | 2021-04-20 10:49:05 | [diff] [blame] | 758 | |
Sigurd Schneider | 76cb0e9 | 2021-06-24 06:13:10 | [diff] [blame] | 759 | export function matchString(actual: string, expected: string|RegExp): true|string { |
| 760 | if (typeof expected === 'string') { |
| 761 | if (actual !== expected) { |
| 762 | return `Expected item "${actual}" to equal "${expected}"`; |
| 763 | } |
| 764 | } else if (!expected.test(actual)) { |
| 765 | return `Expected item "${actual}" to match "${expected}"`; |
| 766 | } |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | export function matchArray<A, E>( |
| 771 | actual: A[], expected: E[], comparator: (actual: A, expected: E) => true | string): true|string { |
Sigurd Schneider | 2ca0006 | 2021-04-20 10:49:05 | [diff] [blame] | 772 | if (actual.length !== expected.length) { |
Sigurd Schneider | c86b89f | 2021-05-05 08:35:31 | [diff] [blame] | 773 | return `Expected [${actual.map(x => `"${x}"`).join(', ')}] to have length ${expected.length}`; |
Sigurd Schneider | 2ca0006 | 2021-04-20 10:49:05 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | for (let i = 0; i < expected.length; ++i) { |
Sigurd Schneider | 76cb0e9 | 2021-06-24 06:13:10 | [diff] [blame] | 777 | const result = comparator(actual[i], expected[i]); |
| 778 | if (result !== true) { |
| 779 | return `Mismatch in row ${i}: ${result}`; |
Sigurd Schneider | 2ca0006 | 2021-04-20 10:49:05 | [diff] [blame] | 780 | } |
| 781 | } |
Sigurd Schneider | c86b89f | 2021-05-05 08:35:31 | [diff] [blame] | 782 | return true; |
| 783 | } |
| 784 | |
Sigurd Schneider | 76cb0e9 | 2021-06-24 06:13:10 | [diff] [blame] | 785 | export function assertOk<Args extends unknown[]>(check: (...args: Args) => true | string) { |
| 786 | return (...args: Args) => { |
| 787 | const result = check(...args); |
| 788 | if (result !== true) { |
| 789 | throw new AssertionError(result); |
| 790 | } |
| 791 | }; |
Sigurd Schneider | 2ca0006 | 2021-04-20 10:49:05 | [diff] [blame] | 792 | } |
Paul Lewis | 46270478 | 2021-05-21 13:47:34 | [diff] [blame] | 793 | |
Sigurd Schneider | 76cb0e9 | 2021-06-24 06:13:10 | [diff] [blame] | 794 | export function matchTable<A, E>( |
| 795 | actual: A[][], expected: E[][], comparator: (actual: A, expected: E) => true | string) { |
| 796 | return matchArray(actual, expected, (actual, expected) => matchArray<A, E>(actual, expected, comparator)); |
| 797 | } |
| 798 | |
| 799 | export const matchStringArray = (actual: string[], expected: (string|RegExp)[]) => |
| 800 | matchArray(actual, expected, matchString); |
| 801 | |
| 802 | export const assertMatchArray = assertOk(matchStringArray); |
| 803 | |
| 804 | export const matchStringTable = (actual: string[][], expected: (string|RegExp)[][]) => |
| 805 | matchTable(actual, expected, matchString); |
| 806 | |
Paul Lewis | 46270478 | 2021-05-21 13:47:34 | [diff] [blame] | 807 | export async function renderCoordinatorQueueEmpty(): Promise<void> { |
Jack Franklin | a3f2d16 | 2021-10-01 11:30:21 | [diff] [blame] | 808 | const {frontend} = getBrowserAndPages(); |
Paul Lewis | 46270478 | 2021-05-21 13:47:34 | [diff] [blame] | 809 | await frontend.evaluate(() => { |
Jack Franklin | a3f2d16 | 2021-10-01 11:30:21 | [diff] [blame] | 810 | return new Promise<void>(resolve => { |
| 811 | const pendingFrames = globalThis.__getRenderCoordinatorPendingFrames(); |
| 812 | if (pendingFrames < 1) { |
| 813 | resolve(); |
| 814 | return; |
| 815 | } |
Paul Lewis | 46270478 | 2021-05-21 13:47:34 | [diff] [blame] | 816 | globalThis.addEventListener('renderqueueempty', resolve, {once: true}); |
| 817 | }); |
| 818 | }); |
| 819 | } |
Sigurd Schneider | 20e1748 | 2021-09-03 10:25:53 | [diff] [blame] | 820 | |
| 821 | export async function setCheckBox(selector: string, wantChecked: boolean): Promise<void> { |
| 822 | const checkbox = await waitFor(selector); |
| 823 | const checked = await checkbox.evaluate(box => (box as HTMLInputElement).checked); |
| 824 | if (checked !== wantChecked) { |
| 825 | await click(`${selector} + label`); |
| 826 | } |
| 827 | assert.strictEqual(await checkbox.evaluate(box => (box as HTMLInputElement).checked), wantChecked); |
| 828 | } |
PhistucK | 257ad69 | 2022-08-29 14:36:19 | [diff] [blame] | 829 | |
| 830 | export const summonSearchBox = async () => { |
| 831 | await pressKey('f', {control: true}); |
| 832 | }; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 833 | |
Danil Somsikov | acd635c | 2024-02-09 12:56:51 | [diff] [blame] | 834 | export const replacePuppeteerUrl = (value: string) => { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 835 | return value.replace(/pptr:.*:([0-9]+)$/, (_, match) => { |
| 836 | return `(index):${match}`; |
| 837 | }); |
| 838 | }; |
Alex Rudenko | facf014 | 2023-09-05 07:31:11 | [diff] [blame] | 839 | |
| 840 | export async function raf(page: puppeteer.Page): Promise<void> { |
| 841 | await page.evaluate(() => { |
| 842 | return new Promise(resolve => window.requestAnimationFrame(resolve)); |
| 843 | }); |
| 844 | } |