Skip to content

Commit 1584846

Browse files
committed
Playright docs
Signed-off-by: Mihai Criveti <[email protected]>
1 parent 0499d8c commit 1584846

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed

tests/playwright/README.md

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
# 🎭 Playwright UI Tests for MCP Context Forge
2+
3+
This directory contains end-to-end UI tests for the MCP Context Forge admin interface and web UI components using [Playwright](https://playwright.dev/).
4+
5+
## 📁 Directory Structure
6+
7+
```
8+
tests/playwright/
9+
├── README.md # This file
10+
├── __init__.py # Package marker
11+
├── conftest.py # Pytest fixtures and configuration
12+
├── test_admin_ui.py # Admin panel UI tests
13+
├── test_api_endpoints.py # API integration tests via UI
14+
├── test_server_management.py # Server CRUD operations
15+
├── pages/ # Page Object Model
16+
│ ├── __init__.py
17+
│ ├── base_page.py # Base page class
18+
│ └── admin_page.py # Admin panel page objects
19+
├── screenshots/ # Test failure screenshots (auto-created)
20+
├── reports/ # Test reports (auto-created)
21+
└── videos/ # Test recordings (auto-created)
22+
```
23+
24+
## 🚀 Quick Start
25+
26+
### Installation
27+
28+
```bash
29+
# Install the project with Playwright dependencies
30+
pip install -e ".[playwright]"
31+
32+
# Install Playwright browsers (only needed once)
33+
make playwright-install # Installs Chromium only
34+
# OR
35+
make playwright-install-all # Installs all browsers (Chromium, Firefox, WebKit)
36+
```
37+
38+
### Running Tests
39+
40+
```bash
41+
# Start the MCP Gateway server first
42+
make serve
43+
44+
# In another terminal, run the tests:
45+
make test-ui # Run with visible browser
46+
make test-ui-headless # Run in headless mode (CI/CD)
47+
make test-ui-debug # Run with Playwright Inspector
48+
make test-ui-smoke # Run only smoke tests (fast)
49+
make test-ui-report # Generate HTML report
50+
```
51+
52+
## 🧪 Test Categories
53+
54+
Tests are organized by functionality and tagged with pytest markers:
55+
56+
- **`@pytest.mark.smoke`** - Quick validation tests that run in < 30 seconds
57+
- **`@pytest.mark.ui`** - UI interaction tests
58+
- **`@pytest.mark.api`** - API endpoint tests through the UI
59+
- **`@pytest.mark.e2e`** - Full end-to-end workflows
60+
- **`@pytest.mark.slow`** - Tests that take > 1 minute
61+
62+
### Running Specific Test Categories
63+
64+
```bash
65+
# Run only smoke tests
66+
pytest tests/playwright -m smoke
67+
68+
# Run all except slow tests
69+
pytest tests/playwright -m "not slow"
70+
71+
# Run UI and API tests
72+
pytest tests/playwright -m "ui or api"
73+
```
74+
75+
## 📝 Writing Tests
76+
77+
### Basic Test Structure
78+
79+
```python
80+
import pytest
81+
from playwright.sync_api import Page, expect
82+
83+
class TestFeatureName:
84+
"""Test suite for specific feature."""
85+
86+
@pytest.mark.smoke
87+
def test_basic_functionality(self, page: Page, base_url: str):
88+
"""Test description."""
89+
# Navigate
90+
page.goto(f"{base_url}/admin")
91+
92+
# Assert page loaded
93+
expect(page).to_have_title("MCP Gateway Admin")
94+
95+
# Interact with elements
96+
page.click('button:has-text("Add Server")')
97+
98+
# Verify results
99+
modal = page.locator('[role="dialog"]')
100+
expect(modal).to_be_visible()
101+
```
102+
103+
### Using Page Objects
104+
105+
```python
106+
from tests.playwright.pages.admin_page import AdminPage
107+
108+
def test_with_page_object(page: Page, base_url: str):
109+
"""Test using page object pattern."""
110+
admin = AdminPage(page, base_url)
111+
admin.navigate()
112+
admin.add_server("Test Server", "http://localhost:9000")
113+
assert admin.server_exists("Test Server")
114+
```
115+
116+
## 🔧 Configuration
117+
118+
### Environment Variables
119+
120+
```bash
121+
# Base URL for testing (default: http://localhost:4444)
122+
export TEST_BASE_URL=http://localhost:8000
123+
124+
# Authentication token
125+
export MCP_AUTH_TOKEN=your-test-token
126+
127+
# Playwright options
128+
export PWDEBUG=1 # Enable Playwright Inspector
129+
export HEADED=1 # Force headed mode
130+
export SLOWMO=100 # Add 100ms delay between actions
131+
```
132+
133+
### pyproject.toml Configuration
134+
135+
The project's `pyproject.toml` includes Playwright configuration in `[tool.pytest.ini_options]`:
136+
137+
- Default browser: `chromium`
138+
- Screenshots: `only-on-failure`
139+
- Videos: `retain-on-failure`
140+
- Traces: `retain-on-failure`
141+
142+
## 🐛 Debugging Tests
143+
144+
### 1. Playwright Inspector
145+
146+
```bash
147+
make test-ui-debug
148+
# OR
149+
PWDEBUG=1 pytest tests/playwright/test_admin_ui.py -s
150+
```
151+
152+
This opens the Playwright Inspector with:
153+
- Step through each action
154+
- See selector playground
155+
- Record new tests
156+
157+
### 2. Headed Mode
158+
159+
```bash
160+
make test-ui
161+
# OR
162+
pytest tests/playwright --headed
163+
```
164+
165+
### 3. Slow Motion
166+
167+
```bash
168+
pytest tests/playwright --headed --slowmo 1000 # 1 second delay
169+
```
170+
171+
### 4. Screenshots and Videos
172+
173+
Failed tests automatically capture:
174+
- Screenshots in `tests/playwright/screenshots/`
175+
- Videos in `test-results/` (when enabled)
176+
- Traces for debugging (when enabled)
177+
178+
### 5. VS Code Debugging
179+
180+
Add to `.vscode/launch.json`:
181+
182+
```json
183+
{
184+
"name": "Debug Playwright Test",
185+
"type": "python",
186+
"request": "launch",
187+
"module": "pytest",
188+
"args": [
189+
"tests/playwright/test_admin_ui.py::TestAdminUI::test_admin_panel_loads",
190+
"-v",
191+
"--headed"
192+
],
193+
"env": {
194+
"PWDEBUG": "console"
195+
}
196+
}
197+
```
198+
199+
## 📊 Test Reports
200+
201+
### HTML Report
202+
203+
```bash
204+
make test-ui-report
205+
open tests/playwright/reports/report.html
206+
```
207+
208+
### Coverage Report
209+
210+
```bash
211+
make test-ui-coverage
212+
open tests/playwright/reports/coverage/index.html
213+
```
214+
215+
### CI/CD Integration
216+
217+
Tests run automatically on GitHub Actions for:
218+
- Pull requests
219+
- Pushes to main/develop branches
220+
- Changes to UI code or test files
221+
222+
## ⚡ Performance Tips
223+
224+
1. **Use `test-ui-parallel` for faster execution**:
225+
```bash
226+
make test-ui-parallel # Runs tests in parallel
227+
```
228+
229+
2. **Run only affected tests**:
230+
```bash
231+
pytest tests/playwright -k "server" # Only server-related tests
232+
```
233+
234+
3. **Skip slow tests during development**:
235+
```bash
236+
pytest tests/playwright -m "not slow"
237+
```
238+
239+
4. **Reuse browser context** for related tests (see conftest.py)
240+
241+
## 🏗️ Best Practices
242+
243+
1. **Use Page Object Model** - Encapsulate page interactions in page classes
244+
2. **Explicit Waits** - Use `page.wait_for_selector()` instead of `time.sleep()`
245+
3. **Meaningful Assertions** - Use Playwright's `expect()` API for auto-waiting
246+
4. **Test Isolation** - Each test should be independent
247+
5. **Descriptive Names** - Test names should explain what they verify
248+
6. **Error Messages** - Include context in assertion messages
249+
7. **Cleanup** - Tests should clean up created resources
250+
251+
## 🔍 Common Issues
252+
253+
### Server Not Running
254+
255+
```bash
256+
# Error: Connection refused to localhost:4444
257+
# Solution: Start the server first
258+
make serve
259+
```
260+
261+
### Browser Not Installed
262+
263+
```bash
264+
# Error: Executable doesn't exist at...
265+
# Solution: Install browsers
266+
make playwright-install
267+
```
268+
269+
### Flaky Tests
270+
271+
```python
272+
# Add retries for flaky tests
273+
@pytest.mark.flaky(reruns=3, reruns_delay=1)
274+
def test_sometimes_flaky(page):
275+
# test code
276+
```
277+
278+
### Timeout Issues
279+
280+
```python
281+
# Increase timeout for slow operations
282+
page.set_default_timeout(30000) # 30 seconds
283+
# OR
284+
page.click("button", timeout=10000) # 10 seconds for this action
285+
```
286+
287+
## 📚 Resources
288+
289+
- [Playwright Python Documentation](https://playwright.dev/python/)
290+
- [Playwright Best Practices](https://playwright.dev/docs/best-practices)
291+
- [pytest-playwright Plugin](https://github.com/microsoft/playwright-pytest)
292+
- [MCP Gateway Documentation](https://ibm.github.io/mcp-context-forge/)
293+
294+
## 🤝 Contributing
295+
296+
1. Follow the existing test patterns
297+
2. Add appropriate test markers
298+
3. Update page objects for new UI elements
299+
4. Include docstrings explaining test purpose
300+
5. Run `make test-ui` locally before submitting PR
301+
6. Ensure all smoke tests pass

0 commit comments

Comments
 (0)