askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
test_context.py · 7.1 KB · 200 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Context-aware testing across different viewports and modalities. |
| 4 | Tests how components adapt between mobile, tablet, and desktop. |
| 5 | Cost: FREE (Playwright viewport testing) |
| 6 | Run: On PR |
| 7 | """ |
| 8 | |
| 9 | from playwright.sync_api import sync_playwright |
| 10 | from pathlib import Path |
| 11 | import json |
| 12 | |
| 13 | COMPONENT_PAGES = [ |
| 14 | 'semantic-action.html', |
| 15 | 'semantic-card.html', |
| 16 | 'semantic-input.html', |
| 17 | 'semantic-modal.html', |
| 18 | 'semantic-navigator.html', |
| 19 | 'semantic-list.html', |
| 20 | 'semantic-menu.html', |
| 21 | 'semantic-feedback.html', |
| 22 | 'semantic-adjuster.html' |
| 23 | ] |
| 24 | |
| 25 | CONTEXTS = [ |
| 26 | { |
| 27 | 'name': 'mobile', |
| 28 | 'viewport': {'width': 375, 'height': 667}, |
| 29 | 'user_agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)', |
| 30 | 'has_touch': True |
| 31 | }, |
| 32 | { |
| 33 | 'name': 'tablet', |
| 34 | 'viewport': {'width': 768, 'height': 1024}, |
| 35 | 'user_agent': 'Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X)', |
| 36 | 'has_touch': True |
| 37 | }, |
| 38 | { |
| 39 | 'name': 'desktop', |
| 40 | 'viewport': {'width': 1400, 'height': 1200}, |
| 41 | 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', |
| 42 | 'has_touch': False |
| 43 | } |
| 44 | ] |
| 45 | |
| 46 | class ContextTester: |
| 47 | def __init__(self): |
| 48 | self.results = [] |
| 49 | self.docs_dir = Path(__file__).parent / 'docs' |
| 50 | |
| 51 | def test_component_context(self, page, component_name, context): |
| 52 | """Test component in specific context""" |
| 53 | doc_path = self.docs_dir / component_name |
| 54 | page.goto(f'file://{doc_path}') |
| 55 | page.wait_for_load_state('networkidle') |
| 56 | |
| 57 | issues = [] |
| 58 | |
| 59 | # Test 1: Touch targets in touch contexts |
| 60 | if context['has_touch']: |
| 61 | buttons = page.locator('button, [role="button"], a[href]').all() |
| 62 | for button in buttons: |
| 63 | box = button.bounding_box() |
| 64 | if box and (box['width'] < 44 or box['height'] < 44): |
| 65 | issues.append({ |
| 66 | 'type': 'small_touch_target_mobile', |
| 67 | 'element': button.evaluate('el => el.tagName.toLowerCase()'), |
| 68 | 'size': f"{box['width']}x{box['height']}", |
| 69 | 'context': context['name'], |
| 70 | 'severity': 'error' |
| 71 | }) |
| 72 | |
| 73 | # Test 2: Viewport responsiveness |
| 74 | viewport_width = context['viewport']['width'] |
| 75 | body_width = page.evaluate('document.body.scrollWidth') |
| 76 | |
| 77 | if body_width > viewport_width: |
| 78 | issues.append({ |
| 79 | 'type': 'horizontal_overflow', |
| 80 | 'viewport': viewport_width, |
| 81 | 'content': body_width, |
| 82 | 'context': context['name'], |
| 83 | 'severity': 'error' |
| 84 | }) |
| 85 | |
| 86 | # Test 3: Text readability (font size minimum) |
| 87 | text_elements = page.locator('p, span, div, li').all() |
| 88 | for elem in text_elements[:10]: # Sample first 10 |
| 89 | try: |
| 90 | font_size = elem.evaluate('el => window.getComputedStyle(el).fontSize') |
| 91 | size_px = float(font_size.replace('px', '')) |
| 92 | |
| 93 | # Mobile should have at least 16px for body text |
| 94 | if context['has_touch'] and size_px < 14: |
| 95 | issues.append({ |
| 96 | 'type': 'small_font_mobile', |
| 97 | 'size': f'{size_px}px', |
| 98 | 'context': context['name'], |
| 99 | 'severity': 'warning' |
| 100 | }) |
| 101 | except: |
| 102 | pass |
| 103 | |
| 104 | # Test 4: Interactive elements spacing (mobile) |
| 105 | if context['has_touch']: |
| 106 | interactive = page.locator('button, a[href], input').all() |
| 107 | for i in range(len(interactive) - 1): |
| 108 | try: |
| 109 | box1 = interactive[i].bounding_box() |
| 110 | box2 = interactive[i+1].bounding_box() |
| 111 | |
| 112 | if box1 and box2: |
| 113 | # Check vertical spacing |
| 114 | if abs(box1['y'] - box2['y']) < 8: |
| 115 | # Elements on same row, check horizontal spacing |
| 116 | spacing = abs(box2['x'] - (box1['x'] + box1['width'])) |
| 117 | if spacing < 8: |
| 118 | issues.append({ |
| 119 | 'type': 'insufficient_spacing', |
| 120 | 'spacing': f'{spacing}px', |
| 121 | 'context': context['name'], |
| 122 | 'severity': 'warning' |
| 123 | }) |
| 124 | except: |
| 125 | pass |
| 126 | |
| 127 | return { |
| 128 | 'component': component_name, |
| 129 | 'context': context['name'], |
| 130 | 'viewport': f"{context['viewport']['width']}x{context['viewport']['height']}", |
| 131 | 'passed': len(issues) == 0, |
| 132 | 'issues': issues, |
| 133 | 'issue_count': len(issues) |
| 134 | } |
| 135 | |
| 136 | def run_all_tests(self): |
| 137 | """Test all components across all contexts""" |
| 138 | print("\n🌐 Running context-aware tests...\n") |
| 139 | |
| 140 | with sync_playwright() as p: |
| 141 | browser = p.chromium.launch(headless=True) |
| 142 | |
| 143 | for context in CONTEXTS: |
| 144 | print(f"\n📱 Testing in {context['name'].upper()} context...") |
| 145 | print(f" Viewport: {context['viewport']['width']}x{context['viewport']['height']}") |
| 146 | |
| 147 | page = browser.new_page( |
| 148 | viewport=context['viewport'], |
| 149 | user_agent=context['user_agent'] |
| 150 | ) |
| 151 | |
| 152 | for component in COMPONENT_PAGES: |
| 153 | print(f" 🔍 {component}...", end='') |
| 154 | result = self.test_component_context(page, component, context) |
| 155 | self.results.append(result) |
| 156 | |
| 157 | if result['passed']: |
| 158 | print(" ✅") |
| 159 | else: |
| 160 | print(f" ❌ ({result['issue_count']} issues)") |
| 161 | |
| 162 | page.close() |
| 163 | |
| 164 | browser.close() |
| 165 | |
| 166 | self.generate_report() |
| 167 | |
| 168 | def generate_report(self): |
| 169 | """Generate test report""" |
| 170 | # Count by context |
| 171 | context_stats = {} |
| 172 | for context in CONTEXTS: |
| 173 | results_for_context = [r for r in self.results if r['context'] == context['name']] |
| 174 | passed = sum(1 for r in results_for_context if r['passed']) |
| 175 | total = len(results_for_context) |
| 176 | context_stats[context['name']] = {'passed': passed, 'total': total} |
| 177 | |
| 178 | # Save JSON |
| 179 | results_path = Path(__file__).parent / 'test-results' / 'results-context.json' |
| 180 | results_path.parent.mkdir(exist_ok=True) |
| 181 | with open(results_path, 'w') as f: |
| 182 | json.dump(self.results, f, indent=2) |
| 183 | |
| 184 | print(f"\n{'='*60}") |
| 185 | print("📊 Context tests complete!") |
| 186 | print(f"{'='*60}\n") |
| 187 | |
| 188 | for context_name, stats in context_stats.items(): |
| 189 | print(f"{context_name.upper()}: {stats['passed']}/{stats['total']} passed") |
| 190 | |
| 191 | print(f"\nResults: {results_path}") |
| 192 | |
| 193 | # Exit with error if any tests failed |
| 194 | total_passed = sum(r['passed'] for r in self.results) |
| 195 | if total_passed < len(self.results): |
| 196 | exit(1) |
| 197 | |
| 198 | if __name__ == '__main__': |
| 199 | tester = ContextTester() |
| 200 | tester.run_all_tests() |