Skip to content

Commit ecee5d2

Browse files
authored
#255 playwright scaffolding, wip (#458)
Signed-off-by: Mihai Criveti <[email protected]>
1 parent af86503 commit ecee5d2

File tree

12 files changed

+651
-2
lines changed

12 files changed

+651
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Playwright Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.11'
20+
21+
- name: Install dependencies
22+
run: |
23+
pip install -e ".[playwright]"
24+
playwright install chromium
25+
26+
- name: Start application
27+
run: |
28+
make serve &
29+
sleep 5 # Wait for server to start
30+
31+
- name: Run Playwright tests
32+
run: |
33+
make test-ui-headless
34+
env:
35+
TEST_BASE_URL: http://localhost:4444
36+
37+
- name: Upload screenshots on failure
38+
if: failure()
39+
uses: actions/upload-artifact@v3
40+
with:
41+
name: playwright-screenshots
42+
path: tests/playwright/screenshots/

Makefile

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ check-manifest: ## 📦 Verify MANIFEST.in completeness
601601
@echo "📦 Verifying MANIFEST.in completeness..."
602602
@$(VENV_DIR)/bin/check-manifest
603603

604-
unimport: ## 📦 Unused import detection
604+
unimport: ## 📦 Unused import detection
605605
@echo "📦 unimport …" && $(VENV_DIR)/bin/unimport --check --diff mcpgateway
606606

607607
vulture: ## 🧹 Dead code detection
@@ -2364,3 +2364,148 @@ db-history:
23642364

