Layout Integration Tests Design
Date: 2026-01-26 Status: Design complete, ready for implementation
Overview
A comprehensive integration test suite for validating layout behavior across browsers and devices. Tests sidenote positioning, margin element overlap prevention, viewport visibility, and permalink functionality.
Architecture
Three layers:
-
assets/js/layout-diagnostics.js— Injectable core module that runs in-browser, returns structured JSON results, and optionally renders visual debugging overlays. -
src/tests/conftest.pyadditions — Playwright fixtures for browser matrix (Chromium, Firefox, WebKit), viewport parametrization, and iOS simulator integration via Appium. -
src/tests/test_layout_*.py— Test files that use the diagnostics module through Playwright, asserting on JSON results.
Loading Strategy
The diagnostics script is served by Django but only loads when:
- DEBUG=True AND ?diagnostics=1 query param present, OR
- Playwright injects it via page.add_script_tag()
This keeps production clean while enabling both automated and manual debugging.
Diagnostics Module API
LayoutDiagnostics = {
// Configuration
config: {
tolerance: 20, // Default alignment tolerance in px
showOverlays: true, // Draw visual debugging overlays
overlayDuration: 5000, // How long overlays persist (ms), 0 = permanent
},
// Test methods - each returns { pass: bool, issues: [], measurements: {} }
checkSidenoteAlignment(), // Sidenote top vs footnote-ref vertical position
checkOverlaps(), // No margin elements overlapping each other
checkViewportVisibility(), // Body text readable, not clipped/obscured
checkPermalinks(), // Positioned correctly, clickable, copy works
checkLayoutMode(), // Returns 'desktop' | 'swipe' | 'nojs-fallback'
// Run all checks
runAll(), // Returns combined results object
// Visual overlay controls
showOverlays(), // Render current issues as colored boxes/lines
clearOverlays(), // Remove all overlay elements
// Utility for manual debugging
inspect(selector), // Highlight element, log its layout metrics
}
Return Format Example
{
pass: false,
issues: [
{ sidenote: "sn:example", footnote: "fnref:example",
delta: 45, threshold: 20, message: "45px drift from reference" }
],
measurements: {
"sn:example": { top: 1240, refTop: 1195, delta: 45 },
}
}
Visual Overlay System
When showOverlays() is called or config.showOverlays is true:
Color Coding
- Green — Passing elements (correctly aligned, no overlap)
- Red — Failing elements (misaligned, overlapping, out of bounds)
- Yellow — Warnings (close to threshold)
Overlay Types
- Alignment lines — Horizontal dashed lines connecting footnote-ref to sidenote top
- Bounding boxes — Semi-transparent rectangles around margin elements
- Overlap zones — Red hatched areas where two elements intersect
- Viewport frame — Dotted outline showing current viewport boundaries
Info Panel
Fixed-position panel showing: - Summary: “3 passed, 1 failed, 1 warning” - Clickable list of issues (clicking scrolls to element) - “Copy JSON” button for raw results
DOM Isolation
Overlays render into <div id="layout-diagnostics-overlay"> with high z-index and pointer-events: none.
Browser Matrix
Playwright (Local CI)
BROWSER_ENGINES = ["chromium", "firefox", "webkit"]
@pytest.fixture(params=BROWSER_ENGINES)
def browser_engine(request, playwright_instance):
engine = getattr(playwright_instance, request.param)
browser = engine.launch()
yield browser, request.param
browser.close()
iOS Simulator (Appium)
Separate test file with Appium/XCUITest driver:
@pytest.fixture(scope="session")
def ios_safari_driver():
from appium import webdriver
from appium.options.ios import XCUITestOptions
options = XCUITestOptions()
options.platform_name = "iOS"
options.device_name = "iPhone 16"
options.browser_name = "Safari"
options.automation_name = "XCUITest"
driver = webdriver.Remote("http://localhost:4723", options=options)
yield driver
driver.quit()
Run separately: pytest -m ios
Viewport & Breakpoint Testing
Breakpoints
BREAKPOINTS = [
{"name": "mobile-portrait", "width": 390, "height": 844},
{"name": "mobile-landscape", "width": 844, "height": 390},
{"name": "tablet", "width": 768, "height": 1024},
{"name": "desktop-narrow", "width": 1024, "height": 768},
{"name": "desktop-wide", "width": 1440, "height": 900},
]
Checks at Each Breakpoint
- Body text column fully within viewport horizontally
- Text not obscured by margin columns
- Minimum readable width maintained (body text >= 300px)
- Margins either visible (desktop) or accessible via swipe (mobile)
Layout Mode Transition Test
Sweeps viewport width from 1200px to 320px to detect where desktop-to-swipe transition occurs:
def test_swipe_mode_activates_at_expected_breakpoint(self, page):
transition_width = None
for width in range(1200, 320, -10):
page.set_viewport_size({"width": width, "height": 800})
page.wait_for_timeout(100)
mode = page.evaluate('LayoutDiagnostics.checkLayoutMode().mode')
if mode == 'swipe' and transition_width is None:
transition_width = width
break
assert 700 <= transition_width <= 950
Permalink Testing
Static Checks (via diagnostics module)
checkPermalinks() {
// For each .sidenote, .margin-box, .figure
return {
pass: bool,
issues: [],
measurements: {
"sn:example": {
hasPermalink: true,
fragmentId: "sn:example",
position: { top: 5, right: 5 },
visibility: "hidden",
clickable: true,
}
}
}
}
Interaction Tests (Playwright)
class TestPermalinkBehavior:
def test_permalink_appears_on_hover(self, desktop_page):
sidenote = page.locator('.sidenote').first
permalink = sidenote.locator('.permalink')
assert not permalink.is_visible()
sidenote.hover()
assert permalink.is_visible()
def test_permalink_copies_fragment_url(self, desktop_page):
# Click permalink, verify clipboard contains fragment URL
def test_long_press_shows_permalink_on_mobile(self, mobile_page):
# Long-press gesture test for touch devices
File Structure
assets/js/
layout-diagnostics.js # Injectable core module (NEW)
src/tests/
conftest.py # Add browser matrix, viewport, iOS fixtures (MODIFY)
test_layout_integration.py # Sidenote alignment, overlaps, visibility (NEW)
test_layout_permalinks.py # Permalink positioning and interaction (NEW)
test_layout_ios.py # iOS-specific tests via Appium (NEW)
Make Targets
test-layout: ## Run layout integration tests (all browsers)
pytest src/tests/test_layout_*.py -v
test-layout-ios: ## Run iOS simulator layout tests
pytest src/tests/test_layout_ios.py -v -m ios
Implementation Order
- Core module —
layout-diagnostics.jswithcheckSidenoteAlignment()and visual overlays - Playwright fixtures — Browser matrix, viewport parametrization
- First test file —
test_layout_integration.pywith alignment tests - Iterate — Add
checkOverlaps(),checkViewportVisibility(),checkPermalinks()one by one - iOS harness — Add Appium fixtures and
test_layout_ios.pylast
Configuration
Pytest Options
def pytest_addoption(parser):
parser.addoption("--layout-tolerance", type=int, default=20,
help="Pixel tolerance for alignment checks")
Tolerance Guidelines
- 20px default — Reasonable for cross-browser variance
- Stricter (10px) — For regression testing on known-good layouts
- Relaxed (50px) — For initial exploration or problematic pages