1import { test, expect } from '@playwright/test';
2
3test.describe('Login Flow', () => {
4 test.beforeEach(async ({ page }) => {
5 await page.goto('http://localhost:3000/login');
6 });
7
8 test('successful login redirects to dashboard', async ({ page }) => {
9 await page.fill('[data-testid="email"]', 'alice@example.com');
10 await page.fill('[data-testid="password"]', 'password123');
11 await page.click('[data-testid="submit"]');
12
13 await expect(page).toHaveURL(/dashboard/);
14 await expect(page.locator('h1')).toContainText('Welcome, Alice');
15 });
16
17 test('shows error for invalid credentials', async ({ page }) => {
18 await page.fill('[data-testid="email"]', 'wrong@example.com');
19 await page.fill('[data-testid="password"]', 'wrongpass');
20 await page.click('[data-testid="submit"]');
21
22 const error = page.locator('[data-testid="error-message"]');
23 await expect(error).toBeVisible();
24 await expect(error).toContainText('Invalid credentials');
25 await expect(page).toHaveURL(/login/); // stayed on login page
26 });
27
28 test('keyboard navigation works', async ({ page }) => {
29 await page.keyboard.press('Tab');
30 await expect(page.locator('[data-testid="email"]')).toBeFocused();
31 await page.keyboard.type('alice@example.com');
32 await page.keyboard.press('Tab');
33 await page.keyboard.type('password123');
34 await page.keyboard.press('Enter');
35 await expect(page).toHaveURL(/dashboard/);
36 });
37});