Playwright End-to-End Testing Guide
Playwright End-to-End Testing Guide
Section titled “Playwright End-to-End Testing Guide”This guide covers the Playwright end-to-end testing setup for the Bartendie application.
Overview
Section titled “Overview”We’ve replaced Cypress with Playwright for better performance, reliability, and modern testing capabilities. Playwright provides:
- Multi-browser support: Chrome, Firefox, Safari, and Edge
- Better CI/CD integration: More reliable in headless environments
- Modern architecture: Better handling of modern web apps
- Powerful API mocking: Superior network interception capabilities
- TypeScript-first approach: Better developer experience
Project Structure
Section titled “Project Structure”frontend/├── playwright.config.js # Main Playwright configuration├── tests/ # Test files directory│ ├── global-setup.js # Global test setup│ ├── global-teardown.js # Global test cleanup│ ├── fixtures/ # Test data and fixtures│ │ └── test-data.js # Shared test data│ ├── helpers/ # Test helper functions│ │ ├── auth-helper.js # Authentication utilities│ │ └── graphql-helper.js # GraphQL testing utilities│ ├── auth.spec.js # Authentication flow tests│ ├── events.spec.js # Event management tests│ ├── venues.spec.js # Venue management tests│ ├── inventory.spec.js # Inventory management tests│ └── recipes.spec.js # Recipe management tests└── package.json # Updated with Playwright scriptsConfiguration
Section titled “Configuration”Playwright Config (playwright.config.js)
Section titled “Playwright Config (playwright.config.js)”Key configuration features:
- Base URL:
http://localhost:5173(Vite dev server) - Multi-browser testing: Chrome, Firefox, Safari, Mobile
- Automatic server startup: Both frontend and backend
- Test artifacts: Screenshots, videos, traces on failure
- Parallel execution: Optimized for CI/CD
Web Servers
Section titled “Web Servers”The configuration automatically starts:
- Frontend:
pnpm devonhttp://localhost:5173 - Backend:
mix phx.serveronhttp://localhost:4000
Available Scripts
Section titled “Available Scripts”# Run all testspnpm e2e
# Run tests with UI (interactive mode)pnpm e2e:open
# Run tests in headed mode (see browser)pnpm e2e:headed
# Debug tests step by steppnpm e2e:debug
# View test reportpnpm e2e:report
# Install/update browserspnpm e2e:installTest Structure
Section titled “Test Structure”Authentication Tests (auth.spec.js)
Section titled “Authentication Tests (auth.spec.js)”- Login with valid/invalid credentials
- Registration flow
- Password reset
- Logout functionality
- Field validation
Event Management Tests (events.spec.js)
Section titled “Event Management Tests (events.spec.js)”- Event list display
- Event creation and validation
- Event details and editing
- Event deletion
- Menu creation for events
Venue Management Tests (venues.spec.js)
Section titled “Venue Management Tests (venues.spec.js)”- Venue list and creation
- Bar management (add/edit/remove)
- Equipment management
- Validation and error handling
Inventory Tests (inventory.spec.js)
Section titled “Inventory Tests (inventory.spec.js)”- Product management
- Inventory level updates
- Low stock alerts
- Inventory history
Recipe Tests (recipes.spec.js)
Section titled “Recipe Tests (recipes.spec.js)”- Recipe creation and editing
- Ingredient management
- Recipe search and filtering
- Category filtering
Helper Functions
Section titled “Helper Functions”Authentication Helper (auth-helper.js)
Section titled “Authentication Helper (auth-helper.js)”import { login, logout, register, isLoggedIn } from './helpers/auth-helper.js';
// Login as different user typesawait login(page, 'admin'); // or 'user', 'manager'
// Check login statusconst loggedIn = await isLoggedIn(page);
// Register new userawait register(page, userData);GraphQL Helper (graphql-helper.js)
Section titled “GraphQL Helper (graphql-helper.js)”import { createTestEvent, waitForGraphQLRequest } from './helpers/graphql-helper.js';
// Create test dataconst event = await createTestEvent(eventData);
// Wait for GraphQL operationsawait waitForGraphQLRequest(page, 'createEvent');Test Data Management
Section titled “Test Data Management”Fixtures (fixtures/test-data.js)
Section titled “Fixtures (fixtures/test-data.js)”Centralized test data for:
- Test users (admin, user, manager)
- Sample events, venues, products
- Recipe templates
Database State
Section titled “Database State”- Global Setup: Ensures clean database state
- Test Isolation: Each test starts with clean state
- Cleanup: Automatic cleanup after test runs
Best Practices
Section titled “Best Practices”1. Use Data Test IDs
Section titled “1. Use Data Test IDs”<button data-testid="create-event-button">Create Event</button>await page.click('[data-testid="create-event-button"]');2. Wait for Network Requests
Section titled “2. Wait for Network Requests”const responsePromise = waitForGraphQLRequest(page, 'createEvent');await page.click('[data-testid="submit-button"]');await responsePromise;3. Use Helper Functions
Section titled “3. Use Helper Functions”// Instead of repeating login stepsawait login(page, 'admin');
// Instead of manual GraphQL callsconst event = await createTestEvent(eventData);4. Proper Assertions
Section titled “4. Proper Assertions”// Wait for elements to be visibleawait expect(page.locator('[data-testid="success-message"]')).toBeVisible();
// Check text contentawait expect(page.locator('[data-testid="event-name"]')).toContainText(eventName);
// Verify URL changesawait expect(page).toHaveURL(/\/events\/[a-zA-Z0-9-]+/);Running Tests
Section titled “Running Tests”Local Development
Section titled “Local Development”# Start both servers manually (optional - auto-started by Playwright)pnpm dev # Frontendcd .. && mix phx.server # Backend
# Run testspnpm e2e # All testspnpm e2e auth.spec.js # Specific test filepnpm e2e:open # Interactive modeCI/CD Integration
Section titled “CI/CD Integration”# Headless mode for CIpnpm e2e
# Generate reportspnpm e2e:reportDebugging
Section titled “Debugging”Interactive Mode
Section titled “Interactive Mode”pnpm e2e:openDebug Mode
Section titled “Debug Mode”pnpm e2e:debugTest Artifacts
Section titled “Test Artifacts”- Screenshots: Captured on failure
- Videos: Recorded for failed tests
- Traces: Full execution traces for debugging
Migration from Cypress
Section titled “Migration from Cypress”Key Changes
Section titled “Key Changes”- Import syntax:
import { test, expect } from '@playwright/test' - Selectors: Same data-testid approach
- Assertions:
expect(locator).toBeVisible()vscy.should('be.visible') - Network waiting:
waitForGraphQLRequest()helper - Page object: Explicit
pageparameter
Benefits Over Cypress
Section titled “Benefits Over Cypress”- Faster execution: Better performance
- More reliable: Less flaky tests
- Better CI/CD: Improved headless mode
- Multi-browser: Built-in cross-browser testing
- Modern API: Better async/await support
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”- Server startup timeout: Increase timeout in config
- Element not found: Add proper waits
- GraphQL timing: Use
waitForGraphQLRequest() - Authentication state: Use
login()helper
Debug Tips
Section titled “Debug Tips”- Use
page.pause()to debug interactively - Check test artifacts in
test-results/ - Use
--headedflag to see browser actions - Add
console.log()in helper functions
Next Steps
Section titled “Next Steps”This Playwright setup provides a solid foundation for comprehensive end-to-end testing. The test coverage includes all major user flows and can be extended as new features are added to the application.