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_structure.py · 5.7 KB · 169 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Fast structural tests for semantic components. |
| 4 | Tests DOM structure, ARIA attributes, and basic accessibility. |
| 5 | Cost: FREE (Playwright DOM queries only) |
| 6 | Run: Every commit |
| 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 | class StructuralTester: |
| 26 | def __init__(self): |
| 27 | self.results = [] |
| 28 | self.docs_dir = Path(__file__).parent / 'docs' |
| 29 | |
| 30 | def test_component(self, page, component_name): |
| 31 | """Run structural tests on a component page""" |
| 32 | doc_path = self.docs_dir / component_name |
| 33 | page.goto(f'file://{doc_path}') |
| 34 | page.wait_for_load_state('networkidle') |
| 35 | |
| 36 | issues = [] |
| 37 | |
| 38 | # Test 1: All interactive elements have proper ARIA |
| 39 | interactive = page.locator('button, a[href], input, [role="button"], [role="link"]').all() |
| 40 | for element in interactive: |
| 41 | aria_label = element.get_attribute('aria-label') |
| 42 | inner_text = element.inner_text().strip() if element.inner_text() else '' |
| 43 | |
| 44 | if not aria_label and not inner_text: |
| 45 | issues.append({ |
| 46 | 'type': 'missing_aria', |
| 47 | 'element': element.evaluate('el => el.tagName.toLowerCase()'), |
| 48 | 'severity': 'error' |
| 49 | }) |
| 50 | |
| 51 | # Test 2: All images have alt text |
| 52 | images = page.locator('img').all() |
| 53 | for img in images: |
| 54 | alt = img.get_attribute('alt') |
| 55 | if alt is None: |
| 56 | issues.append({ |
| 57 | 'type': 'missing_alt', |
| 58 | 'element': 'img', |
| 59 | 'src': img.get_attribute('src'), |
| 60 | 'severity': 'error' |
| 61 | }) |
| 62 | |
| 63 | # Test 3: Form inputs have labels |
| 64 | inputs = page.locator('input, textarea, select').all() |
| 65 | for input_elem in inputs: |
| 66 | input_id = input_elem.get_attribute('id') |
| 67 | aria_label = input_elem.get_attribute('aria-label') |
| 68 | |
| 69 | if input_id: |
| 70 | label = page.locator(f'label[for="{input_id}"]').count() |
| 71 | if label == 0 and not aria_label: |
| 72 | issues.append({ |
| 73 | 'type': 'missing_label', |
| 74 | 'element': input_elem.evaluate('el => el.tagName.toLowerCase()'), |
| 75 | 'severity': 'error' |
| 76 | }) |
| 77 | |
| 78 | # Test 4: Heading hierarchy |
| 79 | headings = page.locator('h1, h2, h3, h4, h5, h6').all() |
| 80 | prev_level = 0 |
| 81 | for heading in headings: |
| 82 | tag = heading.evaluate('el => el.tagName') |
| 83 | level = int(tag[1]) |
| 84 | |
| 85 | if prev_level > 0 and level - prev_level > 1: |
| 86 | issues.append({ |
| 87 | 'type': 'heading_skip', |
| 88 | 'from': f'h{prev_level}', |
| 89 | 'to': f'h{level}', |
| 90 | 'severity': 'warning' |
| 91 | }) |
| 92 | prev_level = level |
| 93 | |
| 94 | # Test 5: Touch targets (44x44px minimum) |
| 95 | buttons = page.locator('button, [role="button"]').all() |
| 96 | for button in buttons: |
| 97 | box = button.bounding_box() |
| 98 | if box: |
| 99 | if box['width'] < 44 or box['height'] < 44: |
| 100 | issues.append({ |
| 101 | 'type': 'small_touch_target', |
| 102 | 'element': 'button', |
| 103 | 'size': f"{box['width']}x{box['height']}", |
| 104 | 'severity': 'error' |
| 105 | }) |
| 106 | |
| 107 | # Test 6: Modals have proper ARIA |
| 108 | modals = page.locator('[role="dialog"]').all() |
| 109 | for modal in modals: |
| 110 | aria_modal = modal.get_attribute('aria-modal') |
| 111 | if aria_modal != 'true': |
| 112 | issues.append({ |
| 113 | 'type': 'modal_missing_aria', |
| 114 | 'element': 'dialog', |
| 115 | 'severity': 'error' |
| 116 | }) |
| 117 | |
| 118 | return { |
| 119 | 'component': component_name, |
| 120 | 'passed': len(issues) == 0, |
| 121 | 'issues': issues, |
| 122 | 'issue_count': len(issues) |
| 123 | } |
| 124 | |
| 125 | def run_all_tests(self): |
| 126 | """Test all components""" |
| 127 | print("\n🏗️ Running structural tests...\n") |
| 128 | |
| 129 | with sync_playwright() as p: |
| 130 | browser = p.chromium.launch(headless=True) |
| 131 | page = browser.new_page(viewport={'width': 1400, 'height': 1200}) |
| 132 | |
| 133 | for component in COMPONENT_PAGES: |
| 134 | print(f"🔍 Testing {component}...") |
| 135 | result = self.test_component(page, component) |
| 136 | self.results.append(result) |
| 137 | |
| 138 | if result['passed']: |
| 139 | print(f"✅ {component} - PASSED") |
| 140 | else: |
| 141 | print(f"❌ {component} - {result['issue_count']} issues") |
| 142 | |
| 143 | browser.close() |
| 144 | |
| 145 | # Generate report |
| 146 | self.generate_report() |
| 147 | |
| 148 | def generate_report(self): |
| 149 | """Generate test report""" |
| 150 | passed = sum(1 for r in self.results if r['passed']) |
| 151 | total = len(self.results) |
| 152 | |
| 153 | # Save JSON |
| 154 | results_path = Path(__file__).parent / 'test-results' / 'results-structure.json' |
| 155 | results_path.parent.mkdir(exist_ok=True) |
| 156 | with open(results_path, 'w') as f: |
| 157 | json.dump(self.results, f, indent=2) |
| 158 | |
| 159 | print(f"\n📊 Structural tests complete!") |
| 160 | print(f" Passed: {passed}/{total}") |
| 161 | print(f" Results: {results_path}") |
| 162 | |
| 163 | # Exit with error if any tests failed |
| 164 | if passed < total: |
| 165 | exit(1) |
| 166 | |
| 167 | if __name__ == '__main__': |
| 168 | tester = StructuralTester() |
| 169 | tester.run_all_tests() |