import { test as base, expect, Page } from '@playwright/test'; // Test user credentials export const testUser = { email: 'test@example.com', password: 'TestPassword123!', firstName: 'Test', lastName: 'User', }; export const adminUser = { email: 'admin@example.com', password: 'AdminPassword123!', firstName: 'Admin', lastName: 'User', }; // Auth fixture type type AuthFixture = { authenticatedPage: Page; adminPage: Page; }; /** * Extended test with authentication fixtures */ export const test = base.extend({ authenticatedPage: async ({ page }, use) => { // Login before test await loginAs(page, testUser.email, testUser.password); await use(page); }, adminPage: async ({ page }, use) => { // Login as admin before test await loginAs(page, adminUser.email, adminUser.password); await use(page); }, }); /** * Login helper function */ export async function loginAs( page: Page, email: string, password: string ): Promise { await page.goto('/auth/login'); await page.fill('input[name="email"]', email); await page.fill('input[name="password"]', password); await page.click('button[type="submit"]'); // Wait for redirect to dashboard await page.waitForURL(/\/(dashboard|onboarding)/, { timeout: 10000 }); } /** * Logout helper function */ export async function logout(page: Page): Promise { // Click user menu await page.click('[data-testid="user-menu"]'); await page.click('[data-testid="logout-button"]'); // Wait for redirect to login await page.waitForURL('/auth/login'); } /** * Register a new user */ export async function registerUser( page: Page, userData: { email: string; password: string; firstName: string; lastName: string; companyName?: string; } ): Promise { await page.goto('/auth/register'); await page.fill('input[name="firstName"]', userData.firstName); await page.fill('input[name="lastName"]', userData.lastName); await page.fill('input[name="email"]', userData.email); await page.fill('input[name="password"]', userData.password); if (userData.companyName) { await page.fill('input[name="companyName"]', userData.companyName); } await page.click('button[type="submit"]'); // Wait for redirect (either to onboarding or verification page) await page.waitForURL(/\/(onboarding|verify-email)/, { timeout: 10000 }); } /** * Check if user is authenticated */ export async function isAuthenticated(page: Page): Promise { try { // Check for auth token in localStorage or auth indicator in UI const token = await page.evaluate(() => { return localStorage.getItem('auth_token') || sessionStorage.getItem('auth_token'); }); return !!token; } catch { return false; } } export { expect };