From 44756f47c899b1e4984d50b48dd817bef77dd138 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Fri, 10 Oct 2025 10:21:05 -0700 Subject: [PATCH] fixup authenticate-as --- .../src/acts/authenticate-as.ts | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/libs/playwright-helpers/src/acts/authenticate-as.ts b/libs/playwright-helpers/src/acts/authenticate-as.ts index df1599290fa..a946da934d1 100644 --- a/libs/playwright-helpers/src/acts/authenticate-as.ts +++ b/libs/playwright-helpers/src/acts/authenticate-as.ts @@ -1,9 +1,9 @@ import * as fs from "fs"; -import { Page } from "@playwright/test"; +import { Page, expect } from "@playwright/test"; import { webServerBaseUrl } from "@playwright-config"; -import { Play, Scene } from "@bitwarden/playwright-helpers"; +import { Play, Scene, SingleUserRecipe } from "@bitwarden/playwright-helpers"; const hostname = new URL(webServerBaseUrl).hostname; const dataDir = process.env.PLAYWRIGHT_DATA_DIR ?? "playwright-data"; @@ -26,7 +26,7 @@ type AuthenticatedContext = { /** * A map of already authenticated emails to their scenes. */ -const AuthenticatedEmails = new Map(); +const AuthenticatedEmails = new Map(); function dataFilePath(email: string): string { return `${dataDir}/auth-${email}.json`; @@ -59,11 +59,15 @@ export async function authenticateAs( }; } - return newAuthenticateAs(email, password); + return newAuthenticateAs(page, email, password); } -function newAuthenticateAs(email: string, password: string): Promise { - using scene = await Play.scene(new SingleUserRecipe({ email, password }), { +async function newAuthenticateAs( + page: Page, + email: string, + password: string, +): Promise { + using scene = await Play.scene(new SingleUserRecipe({ email }), { downAfterAll: true, }); await page.goto("/#/login"); @@ -94,18 +98,8 @@ function newAuthenticateAs(email: string, password: string): Promise { // Get session storage and store as env variable - const sessionStorage = await page.evaluate(() => JSON.stringify(sessionStorage)); - fs.writeFileSync("playwright/.auth/session.json", sessionStorage, "utf-8"); - - // Set session storage in a new context - const sessionStorage = JSON.parse(fs.readFileSync("playwright/.auth/session.json", "utf-8")); - await context.addInitScript((storage) => { - if (window.location.hostname === "example.com") { - for (const [key, value] of Object.entries(storage)) { - window.sessionStorage.setItem(key, value); - } - } - }, sessionStorage); + const json = await page.evaluate(() => JSON.stringify(sessionStorage)); + fs.writeFileSync("playwright/.auth/session.json", json, "utf-8"); } async function loadSession(page: Page, email: string): Promise { @@ -114,10 +108,10 @@ async function loadSession(page: Page, email: string): Promise { } // Set session storage in a new context const sessionStorage = JSON.parse(fs.readFileSync(sessionFilePath(email), "utf-8")); - await context.addInitScript((storage) => { + await page.addInitScript((storage) => { if (window.location.hostname === hostname) { for (const [key, value] of Object.entries(storage)) { - window.sessionStorage.setItem(key, value); + window.sessionStorage.setItem(key, value as any); } } }, sessionStorage);