import { test, expect } from '@playwright/test';
// ============================================
// E2E Tests
// ============================================
test.describe('Authentication Flow', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/login');
});
test('should login with valid credentials', async ({ page }) => {
// Fill form
await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('password123');
// Submit
await page.getByRole('button', { name: 'Sign In' }).click();
// Verify redirect and content
await expect(page).toHaveURL('/dashboard');
await expect(page.getByText('Welcome Back')).toBeVisible();
});
test('should show error on invalid credentials', async ({ page }) => {
await page.getByLabel('Email').fill('wrong@example.com');
await page.getByLabel('Password').fill('wrongpass');
await page.getByRole('button', { name: 'Sign In' }).click();
await expect(page.getByText('Invalid email or password')).toBeVisible();
await expect(page).toHaveURL('/login');
});
});
test.describe('Navigation', () => {
test('should navigate between pages', async ({ page }) => {
await page.goto('/');
await page.getByRole('link', { name: 'About' }).click();
await expect(page).toHaveURL('/about');
await expect(page.locator('h1')).toContainText('About Us');
await page.getByRole('link', { name: 'Home' }).click();
await expect(page).toHaveURL('/');
});
});
// ============================================
// API Testing
// ============================================
test.describe('API Endpoint', () => {
test('should create a new post', async ({ request }) => {
const response = await request.post('/api/posts', {
data: {
title: 'Playwright Test',
content: 'Testing API endpoints',
},
});
expect(response.ok()).toBeTruthy();
const data = await response.json();
expect(data).toHaveProperty('id');
expect(data.title).toBe('Playwright Test');
});
});
// ============================================
// Visual Regression Testing
// ============================================
test('homepage should match screenshot', async ({ page }) => {
await page.goto('/');
await expect(page).toHaveScreenshot('landing-page.png');
});
// ============================================
// Mobile Viewport Request
// ============================================
test.use({ viewport: { width: 375, height: 667 } }); // iPhone SE
test('should display mobile menu', async ({ page }) => {
await page.goto('/');
await expect(page.locator('.mobile-menu-btn')).toBeVisible();
});