23652365
db-revision-id:
23662366
@$(ALEMBIC) current --verbose | awk '/Current revision/ {print $$3}'
2367+
2368+
2369+
# =============================================================================
2370+
# 🎭 UI TESTING (PLAYWRIGHT)
2371+
# =============================================================================
2372+
# help: 🎭 UI TESTING (PLAYWRIGHT)
2373+
# help: playwright-install - Install Playwright browsers (chromium by default)
2374+
# help: playwright-install-all - Install all Playwright browsers (chromium, firefox, webkit)
2375+
# help: test-ui - Run Playwright UI tests with visible browser
2376+
# help: test-ui-headless - Run Playwright UI tests in headless mode
2377+
# help: test-ui-debug - Run Playwright UI tests with Playwright Inspector
2378+
# help: test-ui-smoke - Run UI smoke tests only (fast subset)
2379+
# help: test-ui-parallel - Run UI tests in parallel using pytest-xdist
2380+
# help: test-ui-report - Run UI tests and generate HTML report
2381+
# help: test-ui-coverage - Run UI tests with coverage for admin endpoints
2382+
# help: test-ui-record - Run UI tests and record videos (headless)
2383+
# help: test-ui-update-snapshots - Update visual regression snapshots
2384+
# help: test-ui-clean - Clean up Playwright test artifacts
2385+
2386+
.PHONY: playwright-install playwright-install-all test-ui test-ui-headless test-ui-debug test-ui-smoke test-ui-parallel test-ui-report test-ui-coverage test-ui-record test-ui-update-snapshots test-ui-clean
2387+
2388+
# Playwright test variables
2389+
PLAYWRIGHT_DIR := tests/playwright
2390+
PLAYWRIGHT_REPORTS := $(PLAYWRIGHT_DIR)/reports
2391+
PLAYWRIGHT_SCREENSHOTS := $(PLAYWRIGHT_DIR)/screenshots
2392+
PLAYWRIGHT_VIDEOS := $(PLAYWRIGHT_DIR)/videos
2393+
2394+
## --- Playwright Setup -------------------------------------------------------
2395+
playwright-install:
2396+
@echo "🎭 Installing Playwright browsers (chromium)..."
2397+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2398+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2399+
pip install -e '.[playwright]' 2>/dev/null || pip install playwright pytest-playwright && \
2400+
playwright install chromium"
2401+
@echo "✅ Playwright chromium browser installed!"
2402+
2403+
playwright-install-all:
2404+
@echo "🎭 Installing all Playwright browsers..."
2405+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2406+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2407+
pip install -e '.[playwright]' 2>/dev/null || pip install playwright pytest-playwright && \
2408+
playwright install"
2409+
@echo "✅ All Playwright browsers installed!"
2410+
2411+
## --- UI Test Execution ------------------------------------------------------
2412+
test-ui: playwright-install
2413+
@echo "🎭 Running UI tests with visible browser..."
2414+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2415+
@mkdir -p $(PLAYWRIGHT_SCREENSHOTS) $(PLAYWRIGHT_REPORTS)
2416+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2417+
pytest $(PLAYWRIGHT_DIR)/ -v --headed --screenshot=only-on-failure \
2418+
--browser chromium || { echo '❌ UI tests failed!'; exit 1; }"
2419+
@echo "✅ UI tests completed!"
2420+
2421+
test-ui-headless: playwright-install
2422+
@echo "🎭 Running UI tests in headless mode..."
2423+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2424+
@mkdir -p $(PLAYWRIGHT_SCREENSHOTS) $(PLAYWRIGHT_REPORTS)
2425+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2426+
pytest $(PLAYWRIGHT_DIR)/ -v --screenshot=only-on-failure \
2427+
--browser chromium || { echo '❌ UI tests failed!'; exit 1; }"
2428+
@echo "✅ UI tests completed!"
2429+
2430+
test-ui-debug: playwright-install
2431+
@echo "🎭 Running UI tests with Playwright Inspector..."
2432+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2433+
@mkdir -p $(PLAYWRIGHT_SCREENSHOTS) $(PLAYWRIGHT_REPORTS)
2434+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2435+
PWDEBUG=1 pytest $(PLAYWRIGHT_DIR)/ -v -s --headed \
2436+
--browser chromium"
2437+
2438+
test-ui-smoke: playwright-install
2439+
@echo "🎭 Running UI smoke tests..."
2440+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2441+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2442+
pytest $(PLAYWRIGHT_DIR)/ -v -m smoke --headed \
2443+
--browser chromium || { echo '❌ UI smoke tests failed!'; exit 1; }"
2444+
@echo "✅ UI smoke tests passed!"
2445+
2446+
test-ui-parallel: playwright-install
2447+
@echo "🎭 Running UI tests in parallel..."
2448+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2449+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2450+
pip install -q pytest-xdist && \
2451+
pytest $(PLAYWRIGHT_DIR)/ -v -n auto --dist loadscope \
2452+
--browser chromium || { echo '❌ UI tests failed!'; exit 1; }"
2453+
@echo "✅ UI parallel tests completed!"
2454+
2455+
## --- UI Test Reporting ------------------------------------------------------
2456+
test-ui-report: playwright-install
2457+
@echo "🎭 Running UI tests with HTML report..."
2458+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2459+
@mkdir -p $(PLAYWRIGHT_REPORTS)
2460+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2461+
pip install -q pytest-html && \
2462+
pytest $(PLAYWRIGHT_DIR)/ -v --screenshot=only-on-failure \
2463+
--html=$(PLAYWRIGHT_REPORTS)/report.html --self-contained-html \
2464+
--browser chromium || true"
2465+
@echo "✅ UI test report generated: $(PLAYWRIGHT_REPORTS)/report.html"
2466+
@echo " Open with: open $(PLAYWRIGHT_REPORTS)/report.html"
2467+
2468+
test-ui-coverage: playwright-install
2469+
@echo "🎭 Running UI tests with coverage..."
2470+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2471+
@mkdir -p $(PLAYWRIGHT_REPORTS)
2472+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2473+
pytest $(PLAYWRIGHT_DIR)/ -v --cov=mcpgateway.admin \
2474+
--cov-report=html:$(PLAYWRIGHT_REPORTS)/coverage \
2475+
--cov-report=term --browser chromium || true"
2476+
@echo "✅ UI coverage report: $(PLAYWRIGHT_REPORTS)/coverage/index.html"
2477+
2478+
test-ui-record: playwright-install
2479+
@echo "🎭 Running UI tests with video recording..."
2480+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2481+
@mkdir -p $(PLAYWRIGHT_VIDEOS)
2482+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2483+
pytest $(PLAYWRIGHT_DIR)/ -v --video=on \
2484+
--browser chromium || true"
2485+
@echo "✅ Test videos saved in: $(PLAYWRIGHT_VIDEOS)/"
2486+
2487+
## --- UI Test Utilities ------------------------------------------------------
2488+
test-ui-update-snapshots: playwright-install
2489+
@echo "🎭 Updating visual regression snapshots..."
2490+
@test -d "$(VENV_DIR)" || $(MAKE) venv
2491+
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
2492+
pytest $(PLAYWRIGHT_DIR)/ -v --update-snapshots \
2493+
--browser chromium"
2494+
@echo "✅ Snapshots updated!"
2495+
2496+
test-ui-clean:
2497+
@echo "🧹 Cleaning Playwright test artifacts..."
2498+
@rm -rf $(PLAYWRIGHT_SCREENSHOTS)/*.png
2499+
@rm -rf $(PLAYWRIGHT_VIDEOS)/*.webm
2500+
@rm -rf $(PLAYWRIGHT_REPORTS)/*
2501+
@rm -rf test-results/
2502+
@rm -f playwright-report-*.html test-results-*.xml
2503+
@echo "✅ Playwright artifacts cleaned!"
2504+
2505+
## --- Combined Testing -------------------------------------------------------
2506+
test-all: test test-ui-headless
2507+
@echo "✅ All tests completed (unit + UI)!"
2508+
2509+
# Add UI tests to your existing test suite if needed
2510+
test-full: coverage test-ui-report
2511+
@echo "📊 Full test suite completed with coverage and UI tests!"

pyproject.toml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ dev = [
154154
"yamllint>=1.37.1",
155155
]
156156

157+
# UI Testing
158+
playwright = [
159+
"playwright>=1.53.0",
160+
"pytest-playwright>=0.7.0",
161+
"pytest-html>=4.1.1",
162+
"pytest-timeout>=2.4.0",
163+
]
164+
157165
# Convenience meta-extras
158166
all = [
159167
"mcp-contextforge-gateway[redis]>=0.3.1",
@@ -317,7 +325,7 @@ exclude = [
317325

318326
[tool.pytest.ini_options]
319327
minversion = "6.0"
320-
addopts = "-ra -q --cov=mcpgateway"
328+
addopts = "-ra -q --cov=mcpgateway --ignore=tests/playwright"
321329
testpaths = [ "tests",]
322330
asyncio_mode = "auto"
323331
filterwarnings = [
@@ -329,6 +337,31 @@ env = [
329337
"MCPGATEWAY_ADMIN_API_ENABLED=true",
330338
"MCPGATEWAY_UI_ENABLED=true"
331339
]
340+
341+
# ===== PLAYWRIGHT-SPECIFIC CONFIGURATIONS =====
342+
# Playwright test markers
343+
markers = [
344+
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
345+
"ui: marks tests as UI tests",
346+
"api: marks tests as API tests",
347+
"smoke: marks tests as smoke tests for quick validation",
348+
"e2e: marks tests as end-to-end tests",
349+
]
350+
351+
# Playwright-specific test discovery patterns
352+
python_files = ["test_*.py", "*_test.py"]
353+
python_classes = ["Test*"]
354+
python_functions = ["test_*"]
355+
356+
# Playwright browser configuration (can be overridden via CLI)
357+
# These are custom options that your conftest.py can read
358+
playwright_browser = "chromium" # default browser
359+
playwright_headed = false # run headless by default
360+
playwright_slow_mo = 0 # milliseconds delay between actions
361+
playwright_screenshot = "only-on-failure"
362+
playwright_video = "retain-on-failure"
363+
playwright_trace = "retain-on-failure"
364+
332365
# ── fawltydeps ─────────────────────────────────────────────────────
333366
[tool.fawltydeps]
334367
# only parse main pyproject.toml

tests/playwright/.gitignore

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Playwright Test Artifacts
2+
# This .gitignore is for tests/playwright/ directory
3+
4+
# Screenshots from failed tests
5+
screenshots/
6+
*.png
7+
*.jpg
8+
*.jpeg
9+
10+
# Test reports
11+
reports/
12+
*.html
13+
report.html
14+
*-report.html
15+
playwright-report/
16+
playwright-report-*/
17+
coverage/
18+
htmlcov/
19+
.coverage
20+
coverage.xml
21+
*.lcov
22+
23+
# Test videos
24+
videos/
25+
*.webm
26+
*.mp4
27+
*.avi
28+
*.mov
29+
30+
# Test traces
31+
traces/
32+
*.zip
33+
*.trace
34+
35+
# Playwright test results
36+
test-results/
37+
test-results-*/
38+
results/
39+
*.xml
40+
junit.xml
41+
*-results.xml
42+
43+
# Pytest cache
44+
.pytest_cache/
45+
__pycache__/
46+
*.pyc
47+
*.pyo
48+
49+
# IDE specific
50+
.idea/
51+
.vscode/
52+
*.swp
53+
*.swo
54+
*~
55+
56+
# OS specific
57+
.DS_Store
58+
Thumbs.db
59+
60+
# Temporary files
61+
*.tmp
62+
*.temp
63+
*.log
64+
*.bak
65+
66+
# Environment files (if any test-specific)
67+
.env.test.local
68+
.env.playwright
69+
70+
# Node modules (if using any JS-based Playwright tools)
71+
node_modules/
72+
73+
# Downloaded browser binaries (usually in system cache, but just in case)
74+
.cache/
75+
browsers/
76+
77+
# Allure report artifacts (if using allure-pytest)
78+
allure-report/
79+
allure-results/
80+
81+
# Visual regression snapshots (keep these in git)
82+
# Comment out if you want to track snapshots
83+
# snapshots/
84+
# *-snapshots/
85+
86+
# Performance test results
87+
performance-results/
88+
lighthouse-reports/
89+
90+
# Accessibility test results
91+
a11y-reports/
92+
axe-results/
93+
94+
# Keep the directory structure
95+
!.gitkeep
96+
!__init__.py
97+
!pages/__init__.py

tests/playwright/README.md

Whitespace-only changes.

tests/playwright/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
"""Playwright UI tests for MCP Context Forge."""

0 commit comments

Comments
 (0)