|
| 1 | +import { test as base, expect, type Page } from '@playwright/test'; |
| 2 | +import { LOGIN_SELECTORS, waitForTokenProcessing } from './selectors'; |
| 3 | + |
| 4 | +// Extend the base test with custom fixtures |
| 5 | +export const test = base.extend<{ |
| 6 | + authenticatedPage: Page; |
| 7 | +}>({ |
| 8 | + authenticatedPage: async ({ browser }, use) => { |
| 9 | + // Create a new context with stored auth state |
| 10 | + const context = await browser.newContext({ |
| 11 | + storageState: '.auth/user.json', |
| 12 | + }); |
| 13 | + const page = await context.newPage(); |
| 14 | + await use(page); |
| 15 | + await context.close(); |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +export { expect }; |
| 20 | + |
| 21 | +/** |
| 22 | + * Helper function to perform login via the OIDC flow. |
| 23 | + * Used by auth setup and login tests. |
| 24 | + * |
| 25 | + * Note: The assertions in this function are sanity check assertions - they verify |
| 26 | + * that the login flow elements are present and functional. If these fail, it |
| 27 | + * indicates an infrastructure or IdP issue rather than a test-specific failure. |
| 28 | + */ |
| 29 | +export async function performLogin(page: Page): Promise<void> { |
| 30 | + const baseUrl = process.env.BASE_URL || 'http://localhost:8080'; |
| 31 | + const testEmail = process.env.TEST_USER_EMAIL || 'user@stickerlandia.com'; |
| 32 | + const testPassword = process.env.TEST_USER_PASSWORD || 'Stickerlandia2025!'; |
| 33 | + |
| 34 | + console.log(`[performLogin] Starting login flow to ${baseUrl}`); |
| 35 | + |
| 36 | + // Navigate to home page |
| 37 | + await page.goto(baseUrl); |
| 38 | + await page.waitForLoadState('networkidle'); |
| 39 | + console.log(`[performLogin] Home page loaded`); |
| 40 | + |
| 41 | + // Click the "Sign In" button in the header to initiate login |
| 42 | + const signInButton = page.getByRole('button', { name: /sign in/i }); |
| 43 | + await expect(signInButton).toBeVisible({ timeout: 10000 }); |
| 44 | + await expect(signInButton).toBeEnabled({ timeout: 5000 }); |
| 45 | + await signInButton.click(); |
| 46 | + console.log(`[performLogin] Clicked Sign In button`); |
| 47 | + |
| 48 | + // Wait for navigation away from home page |
| 49 | + await page.waitForURL((url) => url.href !== baseUrl && url.href !== `${baseUrl}/`, { |
| 50 | + timeout: 30000, |
| 51 | + }); |
| 52 | + console.log(`[performLogin] Navigated to: ${page.url()}`); |
| 53 | + |
| 54 | + // Wait for the page to fully load (the login form is server-rendered) |
| 55 | + await page.waitForLoadState('networkidle'); |
| 56 | + await page.waitForLoadState('domcontentloaded'); |
| 57 | + console.log(`[performLogin] Page loaded, looking for email input`); |
| 58 | + |
| 59 | + // Define the email input locator |
| 60 | + const emailInput = page.locator(LOGIN_SELECTORS.emailInput).first(); |
| 61 | + |
| 62 | + // Wait for login form with longer timeout - the server-rendered page can be slow |
| 63 | + try { |
| 64 | + await expect(emailInput).toBeVisible({ timeout: 20000 }); |
| 65 | + console.log(`[performLogin] Email input visible`); |
| 66 | + } catch { |
| 67 | + // Check if we were redirected back with a token (already authenticated) |
| 68 | + const currentUrl = page.url(); |
| 69 | + console.log(`[performLogin] Email input not found. Current URL: ${currentUrl}`); |
| 70 | + if (currentUrl.includes('access_token=') || currentUrl.includes('/dashboard')) { |
| 71 | + console.log(`[performLogin] Already authenticated, skipping login`); |
| 72 | + // Already authenticated, skip login |
| 73 | + if (currentUrl.includes('access_token=')) { |
| 74 | + await page.waitForFunction( |
| 75 | + () => !window.location.search.includes('access_token'), |
| 76 | + { timeout: 15000 } |
| 77 | + ); |
| 78 | + } |
| 79 | + if (!page.url().includes('/dashboard')) { |
| 80 | + await page.goto(`${baseUrl}/dashboard`); |
| 81 | + } |
| 82 | + await page.waitForLoadState('networkidle'); |
| 83 | + await page.waitForSelector('main#main', { timeout: 10000 }); |
| 84 | + return; |
| 85 | + } |
| 86 | + // Re-throw with more context |
| 87 | + throw new Error(`Login form did not appear within timeout. Current URL: ${page.url()}`); |
| 88 | + } |
| 89 | + |
| 90 | + // Fill in credentials |
| 91 | + console.log(`[performLogin] Filling credentials for ${testEmail}`); |
| 92 | + await emailInput.fill(testEmail); |
| 93 | + |
| 94 | + const passwordInput = page.locator(LOGIN_SELECTORS.passwordInput).first(); |
| 95 | + await expect(passwordInput).toBeVisible({ timeout: 5000 }); |
| 96 | + await passwordInput.fill(testPassword); |
| 97 | + |
| 98 | + // Click submit |
| 99 | + const submitButton = page.locator(LOGIN_SELECTORS.submitButton).first(); |
| 100 | + await expect(submitButton).toBeVisible({ timeout: 5000 }); |
| 101 | + await submitButton.click(); |
| 102 | + console.log(`[performLogin] Clicked submit button`); |
| 103 | + |
| 104 | + // Wait for redirect back to the app - either with token or to dashboard |
| 105 | + await page.waitForURL( |
| 106 | + (url) => { |
| 107 | + const href = url.href; |
| 108 | + return ( |
| 109 | + href.includes('access_token=') || |
| 110 | + href.includes('/dashboard') || |
| 111 | + (href.includes('localhost:8080') && !href.includes('/api/users')) |
| 112 | + ); |
| 113 | + }, |
| 114 | + { timeout: 30000 } |
| 115 | + ); |
| 116 | + console.log(`[performLogin] Redirected to: ${page.url()}`); |
| 117 | + |
| 118 | + // Handle token URL processing |
| 119 | + if (page.url().includes('access_token=')) { |
| 120 | + console.log(`[performLogin] Waiting for token to be processed`); |
| 121 | + await waitForTokenProcessing(page); |
| 122 | + console.log(`[performLogin] Token processed, URL now: ${page.url()}`); |
| 123 | + } |
| 124 | + |
| 125 | + // Give the app time to process auth state |
| 126 | + await page.waitForTimeout(1000); |
| 127 | + |
| 128 | + // Navigate to dashboard if not already there |
| 129 | + if (!page.url().includes('/dashboard')) { |
| 130 | + console.log(`[performLogin] Navigating to dashboard`); |
| 131 | + await page.goto(`${baseUrl}/dashboard`); |
| 132 | + } |
| 133 | + |
| 134 | + await page.waitForLoadState('networkidle'); |
| 135 | + console.log(`[performLogin] On dashboard, waiting for content`); |
| 136 | + |
| 137 | + // Wait for actual dashboard content (not just the hidden main element) |
| 138 | + // The dashboard shows "Welcome Back" when user data is loaded |
| 139 | + await page.getByText(/Welcome Back/i).waitFor({ state: 'visible', timeout: 15000 }); |
| 140 | + console.log(`[performLogin] Login complete - dashboard content visible`); |
| 141 | +} |
| 142 | + |
| 143 | +// Helper to check if user is logged in |
| 144 | +export async function isLoggedIn(page: Page): Promise<boolean> { |
| 145 | + try { |
| 146 | + await page.goto('/dashboard'); |
| 147 | + await page.waitForURL(/\/dashboard/, { timeout: 5000 }); |
| 148 | + return true; |
| 149 | + } catch { |
| 150 | + return false; |
| 151 | + } |
| 152 | +} |
0 commit comments