| 32 | ElementType extends null ? puppeteer.NodeFor<Selector>: ElementType; |
| 33 | |
| 34 | export class PageWrapper { |
| 35 | page: puppeteer.Page; |
| 36 | evaluate: puppeteer.Page['evaluate']; |
| 37 | waitForNavigation: puppeteer.Page['waitForNavigation']; |
| 38 | bringToFront: puppeteer.Page['bringToFront']; |
| 39 | evaluateOnNewDocument: puppeteer.Page['evaluateOnNewDocument']; |
| 40 | removeScriptToEvaluateOnNewDocument: puppeteer.Page['removeScriptToEvaluateOnNewDocument']; |
| 41 | locator: puppeteer.Page['locator']; |
| 42 | |
| 43 | constructor(page: puppeteer.Page) { |
| 44 | this.page = page; |
| 45 | this.evaluate = page.evaluate.bind(page); |
| 46 | this.bringToFront = page.bringToFront.bind(page); |
| 47 | this.evaluateOnNewDocument = page.evaluateOnNewDocument.bind(page); |
| 48 | this.removeScriptToEvaluateOnNewDocument = page.removeScriptToEvaluateOnNewDocument.bind(page); |
| 49 | this.waitForNavigation = page.waitForNavigation.bind(page); |
| 50 | this.locator = page.locator.bind(page); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * `waitForFunction` runs in the test context and not in the page |
| 55 | * context. If you want to evaluate code on the page, use |
| 56 | * {@link PageWrapper.evaluate} within the `waitForFunction` callback. |
| 57 | */ |
| 58 | async waitForFunction<T>( |
| 59 | fn: (logger: Logger) => T | undefined | Promise<T|undefined>, asyncScope = new AsyncScope(), |
| 60 | description?: string) { |
| 61 | const signal = AsyncScope.abortSignal; |
| 62 | const innerFunction = async (messages: string[]) => { |
| 63 | let iterations = 0; |
| 64 | const logger = {log: messages.push.bind(messages)}; |
| 65 | while (true) { |
| 66 | signal?.throwIfAborted(); |
| 67 | const messagesBefore = messages.length; |
| 68 | const result = await fn(logger); |
| 69 | // On completing an iteration, remove any leftover messages from the previous iteration. |
| 70 | messages.splice(0, messagesBefore); |
| 71 | messages.push(`Iteration ${iterations++} result: ${result ? 'success' : `${result}`}`); |
| 72 | signal?.throwIfAborted(); |
| 73 | if (result) { |
| 74 | return result as Exclude<NonNullable<T>, false>; |
| 75 | } |
| 76 | await this.timeout(100); |
| 77 | } |
| 78 | }; |
| 79 | return await asyncScope.exec(innerFunction, description); |
| 80 | } |
| 81 | |
| 82 | timeout(duration: number) { |
| 83 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 84 | } |
| 85 | |
| 86 | async screenshot(): Promise<string> { |
| 87 | await this.bringToFront(); |
| 88 | return await this.page.screenshot({ |
| 89 | encoding: 'base64', |
| 90 | }); |
| 91 | } |
nothing calls this directly
no test coverage detected