refactor(e2e): reuse login helper and update test cases

This commit is contained in:
sumit_chaturvedi
2025-06-27 10:26:20 +05:30
parent 1880b97687
commit 95cb36e09b
2 changed files with 36 additions and 23 deletions
+5 -5
View File
@@ -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
},
});
+31 -18
View File
@@ -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 });
});