import { test as baseTest, Page } from '@playwright/test'; /** * Shared Playwright fixture for authentication * Provides authenticated browsers for different user roles */ interface AuthFixture { authenticatedDriver: Page; unonboardedDriver: Page; authenticatedAdmin: Page; unauthenticatedPage: Page; } const DEMO_PASSWORD = 'Demo1234!'; export const testWithAuth = baseTest.extend({ authenticatedDriver: async ({ browser }, use) => { const context = await browser.newContext(); const page = await context.newPage(); // Navigate to login page await page.goto('/auth/login'); // Wait for the form to be ready await page.waitForSelector('[data-testid="email-input"]', { state: 'visible', timeout: 10000 }); // Fill and submit login form await page.getByTestId('email-input').fill('demo.driver@example.com'); await page.getByTestId('password-input').fill(DEMO_PASSWORD); await page.getByTestId('login-submit').click(); // Wait for redirect to dashboard or another authenticated page await page.waitForURL('**/dashboard**', { timeout: 15000 }); await use(page); await context.close(); }, unonboardedDriver: async ({ browser }, use) => { const context = await browser.newContext(); const page = await context.newPage(); // Navigate to login page await page.goto('/auth/login'); // Wait for the form to be ready await page.waitForSelector('[data-testid="email-input"]', { state: 'visible', timeout: 10000 }); // Fill and submit login form await page.getByTestId('email-input').fill('demo.driver@example.com'); await page.getByTestId('password-input').fill(DEMO_PASSWORD); await page.getByTestId('login-submit').click(); // Wait for redirect to onboarding or dashboard // Note: If the user is already onboarded in the current environment, they will land on /dashboard try { await Promise.race([ page.waitForURL('**/onboarding**', { timeout: 15000 }), page.waitForURL('**/dashboard**', { timeout: 15000 }) ]); } catch (e) { console.log('Navigation timeout: User did not redirect to onboarding or dashboard'); } // If we are on dashboard but need to be on onboarding for tests, // we navigate to /onboarding?force=true to bypass the redirect if (page.url().includes('/dashboard')) { await page.goto('/onboarding?force=true'); } await use(page); await context.close(); }, authenticatedAdmin: async ({ browser }, use) => { const context = await browser.newContext(); const page = await context.newPage(); // Navigate to login page await page.goto('/auth/login'); // Wait for the form to be ready await page.waitForSelector('[data-testid="email-input"]', { state: 'visible', timeout: 10000 }); // Fill and submit login form await page.getByTestId('email-input').fill('demo.admin@example.com'); await page.getByTestId('password-input').fill(DEMO_PASSWORD); await page.getByTestId('login-submit').click(); // Wait for redirect to dashboard or another authenticated page await page.waitForURL('**/dashboard**', { timeout: 15000 }); await use(page); await context.close(); }, unauthenticatedPage: async ({ browser }, use) => { const page = await browser.newPage(); await use(page); await page.close(); }, }); export { expect } from '@playwright/test';