Playwright Python Migration Design
Migrate browser tests from Node/Playwright to Python/pytest-playwright.
Goal
Consolidate testing under Python/pytest, eliminating the Node dependency for browser tests while preserving full test coverage for mobile margin behavior.
Current State
tests/mobile-margins.spec.js- 11 tests across 6 describe blocksplaywright.config.js- configures Mobile Safari (iPhone 13) and Mobile Chrome (Pixel 5)package.json- Node dependencies for @playwright/test- Tests run via
npx playwright test/make test-mobile
Target State
src/tests/test_mobile_margins.py- same tests, pytest stylesrc/tests/conftest.py- mobile device fixtures- Tests run via
pytest/make test-mobile - No Node artifacts remaining
Dependencies
Add to pyproject.toml:
[project.optional-dependencies]
test = [
"pytest",
"pytest-playwright",
]
After install: playwright install chromium
Fixtures (conftest.py)
import pytest
from playwright.sync_api import sync_playwright
MOBILE_DEVICES = ["iPhone 13", "Pixel 5"]
BASE_URL = "http://localhost:1969"
@pytest.fixture(scope="session")
def playwright_instance():
with sync_playwright() as p:
yield p
@pytest.fixture(scope="session")
def browser(playwright_instance):
browser = playwright_instance.chromium.launch()
yield browser
browser.close()
@pytest.fixture(params=MOBILE_DEVICES)
def mobile_context(request, playwright_instance, browser):
device = playwright_instance.devices[request.param]
context = browser.new_context(**device, base_url=BASE_URL)
yield context
context.close()
@pytest.fixture
def mobile_page(mobile_context):
page = mobile_context.new_page()
yield page
page.close()
@pytest.fixture(params=MOBILE_DEVICES)
def mobile_nojs_context(request, playwright_instance, browser):
"""For testing graceful degradation without JavaScript."""
device = playwright_instance.devices[request.param]
context = browser.new_context(
**device,
base_url=BASE_URL,
java_script_enabled=False
)
yield context
context.close()
@pytest.fixture
def mobile_nojs_page(mobile_nojs_context):
page = mobile_nojs_context.new_page()
yield page
page.close()
Test Translation Pattern
| JS Pattern | Python Pattern |
|---|---|
test.describe('Name', () => {...}) |
class TestName: |
test('does thing', async ({ page }) => {...}) |
def test_does_thing(self, mobile_page): |
await page.goto(url) |
mobile_page.goto(url) |
await page.waitForSelector(sel) |
mobile_page.wait_for_selector(sel) |
await expect(loc).toBeVisible() |
assert loc.is_visible() or expect(loc).to_be_visible() |
await loc.tap() |
loc.tap() |
await loc.evaluate(fn) |
loc.evaluate('fn as string') |
Test Groups to Migrate
- Mobile Margins - No JS Fallback (1 test) - uses
mobile_nojs_page - Mobile Margins - Swipe Layout (2 tests)
- Mobile Margins - Initial State (1 test)
- Mobile Margins - Footnote Navigation (4 tests)
- Mobile Margins - Swipe Gestures (2 tests)
- Mobile Margins - Peek Gutters (1 test)
Total: 11 tests x 2 devices = 22 test runs
Makefile Changes
test-mobile: check-activated
pytest src/tests/test_mobile_margins.py -v
test-mobile-headed: check-activated
pytest src/tests/test_mobile_margins.py -v --headed
Cleanup
Remove after migration verified:
- tests/ directory (the Node one at project root)
- playwright.config.js
- package.json
- package-lock.json
- node_modules/
Implementation Steps
- Add pytest-playwright to pyproject.toml
- Run
uv syncandplaywright install chromium - Add fixtures to src/tests/conftest.py
- Create src/tests/test_mobile_margins.py with translated tests
- Verify tests pass:
make servein one terminal,pytest src/tests/test_mobile_margins.py -vin another - Update Makefile targets
- Remove Node artifacts
- Commit