From e0d57086a8becb6c2377648bd3934c65364f2ff1 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Mon, 10 Nov 2025 18:21:51 -0800 Subject: [PATCH] Include fetch override in auth fixtures --- .../src/fixtures/auth.fixture.ts | 3 ++ .../src/fixtures/page-extension.ts | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/libs/playwright-helpers/src/fixtures/auth.fixture.ts b/libs/playwright-helpers/src/fixtures/auth.fixture.ts index bc127c64a8f..9831cf2afc5 100644 --- a/libs/playwright-helpers/src/fixtures/auth.fixture.ts +++ b/libs/playwright-helpers/src/fixtures/auth.fixture.ts @@ -9,6 +9,8 @@ type BrowserName = "chromium" | "firefox" | "webkit"; import { Play, SingleUserScene, SingleUserSceneTemplate } from "@bitwarden/playwright-helpers"; +import { addInitScriptForPlayId } from "./page-extension"; + const hostname = new URL(webServerBaseUrl).hostname; const dataDir = process.env.PLAYWRIGHT_DATA_DIR ?? "playwright-data"; // Ensure data directory exists @@ -79,6 +81,7 @@ export class AuthFixture { } const context = await this._browser.newContext(); this._page = await context.newPage(); + await addInitScriptForPlayId(this._page, process.env.PLAY_ID!); } return this._page; } diff --git a/libs/playwright-helpers/src/fixtures/page-extension.ts b/libs/playwright-helpers/src/fixtures/page-extension.ts index adc8f510d29..d74681bac39 100644 --- a/libs/playwright-helpers/src/fixtures/page-extension.ts +++ b/libs/playwright-helpers/src/fixtures/page-extension.ts @@ -2,28 +2,29 @@ import { Page, TestFixture } from "@playwright/test"; export function pageExtension(): TestFixture { return async ({ page, playId }, use) => { - await page.addInitScript( - ({ p }) => { - const originalFetch = window.fetch; - window.fetch = async function ( - input: string | URL | globalThis.Request, - init?: RequestInit, - ) { - // Build a Request that takes into account both the input and any provided init overrides - const baseRequest = - input instanceof globalThis.Request - ? init - ? new Request(input, init) - : input - : new Request(input, init); - - baseRequest.headers.set("x-play-id", p); - - return originalFetch(baseRequest); - }; - }, - { p: playId }, - ); + await addInitScriptForPlayId(page, playId); await use(page); }; } + +export function addInitScriptForPlayId(page: Page, playId: string): Promise { + return page.addInitScript( + ({ p }) => { + const originalFetch = window.fetch; + window.fetch = async function (input: string | URL | globalThis.Request, init?: RequestInit) { + // Build a Request that takes into account both the input and any provided init overrides + const baseRequest = + input instanceof globalThis.Request + ? init + ? new Request(input, init) + : input + : new Request(input, init); + + baseRequest.headers.set("x-play-id", p); + + return originalFetch(baseRequest); + }; + }, + { p: playId }, + ); +}