diff --git a/test/lib/browsers/base.ts b/test/lib/browsers/base.ts index 80975aee58ab0..bc1d9193719fc 100644 --- a/test/lib/browsers/base.ts +++ b/test/lib/browsers/base.ts @@ -1,3 +1,5 @@ +export type Event = 'request' + // This is the base Browser interface all browser // classes should build off of, it is the bare // methods we aim to support across tests @@ -68,6 +70,8 @@ export class BrowserInterface { deleteCookies(): BrowserInterface { return this } + on(event: Event, cb: (...args: any[]) => void) {} + off(event: Event, cb: (...args: any[]) => void) {} async loadPage(url: string, { disableCache: boolean }): Promise {} async get(url: string): Promise {} diff --git a/test/lib/browsers/playwright.ts b/test/lib/browsers/playwright.ts index bcd5ac59730d4..3ca06ce25018a 100644 --- a/test/lib/browsers/playwright.ts +++ b/test/lib/browsers/playwright.ts @@ -1,4 +1,4 @@ -import { BrowserInterface } from './base' +import { BrowserInterface, Event } from './base' import fs from 'fs-extra' import { chromium, @@ -28,6 +28,23 @@ export async function quit() { class Playwright extends BrowserInterface { private activeTrace?: string + private eventCallbacks: Record void>> = { + request: new Set(), + } + + on(event: Event, cb: (...args: any[]) => void) { + if (!this.eventCallbacks[event]) { + throw new Error( + `Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys( + event + )}` + ) + } + this.eventCallbacks[event]?.add(cb) + } + off(event: Event, cb: (...args: any[]) => void) { + this.eventCallbacks[event]?.delete(cb) + } async setup(browserName: string) { if (browser) return @@ -84,6 +101,9 @@ class Playwright extends BrowserInterface { page.on('pageerror', (error) => { console.error('page error', error) }) + page.on('request', (req) => { + this.eventCallbacks.request.forEach((cb) => cb(req)) + }) if (opts?.disableCache) { // TODO: this doesn't seem to work (dev tools does not check the box as expected)