# Playwright E2E Testing Guide for Bakery-IA This directory contains end-to-end (E2E) tests for the Bakery-IA SaaS application using [Playwright](https://playwright.dev). ## ๐ Project Structure ``` tests/ โโโ auth/ # Authentication tests (login, register, logout) โโโ onboarding/ # Onboarding wizard tests โโโ dashboard/ # Dashboard and main page tests โโโ operations/ # Business operations tests โโโ analytics/ # Analytics page tests (to be added) โโโ settings/ # Settings page tests (to be added) โโโ fixtures/ # Test data files (CSV, images, etc.) โโโ helpers/ # Utility functions and helpers โ โโโ auth.ts # Authentication helpers โ โโโ utils.ts # General utilities โโโ .auth/ # Stored authentication states (gitignored) โโโ .gitignore # Files to ignore in git โโโ auth.setup.ts # Global authentication setup ``` ## ๐ Getting Started ### Prerequisites - Node.js 20+ - npm installed - Playwright browsers installed ### Installation Playwright is already installed in this project. If you need to reinstall browsers: ```bash npx playwright install ``` ## ๐ฏ Running Tests ### Run all tests (headless) ```bash npm run test:e2e ``` ### Run tests with UI (interactive mode) ```bash npm run test:e2e:ui ``` ### Run tests in headed mode (see browser) ```bash npm run test:e2e:headed ``` ### Run tests in debug mode (step through tests) ```bash npm run test:e2e:debug ``` ### Run specific test file ```bash npx playwright test tests/auth/login.spec.ts ``` ### Run tests matching a pattern ```bash npx playwright test --grep "login" ``` ### View test report ```bash npm run test:e2e:report ``` ## ๐ฌ Recording Tests (Codegen) Playwright has a built-in test generator that records your actions: ```bash npm run test:e2e:codegen ``` This opens a browser where you can interact with your app. Playwright will generate test code for your actions. ## ๐ Authentication Tests use a global authentication setup to avoid logging in before every test. ### How it works: 1. `auth.setup.ts` runs once before all tests 2. Logs in with test credentials 3. Saves authentication state to `tests/.auth/user.json` 4. Other tests reuse this state ### Test Credentials Set these environment variables (or use defaults): ```bash export TEST_USER_EMAIL="test@bakery.com" export TEST_USER_PASSWORD="test-password-123" ``` ### Creating authenticated tests ```typescript import { test, expect } from '@playwright/test'; test.describe('My Test Suite', () => { // Use saved auth state test.use({ storageState: 'tests/.auth/user.json' }); test('my test', async ({ page }) => { // Already logged in! await page.goto('/app/dashboard'); }); }); ``` ## ๐ Writing Tests ### Basic Test Structure ```typescript import { test, expect } from '@playwright/test'; test.describe('Feature Name', () => { test.beforeEach(async ({ page }) => { // Setup before each test await page.goto('/your-page'); }); test('should do something', async ({ page }) => { // Your test code await page.getByRole('button', { name: 'Click me' }).click(); await expect(page).toHaveURL('/expected-url'); }); }); ``` ### Using Helpers ```typescript import { login, logout, TEST_USER } from '../helpers/auth'; import { waitForLoadingToFinish, expectToastMessage } from '../helpers/utils'; test('my test with helpers', async ({ page }) => { await login(page, TEST_USER); await waitForLoadingToFinish(page); await expectToastMessage(page, 'Success!'); }); ``` ### Best Practices 1. **Use semantic selectors** ```typescript // โ Good page.getByRole('button', { name: 'Submit' }) page.getByLabel('Email') // โ Avoid page.locator('.btn-primary') page.locator('#email-input') ``` 2. **Wait for elements properly** ```typescript // โ Good - Auto-waits await page.getByText('Hello').click(); // โ Avoid - Manual waits await page.waitForTimeout(3000); ``` 3. **Use data-testid for complex elements** ```typescript // In your component