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_attention.py · 13.6 KB · 368 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Attention budget tests. |
| 4 | Tests cognitive load, visual complexity, and attention score adaptation. |
| 5 | Cost: FREE (Computed metrics from DOM/screenshots) |
| 6 | Run: On PR |
| 7 | """ |
| 8 | |
| 9 | from playwright.sync_api import sync_playwright |
| 10 | from pathlib import Path |
| 11 | import json |
| 12 | import math |
| 13 | |
| 14 | class AttentionTester: |
| 15 | def __init__(self): |
| 16 | self.results = [] |
| 17 | |
| 18 | def calculate_visual_complexity(self, page): |
| 19 | """Calculate visual complexity score""" |
| 20 | metrics = page.evaluate("""() => { |
| 21 | const walker = document.createTreeWalker( |
| 22 | document.body, |
| 23 | NodeFilter.SHOW_ELEMENT, |
| 24 | null |
| 25 | ); |
| 26 | |
| 27 | let elementCount = 0; |
| 28 | let colorCount = new Set(); |
| 29 | let fontCount = new Set(); |
| 30 | let animatingElements = 0; |
| 31 | |
| 32 | while (walker.nextNode()) { |
| 33 | const el = walker.currentNode; |
| 34 | const style = window.getComputedStyle(el); |
| 35 | |
| 36 | elementCount++; |
| 37 | colorCount.add(style.color); |
| 38 | colorCount.add(style.backgroundColor); |
| 39 | fontCount.add(style.fontFamily); |
| 40 | |
| 41 | // Check for animations |
| 42 | if (style.animation !== 'none' || style.transition !== 'none') { |
| 43 | animatingElements++; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return { |
| 48 | elementCount, |
| 49 | colorCount: colorCount.size, |
| 50 | fontCount: fontCount.size, |
| 51 | animatingElements |
| 52 | }; |
| 53 | }""") |
| 54 | |
| 55 | # Calculate complexity score (0-100) |
| 56 | # More elements, colors, fonts = higher complexity |
| 57 | element_score = min((metrics['elementCount'] / 50) * 30, 30) # Max 30 points |
| 58 | color_score = min((metrics['colorCount'] / 10) * 30, 30) # Max 30 points |
| 59 | font_score = min((metrics['fontCount'] / 5) * 20, 20) # Max 20 points |
| 60 | animation_score = min((metrics['animatingElements'] / 5) * 20, 20) # Max 20 points |
| 61 | |
| 62 | complexity = element_score + color_score + font_score + animation_score |
| 63 | |
| 64 | return { |
| 65 | 'complexity_score': round(complexity, 1), |
| 66 | 'metrics': metrics |
| 67 | } |
| 68 | |
| 69 | def test_component_attention_budget(self, page, component_name): |
| 70 | """Test if component stays within attention budget""" |
| 71 | issues = [] |
| 72 | |
| 73 | # Load component page |
| 74 | file_path = Path(__file__).parent / 'docs' / component_name |
| 75 | page.goto(f'file://{file_path}') |
| 76 | page.wait_for_load_state('networkidle') |
| 77 | |
| 78 | # Calculate visual complexity |
| 79 | complexity = self.calculate_visual_complexity(page) |
| 80 | |
| 81 | # Thresholds: |
| 82 | # 0-30: Low complexity (good) |
| 83 | # 31-60: Medium complexity (acceptable) |
| 84 | # 61-100: High complexity (warning) |
| 85 | severity = 'info' |
| 86 | if complexity['complexity_score'] > 60: |
| 87 | severity = 'warning' |
| 88 | issues.append({ |
| 89 | 'type': 'high_visual_complexity', |
| 90 | 'score': complexity['complexity_score'], |
| 91 | 'severity': severity, |
| 92 | 'message': f"Visual complexity {complexity['complexity_score']}/100 exceeds recommended threshold" |
| 93 | }) |
| 94 | elif complexity['complexity_score'] > 30: |
| 95 | severity = 'info' |
| 96 | issues.append({ |
| 97 | 'type': 'medium_visual_complexity', |
| 98 | 'score': complexity['complexity_score'], |
| 99 | 'severity': severity, |
| 100 | 'message': f"Visual complexity {complexity['complexity_score']}/100 is acceptable" |
| 101 | }) |
| 102 | |
| 103 | return { |
| 104 | 'component': component_name, |
| 105 | 'passed': len([i for i in issues if i['severity'] in ['error', 'warning']]) == 0, |
| 106 | 'complexity': complexity, |
| 107 | 'issues': issues |
| 108 | } |
| 109 | |
| 110 | def test_interactive_element_count(self, page, component_name): |
| 111 | """Test number of interactive elements (attention budget)""" |
| 112 | issues = [] |
| 113 | |
| 114 | file_path = Path(__file__).parent / 'docs' / component_name |
| 115 | page.goto(f'file://{file_path}') |
| 116 | page.wait_for_load_state('networkidle') |
| 117 | |
| 118 | # Count interactive elements |
| 119 | interactive_count = page.locator('button, a[href], input, select, textarea, [role="button"], [tabindex]:not([tabindex="-1"])').count() |
| 120 | |
| 121 | # Thresholds based on Miller's Law (7±2 items) |
| 122 | # 0-9 items: Good (within working memory) |
| 123 | # 10-15 items: Warning (approaching cognitive overload) |
| 124 | # 16+ items: Error (too many choices) |
| 125 | |
| 126 | if interactive_count > 15: |
| 127 | issues.append({ |
| 128 | 'type': 'too_many_interactive_elements', |
| 129 | 'count': interactive_count, |
| 130 | 'recommended': '≤9', |
| 131 | 'severity': 'error', |
| 132 | 'message': f"{interactive_count} interactive elements exceeds cognitive load limit (Miller's Law: 7±2)" |
| 133 | }) |
| 134 | elif interactive_count > 9: |
| 135 | issues.append({ |
| 136 | 'type': 'high_interactive_count', |
| 137 | 'count': interactive_count, |
| 138 | 'recommended': '≤9', |
| 139 | 'severity': 'warning', |
| 140 | 'message': f"{interactive_count} interactive elements approaches cognitive overload" |
| 141 | }) |
| 142 | |
| 143 | return { |
| 144 | 'component': component_name, |
| 145 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 146 | 'interactive_count': interactive_count, |
| 147 | 'issues': issues |
| 148 | } |
| 149 | |
| 150 | def test_animation_attention_cost(self, page, component_name): |
| 151 | """Test that animations don't consume too much attention""" |
| 152 | issues = [] |
| 153 | |
| 154 | file_path = Path(__file__).parent / 'docs' / component_name |
| 155 | page.goto(f'file://{file_path}') |
| 156 | page.wait_for_load_state('networkidle') |
| 157 | |
| 158 | # Check for animations |
| 159 | animations = page.evaluate("""() => { |
| 160 | const elements = document.querySelectorAll('*'); |
| 161 | let autoplayAnimations = 0; |
| 162 | let longAnimations = 0; |
| 163 | let infiniteAnimations = 0; |
| 164 | |
| 165 | elements.forEach(el => { |
| 166 | const style = window.getComputedStyle(el); |
| 167 | const animations = style.animation; |
| 168 | |
| 169 | if (animations && animations !== 'none') { |
| 170 | // Check for autoplay (not triggered by hover/focus) |
| 171 | if (!el.matches(':hover') && !el.matches(':focus')) { |
| 172 | autoplayAnimations++; |
| 173 | } |
| 174 | |
| 175 | // Parse animation duration |
| 176 | const durationMatch = animations.match(/([\\d.]+)s/); |
| 177 | if (durationMatch && parseFloat(durationMatch[1]) > 3) { |
| 178 | longAnimations++; |
| 179 | } |
| 180 | |
| 181 | // Check for infinite animations |
| 182 | if (animations.includes('infinite')) { |
| 183 | infiniteAnimations++; |
| 184 | } |
| 185 | } |
| 186 | }); |
| 187 | |
| 188 | return { autoplayAnimations, longAnimations, infiniteAnimations }; |
| 189 | }""") |
| 190 | |
| 191 | if animations['autoplayAnimations'] > 3: |
| 192 | issues.append({ |
| 193 | 'type': 'too_many_autoplay_animations', |
| 194 | 'count': animations['autoplayAnimations'], |
| 195 | 'severity': 'warning', |
| 196 | 'message': f"{animations['autoplayAnimations']} autoplay animations consume attention budget" |
| 197 | }) |
| 198 | |
| 199 | if animations['infiniteAnimations'] > 0: |
| 200 | issues.append({ |
| 201 | 'type': 'infinite_animations', |
| 202 | 'count': animations['infiniteAnimations'], |
| 203 | 'severity': 'warning', |
| 204 | 'message': 'Infinite animations continuously consume attention' |
| 205 | }) |
| 206 | |
| 207 | return { |
| 208 | 'component': component_name, |
| 209 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 210 | 'animations': animations, |
| 211 | 'issues': issues |
| 212 | } |
| 213 | |
| 214 | def test_color_palette_size(self, page, component_name): |
| 215 | """Test that color palette is constrained""" |
| 216 | issues = [] |
| 217 | |
| 218 | file_path = Path(__file__).parent / 'docs' / component_name |
| 219 | page.goto(f'file://{file_path}') |
| 220 | page.wait_for_load_state('networkidle') |
| 221 | |
| 222 | # Count unique colors |
| 223 | colors = page.evaluate("""() => { |
| 224 | const elements = document.querySelectorAll('*'); |
| 225 | const colors = new Set(); |
| 226 | |
| 227 | elements.forEach(el => { |
| 228 | const style = window.getComputedStyle(el); |
| 229 | colors.add(style.color); |
| 230 | colors.add(style.backgroundColor); |
| 231 | colors.add(style.borderColor); |
| 232 | }); |
| 233 | |
| 234 | // Remove transparent/none |
| 235 | colors.delete('rgba(0, 0, 0, 0)'); |
| 236 | colors.delete('transparent'); |
| 237 | colors.delete('none'); |
| 238 | |
| 239 | return colors.size; |
| 240 | }""") |
| 241 | |
| 242 | # Threshold: More than 12 unique colors indicates poor color system |
| 243 | if colors > 12: |
| 244 | issues.append({ |
| 245 | 'type': 'too_many_colors', |
| 246 | 'count': colors, |
| 247 | 'recommended': '≤12', |
| 248 | 'severity': 'warning', |
| 249 | 'message': f"{colors} unique colors exceeds recommended palette size" |
| 250 | }) |
| 251 | |
| 252 | return { |
| 253 | 'component': component_name, |
| 254 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 255 | 'color_count': colors, |
| 256 | 'issues': issues |
| 257 | } |
| 258 | |
| 259 | def test_focus_trap_attention(self, page, component_name): |
| 260 | """Test that focus trapping doesn't consume excessive attention""" |
| 261 | issues = [] |
| 262 | |
| 263 | file_path = Path(__file__).parent / 'docs' / component_name |
| 264 | page.goto(f'file://{file_path}') |
| 265 | page.wait_for_load_state('networkidle') |
| 266 | |
| 267 | # Check for modal/dialog components that should trap focus |
| 268 | has_modal = page.locator('semantic-modal, [role="dialog"]').count() > 0 |
| 269 | |
| 270 | if has_modal: |
| 271 | # Test focus trap cycle length |
| 272 | focusable = page.locator('button, a[href], input, select, textarea, [tabindex]:not([tabindex="-1"])').all() |
| 273 | focusable_count = len(focusable) |
| 274 | |
| 275 | # If modal has more than 15 focusable elements, focus cycling becomes cognitive burden |
| 276 | if focusable_count > 15: |
| 277 | issues.append({ |
| 278 | 'type': 'long_focus_trap', |
| 279 | 'count': focusable_count, |
| 280 | 'severity': 'warning', |
| 281 | 'message': f"Focus trap contains {focusable_count} elements, making keyboard navigation difficult" |
| 282 | }) |
| 283 | |
| 284 | return { |
| 285 | 'component': component_name, |
| 286 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 287 | 'issues': issues |
| 288 | } |
| 289 | |
| 290 | def run_all_tests(self): |
| 291 | """Run all attention budget tests""" |
| 292 | print("\n🧠 Running attention budget tests...\n") |
| 293 | |
| 294 | components = [ |
| 295 | 'semantic-action.html', |
| 296 | 'semantic-card.html', |
| 297 | 'semantic-input.html', |
| 298 | 'semantic-modal.html', |
| 299 | 'semantic-navigator.html', |
| 300 | 'semantic-list.html', |
| 301 | 'semantic-menu.html', |
| 302 | 'semantic-feedback.html', |
| 303 | 'semantic-adjuster.html' |
| 304 | ] |
| 305 | |
| 306 | with sync_playwright() as p: |
| 307 | browser = p.chromium.launch(headless=True) |
| 308 | page = browser.new_page(viewport={'width': 1400, 'height': 1200}) |
| 309 | |
| 310 | for component in components: |
| 311 | print(f"\n📋 Testing {component}:") |
| 312 | |
| 313 | # Run all attention tests for this component |
| 314 | tests = [ |
| 315 | ('Visual Complexity', lambda: self.test_component_attention_budget(page, component)), |
| 316 | ('Interactive Elements', lambda: self.test_interactive_element_count(page, component)), |
| 317 | ('Animation Cost', lambda: self.test_animation_attention_cost(page, component)), |
| 318 | ('Color Palette', lambda: self.test_color_palette_size(page, component)), |
| 319 | ('Focus Trap', lambda: self.test_focus_trap_attention(page, component)) |
| 320 | ] |
| 321 | |
| 322 | for test_name, test_func in tests: |
| 323 | print(f" 🔍 {test_name}...", end='') |
| 324 | result = test_func() |
| 325 | self.results.append({ |
| 326 | 'test': test_name, |
| 327 | 'component': component, |
| 328 | 'passed': result['passed'], |
| 329 | 'issues': result.get('issues', []) |
| 330 | }) |
| 331 | |
| 332 | error_count = len([i for i in result.get('issues', []) if i['severity'] == 'error']) |
| 333 | warning_count = len([i for i in result.get('issues', []) if i['severity'] == 'warning']) |
| 334 | |
| 335 | if result['passed']: |
| 336 | if warning_count > 0: |
| 337 | print(f" ⚠️ ({warning_count} warnings)") |
| 338 | else: |
| 339 | print(" ✅") |
| 340 | else: |
| 341 | print(f" ❌ ({error_count} errors)") |
| 342 | |
| 343 | browser.close() |
| 344 | |
| 345 | self.generate_report() |
| 346 | |
| 347 | def generate_report(self): |
| 348 | """Generate test report""" |
| 349 | passed = sum(1 for r in self.results if r['passed']) |
| 350 | total = len(self.results) |
| 351 | |
| 352 | # Save JSON |
| 353 | results_path = Path(__file__).parent / 'test-results' / 'results-attention.json' |
| 354 | results_path.parent.mkdir(exist_ok=True) |
| 355 | with open(results_path, 'w') as f: |
| 356 | json.dump(self.results, f, indent=2) |
| 357 | |
| 358 | print(f"\n📊 Attention budget tests complete!") |
| 359 | print(f" Passed: {passed}/{total}") |
| 360 | print(f" Results: {results_path}") |
| 361 | |
| 362 | # Exit with error if any tests failed |
| 363 | if passed < total: |
| 364 | exit(1) |
| 365 | |
| 366 | if __name__ == '__main__': |
| 367 | tester = AttentionTester() |
| 368 | tester.run_all_tests() |