|
| 1 | +# End-to-End Tests |
| 2 | + |
| 3 | +This directory contains end-to-end (e2e) tests for the COH3 Stats application using Playwright. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +### Install Playwright |
| 8 | + |
| 9 | +First, install Playwright and its dependencies: |
| 10 | + |
| 11 | +```bash |
| 12 | +yarn add -E -D @playwright/test@1.49.1 |
| 13 | +``` |
| 14 | + |
| 15 | +Then install the Playwright browsers: |
| 16 | + |
| 17 | +```bash |
| 18 | +npx playwright install |
| 19 | +``` |
| 20 | + |
| 21 | +Or install with dependencies (recommended for CI): |
| 22 | + |
| 23 | +```bash |
| 24 | +npx playwright install --with-deps |
| 25 | +``` |
| 26 | + |
| 27 | +## Running Tests |
| 28 | + |
| 29 | +### Run all tests |
| 30 | + |
| 31 | +```bash |
| 32 | +yarn test:e2e |
| 33 | +``` |
| 34 | + |
| 35 | +### Run tests in UI mode (interactive) |
| 36 | + |
| 37 | +```bash |
| 38 | +yarn test:e2e:ui |
| 39 | +``` |
| 40 | + |
| 41 | +### Run tests in headed mode (see browser) |
| 42 | + |
| 43 | +```bash |
| 44 | +yarn test:e2e:headed |
| 45 | +``` |
| 46 | + |
| 47 | +### Run specific test file |
| 48 | + |
| 49 | +```bash |
| 50 | +npx playwright test e2e/smoke/home.spec.ts |
| 51 | +``` |
| 52 | + |
| 53 | +### Run tests for specific browser |
| 54 | + |
| 55 | +```bash |
| 56 | +npx playwright test --project=chromium |
| 57 | +npx playwright test --project=firefox |
| 58 | +npx playwright test --project=webkit |
| 59 | +``` |
| 60 | + |
| 61 | +## Test Structure |
| 62 | + |
| 63 | +``` |
| 64 | +e2e/ |
| 65 | +├── helpers/ |
| 66 | +│ └── test-utils.ts # Common test utilities and helper functions |
| 67 | +├── smoke/ |
| 68 | +│ ├── home.spec.ts # Home page tests |
| 69 | +│ ├── leaderboards.spec.ts # Leaderboards tests |
| 70 | +│ ├── search.spec.ts # Search page tests |
| 71 | +│ ├── about.spec.ts # About page tests |
| 72 | +│ ├── news.spec.ts # News page tests |
| 73 | +│ ├── stats.spec.ts # Stats pages tests |
| 74 | +│ ├── explorer.spec.ts # Explorer pages tests |
| 75 | +│ ├── other-pages.spec.ts # Other static pages tests |
| 76 | +│ └── dynamic-routes.spec.ts # Dynamic route tests |
| 77 | +└── README.md # This file |
| 78 | +``` |
| 79 | + |
| 80 | +## Writing Tests |
| 81 | + |
| 82 | +### Basic Test Structure |
| 83 | + |
| 84 | +```typescript |
| 85 | +import { test, expect } from "@playwright/test"; |
| 86 | +import { navigateAndWait, checkPageLoaded } from "../helpers/test-utils"; |
| 87 | + |
| 88 | +test.describe("Feature Name", () => { |
| 89 | + test("should do something", async ({ page }) => { |
| 90 | + await navigateAndWait(page, "/path"); |
| 91 | + await checkPageLoaded(page); |
| 92 | + |
| 93 | + // Your test assertions |
| 94 | + await expect(page.locator("selector")).toBeVisible(); |
| 95 | + }); |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +### Helper Functions |
| 100 | + |
| 101 | +- `navigateAndWait(page, path)` - Navigate to a path and wait for page load |
| 102 | +- `checkPageLoaded(page)` - Verify page loaded without errors |
| 103 | +- `checkFooterPresent(page)` - Verify footer is present |
| 104 | + |
| 105 | +## Configuration |
| 106 | + |
| 107 | +The Playwright configuration is in `playwright.config.ts` at the root of the project. |
| 108 | + |
| 109 | +Key settings: |
| 110 | + |
| 111 | +- **Base URL**: `http://localhost:3000` (configurable via `PLAYWRIGHT_BASE_URL` env var) |
| 112 | +- **Test Directory**: `./e2e` |
| 113 | +- **Browsers**: Chromium, Firefox, WebKit, Mobile Chrome, Mobile Safari |
| 114 | +- **Retries**: 2 retries on CI, 0 locally |
| 115 | +- **Workers**: 1 worker on CI, parallel locally |
| 116 | +- **Web Server**: Automatically starts `yarn start` before tests |
| 117 | + |
| 118 | +## CI/CD Integration |
| 119 | + |
| 120 | +Tests are automatically run in GitHub Actions on: |
| 121 | + |
| 122 | +- Pull requests to master |
| 123 | +- Pushes to master |
| 124 | + |
| 125 | +See `.github/workflows/tests.yaml` for the CI configuration. |
| 126 | + |
| 127 | +## Debugging |
| 128 | + |
| 129 | +### View test report |
| 130 | + |
| 131 | +After running tests, view the HTML report: |
| 132 | + |
| 133 | +```bash |
| 134 | +npx playwright show-report |
| 135 | +``` |
| 136 | + |
| 137 | +### Debug specific test |
| 138 | + |
| 139 | +```bash |
| 140 | +npx playwright test --debug e2e/smoke/home.spec.ts |
| 141 | +``` |
| 142 | + |
| 143 | +### View traces |
| 144 | + |
| 145 | +Traces are automatically captured on first retry. View them with: |
| 146 | + |
| 147 | +```bash |
| 148 | +npx playwright show-trace trace.zip |
| 149 | +``` |
| 150 | + |
| 151 | +## Best Practices |
| 152 | + |
| 153 | +1. **Use data-testid sparingly** - Prefer semantic selectors (role, text, label) |
| 154 | +2. **Wait for elements** - Use Playwright's auto-waiting features |
| 155 | +3. **Keep tests independent** - Each test should be able to run in isolation |
| 156 | +4. **Use descriptive test names** - Make it clear what the test is checking |
| 157 | +5. **Group related tests** - Use `test.describe()` to organize tests |
| 158 | +6. **Handle dynamic content** - Use appropriate timeouts for API-dependent content |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +### Tests timing out |
| 163 | + |
| 164 | +- Increase timeout in test: `test.setTimeout(60000)` |
| 165 | +- Check if the dev server is running |
| 166 | +- Verify network connectivity |
| 167 | + |
| 168 | +### Flaky tests |
| 169 | + |
| 170 | +- Add explicit waits for dynamic content |
| 171 | +- Use `waitForLoadState('networkidle')` |
| 172 | +- Check for race conditions |
| 173 | + |
| 174 | +### Browser not installed |
| 175 | + |
| 176 | +Run: `npx playwright install` |
0 commit comments