From 95cb36e09b8d03d01df15a9e4be5e8a96929a1ed Mon Sep 17 00:00:00 2001 From: sumit_chaturvedi Date: Fri, 27 Jun 2025 10:26:20 +0530 Subject: [PATCH] refactor(e2e): reuse login helper and update test cases --- ui/playwright.config.ts | 10 ++--- ui/tests/e2e/auth/authentication.spec.ts | 49 +++++++++++++++--------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/ui/playwright.config.ts b/ui/playwright.config.ts index ac947a31da..70b78977e4 100644 --- a/ui/playwright.config.ts +++ b/ui/playwright.config.ts @@ -62,9 +62,9 @@ export default defineConfig({ webServer: isLocal ? undefined // Skip web server in local runs : { - command: "npm run dev", - url: "http://localhost:3000", - reuseExistingServer: true, - timeout: 120 * 1000, // wait up to 2 minutes for frontend to boot - }, + command: "npm run dev", + url: "http://localhost:3000", + reuseExistingServer: true, + timeout: 120 * 1000, // wait up to 2 minutes for frontend to boot + }, }); diff --git a/ui/tests/e2e/auth/authentication.spec.ts b/ui/tests/e2e/auth/authentication.spec.ts index 963365133b..ddbbe50133 100644 --- a/ui/tests/e2e/auth/authentication.spec.ts +++ b/ui/tests/e2e/auth/authentication.spec.ts @@ -1,13 +1,20 @@ -import { test, expect, request } from '@playwright/test'; +import { test, expect, request, Page } from '@playwright/test'; // Test credentials const testEmail = 'test@gmail.com'; const testPassword = 'Testt@123456'; +// Helper login function +const login = async (page: Page, email: string, password: string) => { + await page.goto('/sign-in'); + await page.fill('input[name="email"]', email); + await page.fill('input[name="password"]', password); + await page.getByRole('button', { name: /log in/i }).click(); +}; + test.beforeAll(async () => { const apiContext = await request.newContext(); - - await apiContext.post(`${process.env.API_BASE_URL}/users`, { + const response = await apiContext.post(`${process.env.API_BASE_URL}/users`, { headers: { 'Content-Type': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json', @@ -25,32 +32,38 @@ test.beforeAll(async () => { }, }); + if (!response.ok()) { + console.warn(`User creation may have failed: ${response.status()} - ${await response.text()}`); + } + await apiContext.dispose(); }); +// Test invalid login test('should show error for invalid credentials', async ({ page }) => { - await page.goto('/sign-in'); - - await page.fill('input[name="email"]', 'wrong@gmail.com'); - await page.fill('input[name="password"]', 'WrongPassword123'); - await page.getByRole('button', { name: /log in/i }).click(); + await login(page, 'wrong@gmail.com', 'WrongPassword123'); await page.waitForTimeout(7000); await expect(page.getByText(/invalid email or password/i)).toBeVisible({ timeout: 10000 }); - }); +// Test valid login and redirection test('should sign in successfully', async ({ page }) => { - // Go to login page - await page.goto('/sign-in'); - - // Fill login form - await page.fill('input[name="email"]', testEmail); - await page.fill('input[name="password"]', testPassword); - - // Submit the form - await page.getByRole('button', { name: /log in/i }).click(); + await login(page, testEmail, testPassword); await page.waitForTimeout(7000); await page.waitForURL((url) => !url.pathname.includes('sign-in'), { timeout: 15000, }); }); + +// Test session persistence after reload +test('should persist session after login', async ({ page }) => { + await login(page, testEmail, testPassword); + await page.waitForTimeout(7000); + await page.waitForURL((url) => !url.pathname.includes('sign-in'), { timeout: 15000 }); + await page.reload(); + + await expect(page.getByRole('button', { name: /sign out/i })).toBeVisible(); + + await page.goto("/findings") + await expect(page.getByText(/Browse all findings/i).first()).toBeVisible({ timeout: 10000 }); +});