From fc3d09e9eb4b14afbb0a4ec9a2db37823bd24ba4 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Tue, 27 Jan 2026 08:04:54 -0800 Subject: [PATCH] Allow specifying a page throughout the auth fixture --- .../src/fixtures/auth.fixture.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/libs/playwright-helpers/src/fixtures/auth.fixture.ts b/libs/playwright-helpers/src/fixtures/auth.fixture.ts index c3e22b3a3ec..e10cdd7dfd0 100644 --- a/libs/playwright-helpers/src/fixtures/auth.fixture.ts +++ b/libs/playwright-helpers/src/fixtures/auth.fixture.ts @@ -44,7 +44,10 @@ type AuthenticatedContext = { scene: SingleUserScene; }; -type SessionOptions = Simplify, "email">>; +type SessionOptions = Simplify, "email">> & { + /** The page to use for authenticating */ + page?: Page; +}; /** * A map of already authenticated emails to their scenes. @@ -53,7 +56,6 @@ const AuthenticatedEmails = new Map(); export class AuthFixture { private _browser!: Browser; - private _page!: Page; constructor(private readonly browserName: BrowserName) {} @@ -92,7 +94,7 @@ export class AuthFixture { /** * Creates a testing {@link Scene} with a user and a {@link Page} authenticated as that user. * If the user has already been authenticated in this worker, it will reuse the existing session, - * but the pages are independent. + * but the pages are independent unless a page is provided in the options. * * @param email email of the user * @param password password of the user @@ -104,16 +106,24 @@ export class AuthFixture { options: SessionOptions = {}, ): Promise { if (AuthenticatedEmails.has(email)) { - return await this.resumeSession(email, password); + return await this.resumeSession(email, password, options); } // start a new session return await this.newSession(email, password, options); } - async resumeSession(email: string, password: string): Promise { - const page = await this.newPage(); - if (AuthenticatedEmails.get(email)!.password !== password) { + /** Attempts to reload a page with session details for the given account. This will fail + * if the account has not already been authenticated in this worker. + */ + async resumeSession( + email: string, + password: string, + options: SessionOptions = {}, + ): Promise { + const page = options?.page || (await this.newPage()); + const previousAuth = AuthenticatedEmails.get(email); + if (previousAuth != null && previousAuth.password !== password) { throw new Error( `Email ${email} is already authenticated with a different password (${ AuthenticatedEmails.get(email)!.password @@ -140,6 +150,7 @@ export class AuthFixture { }; } + /** Create the given account with the seeder API, and authenticates */ async newSession( email: string, password: string, @@ -150,11 +161,14 @@ export class AuthFixture { return await this.authenticateForScene(scene, password); } + /** Authenticates the given account without first creating it with the + * Seeder or attempting to resume a previous session */ async authenticateForScene( scene: SingleUserScene, password: string, + page?: Page, ): Promise { - const page = await this.newPage(); + page = page || (await this.newPage()); const email = scene.upArgs.email; const mangledEmail = scene.mangle(email); await page.goto("/#/login");