Comprehensive Playwright Testing Documentation
Overview
This document provides a complete guide to the Playwright end-to-end testing strategy implemented for the SocleStack Next.js user management application. The test suite covers authentication, user management, authorization, form validation, performance, accessibility, and security testing.
Table of Contents
- Test Architecture
- Getting Started
- Test Suites
- Page Object Model
- Test Utilities
- CI/CD Integration
- Best Practices
- Troubleshooting
Test Architecture
Directory Structure
tests/
├── e2e/ # End-to-end test suites
│ ├── auth/ # Authentication tests
│ │ ├── authentication.spec.ts
│ │ ├── login.spec.ts
│ │ └── registration.spec.ts
│ ├── user-management/ # User management tests
│ │ └── profile-management.spec.ts
│ ├── authorization/ # Role-based access tests
│ │ └── role-based-access.spec.ts
│ ├── validation/ # Form validation tests
│ │ └── form-validation.spec.ts
│ ├── performance/ # Performance tests
│ │ └── performance.spec.ts
│ ├── accessibility/ # Accessibility tests
│ │ └── accessibility.spec.ts
│ └── security/ # Security tests
│ └── security.spec.ts
├── pages/ # Page Object Models
│ ├── BasePage.ts
│ ├── LoginPage.ts
│ ├── RegistrationPage.ts
│ ├── ProfilePage.ts
│ └── ExtendedProfilePage.ts
├── utils/ # Test utilities
│ ├── auth-helpers.ts
│ ├── database-helpers.ts
│ └── test-data-factory.ts
├── config/ # Test configuration
│ ├── global-setup.ts
│ └── global-teardown.ts
└── fixtures/ # Test fixtures
├── auth/
└── data/Configuration
The test suite uses a sophisticated Playwright configuration that supports:
- Cross-browser testing (Chromium, Firefox, Safari, Edge)
- Mobile device testing (iPhone, Android)
- Parallel execution with sharding
- Automatic retries on CI
- Multiple reporters (HTML, JSON, JUnit)
- Screenshot and video capture on failure
- Trace collection for debugging
Getting Started
Prerequisites
- Node.js 18+ installed
- Database (PostgreSQL recommended)
- Application running on localhost:3000
Installation
# Install dependencies
npm ci
# Install Playwright browsers
npx playwright install --with-deps
# Setup test database
npm run db:migrate
npm run db:seed:testRunning Tests
Basic Usage
# Run all tests
npm run test:e2e
# Run specific test suite
npm run test:auth
npm run test:user-management
npm run test:security
# Run tests in headed mode (visible browser)
npm run test:e2e -- --headed
# Run tests in debug mode
npm run test:e2e -- --debug
# Run specific test file
npx playwright test tests/e2e/auth/authentication.spec.tsAdvanced Usage
# Run tests on specific browser
npx playwright test --project=firefox
# Run tests with grep pattern
npx playwright test --grep="login"
# Run tests with parallel workers
npx playwright test --workers=4
# Run tests with retries
npx playwright test --retries=2Using the Test Runner Script
The project includes a comprehensive test runner script with advanced options:
# Run all tests
node scripts/test-runner.js
# Run specific mode
node scripts/test-runner.js --mode=auth
node scripts/test-runner.js --mode=performance
node scripts/test-runner.js --mode=accessibility
# Run with specific browser
node scripts/test-runner.js --browser=firefox
# Run in headed mode with debug
node scripts/test-runner.js --headed --debug
# Run with custom configuration
node scripts/test-runner.js \
--mode=all \
--browser=chromium \
--parallel=2 \
--retries=1 \
--timeout=30000 \
--env=stagingTest Suites
1. Authentication Tests (tests/e2e/auth/)
Coverage:
- User login/logout flow
- Registration process with email verification
- Password reset functionality
- Session management
- Remember me functionality
- Social login (OAuth)
- Rate limiting protection
- CSRF protection
- Input sanitization
Key Test Cases:
- Valid and invalid credential combinations
- Password complexity requirements
- Email verification workflow
- Session persistence across browser restarts
- Multiple concurrent sessions
- Logout from all devices
- Account lockout after failed attempts
2. User Management Tests (tests/e2e/user-management/)
Coverage:
- Profile viewing and editing
- Password change functionality
- Email change with verification
- Profile picture upload/removal
- Account settings management
- Notification preferences
- Privacy settings
- Two-factor authentication
- Account deletion
Key Test Cases:
- Profile information updates
- Email change confirmation
- Password strength enforcement
- File upload validation
- Settings persistence
- Account deletion with confirmation
3. Authorization Tests (tests/e2e/authorization/)
Coverage:
- Role-based access control (Admin, Moderator, User)
- Protected route enforcement
- API endpoint protection
- Permission inheritance
- Role escalation prevention
- Session-based access control
Key Test Cases:
- Admin access to all features
- Moderator limited access
- User restricted access
- Unauthenticated redirects
- API access control
- Role change effects
4. Form Validation Tests (tests/e2e/validation/)
Coverage:
- Client-side validation
- Server-side validation
- Real-time validation feedback
- Cross-field validation
- Input sanitization
- File upload validation
- Error message display
Key Test Cases:
- Required field validation
- Email format validation
- Password confirmation matching
- Username availability checking
- XSS prevention
- File type and size limits
5. Performance Tests (tests/e2e/performance/)
Coverage:
- Page load performance
- Authentication response times
- Database query performance
- Network request optimization
- Memory usage monitoring
- Core Web Vitals measurement
Key Test Cases:
- Login page load under 2 seconds
- Authentication within 3 seconds
- Bulk data handling
- Concurrent user simulation
- Bundle size optimization
6. Accessibility Tests (tests/e2e/accessibility/)
Coverage:
- Keyboard navigation
- Screen reader support
- Color contrast compliance
- Focus management
- ARIA attributes
- Mobile accessibility
- WCAG 2.1 AA compliance
Key Test Cases:
- Full keyboard navigation
- Screen reader announcements
- Color contrast ratios
- Focus trap in modals
- Alternative input methods
- Mobile touch targets
7. Security Tests (tests/e2e/security/)
Coverage:
- SQL injection prevention
- XSS protection
- CSRF token validation
- Rate limiting enforcement
- Input sanitization
- Session security
- File upload security
Key Test Cases:
- Malicious input handling
- Authentication bypass attempts
- Session hijacking prevention
- File upload exploits
- API security headers
Page Object Model
The test suite uses the Page Object Model pattern for maintainable and reusable test code.
Base Page Class
// tests/pages/BasePage.ts
export abstract class BasePage {
readonly page: Page;
constructor(page: Page) {
this.page = page;
}
// Common navigation and utility methods
async navigateTo(path: string): Promise<void>
async waitForPageLoad(): Promise<void>
async assertSuccessMessage(message?: string): Promise<void>
async assertErrorMessage(message?: string): Promise<void>
// ... more common methods
}Specialized Page Classes
Each page extends the base class and implements page-specific functionality:
- LoginPage: Login form interactions and validations
- RegistrationPage: Registration form with complex validation
- ProfilePage: Profile management functionality
- ExtendedProfilePage: Advanced profile features
Example Usage
test('should login successfully', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('user@test.com', 'password123');
await loginPage.assertSuccessfulLogin();
});Test Utilities
AuthHelpers
Provides authentication-related utilities:
// Quick authentication methods
await AuthHelpers.loginAsAdmin(page);
await AuthHelpers.loginAsUser(page);
await AuthHelpers.logout(page);
// Test authentication states
const isAuth = await AuthHelpers.isAuthenticated(page);
await AuthHelpers.assertUserRole(page, Role.ADMIN);
// Test access control
const { allowed, denied } = await AuthHelpers.testAccessControl(page, routes);DatabaseHelpers
Manages test data and database operations:
// User management
const user = await DatabaseHelpers.createTestUser(userData);
const users = await DatabaseHelpers.setupTestUsers();
await DatabaseHelpers.cleanupDatabase();
// Bulk operations
await DatabaseHelpers.createBulkTestData(1000);
const stats = await DatabaseHelpers.getDatabaseStats();TestDataFactory
Generates consistent test data:
// Create test users
const user = TestDataFactory.createUser();
const admin = TestDataFactory.createAdminUser();
const bulkUsers = TestDataFactory.createBulkUsers(50);
// Generate form data
const formData = TestDataFactory.createFormData();
const securityData = TestDataFactory.createSecurityTestData();
const performanceData = TestDataFactory.createPerformanceTestData();CI/CD Integration
GitHub Actions Workflow
The project includes a comprehensive GitHub Actions workflow that:
- Runs tests in parallel across multiple browsers and shards
- Executes specialized test suites (performance, accessibility, security)
- Generates combined reports with detailed metrics
- Comments on pull requests with test results
- Deploys test reports to GitHub Pages
Workflow Structure
jobs:
test: # Main E2E tests (parallel across browsers/shards)
performance-tests: # Performance benchmarks
accessibility-tests: # WCAG compliance checks
security-tests: # Security vulnerability tests
generate-report: # Combined reporting
deploy-test-reports: # Report deploymentLocal CI Simulation
# Run the same tests as CI locally
npm run test:ci
# Run specific CI job equivalent
npm run test:performance
npm run test:accessibility
npm run test:securityBest Practices
Writing Tests
- Use descriptive test names that explain the scenario
- Follow the Arrange-Act-Assert pattern
- Use test.step() for complex scenarios
- Keep tests isolated and independent
- Use proper test data cleanup
Page Objects
- Create reusable locators with data-testid attributes
- Implement helper methods for common actions
- Use assertions within page objects for validation
- Keep page objects focused on single page functionality
Test Data
- Use factories for consistent test data generation
- Implement proper cleanup after each test
- Use unique identifiers to avoid conflicts
- Mock external dependencies when appropriate
Performance
- Run tests in parallel when possible
- Use test.beforeEach() for common setup
- Implement proper waiting strategies
- Optimize test execution time
Troubleshooting
Common Issues
Tests Failing in CI but Passing Locally
Possible Causes:
- Timing issues due to slower CI environment
- Different browser versions
- Missing environment variables
- Database state issues
Solutions:
// Increase timeouts for CI
test.setTimeout(process.env.CI ? 60000 : 30000);
// Use more robust waiting strategies
await page.waitForLoadState('networkidle');
await expect(element).toBeVisible({ timeout: 10000 });Flaky Tests
Common Patterns:
- Race conditions
- Unreliable element selectors
- Network timing issues
- Animation interference
Solutions:
// Use auto-waiting assertions
await expect(page.locator('[data-testid="element"]')).toBeVisible();
// Wait for specific conditions
await page.waitForFunction(() => window.dataLoaded === true);
// Disable animations
await page.addInitScript(() => {
document.addEventListener('DOMContentLoaded', () => {
const style = document.createElement('style');
style.textContent = '*, *::before, *::after { animation-duration: 0s !important; }';
document.head.appendChild(style);
});
});Database Issues
Setup Problems:
# Reset test database
npm run db:reset:test
# Check database connection
npm run db:status
# Re-run migrations
npm run db:migrate:freshDebugging
Visual Debugging
# Run tests in headed mode
npx playwright test --headed
# Run tests in debug mode (pauses execution)
npx playwright test --debug
# Generate trace files
npx playwright test --trace=onScreenshots and Videos
// Take screenshot during test
await page.screenshot({ path: 'debug-screenshot.png', fullPage: true });
// Record video (configured in playwright.config.ts)
// Videos are automatically saved on failureTrace Viewer
# Generate trace
npx playwright test --trace=on
# View trace
npx playwright show-trace test-results/trace.zipGetting Help
- Check test reports in
test-results/html-report/ - Review CI logs for detailed error messages
- Use trace viewer for step-by-step debugging
- Check database state during test execution
- Verify environment variables and configuration
Contributing
When adding new tests:
- Follow existing patterns and conventions
- Add appropriate test data to factories
- Update page objects as needed
- Include both positive and negative test cases
- Add documentation for complex test scenarios
- Ensure tests pass on all supported browsers
For questions or suggestions, please refer to the project's contribution guidelines.