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
visual-test-suite.js · 27.0 KB · 718 lines
| 1 | /** |
| 2 | * Visual Testing Suite |
| 3 | * Automatically tests all component states, interactions, and accessibility |
| 4 | */ |
| 5 | |
| 6 | class VisualTestSuite { |
| 7 | constructor() { |
| 8 | this.tests = []; |
| 9 | this.results = []; |
| 10 | this.currentTestIndex = 0; |
| 11 | this.isRunning = false; |
| 12 | this.panel = null; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Initialize the test suite UI |
| 17 | */ |
| 18 | init() { |
| 19 | this.createPanel(); |
| 20 | this.registerDefaultTests(); |
| 21 | console.log('Visual Test Suite initialized. Press Ctrl+Shift+T to open.'); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create the test panel UI |
| 26 | */ |
| 27 | createPanel() { |
| 28 | this.panel = document.createElement('div'); |
| 29 | this.panel.id = 'visual-test-panel'; |
| 30 | this.panel.style.cssText = ` |
| 31 | position: fixed; |
| 32 | top: 24px; |
| 33 | left: 24px; |
| 34 | width: 400px; |
| 35 | max-height: 80vh; |
| 36 | background: #1c1c1e; |
| 37 | border: 2px solid #ff3b30; |
| 38 | border-radius: 16px; |
| 39 | padding: 24px; |
| 40 | z-index: 999999; |
| 41 | display: none; |
| 42 | overflow-y: auto; |
| 43 | box-shadow: 0 16px 48px rgba(0, 0, 0, 0.8); |
| 44 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; |
| 45 | color: #fff; |
| 46 | `; |
| 47 | |
| 48 | this.panel.innerHTML = ` |
| 49 | <div style="margin-bottom: 24px;"> |
| 50 | <h2 style="margin: 0 0 8px 0; font-size: 18px; color: #ff3b30;">🧪 Visual Test Suite</h2> |
| 51 | <p style="margin: 0; font-size: 12px; color: #98989d;">Automated component testing & accessibility checks</p> |
| 52 | </div> |
| 53 | |
| 54 | <div style="display: flex; gap: 8px; margin-bottom: 24px;"> |
| 55 | <button id="run-all-tests" style="flex: 1; padding: 12px; background: #34c759; border: none; border-radius: 8px; color: #000; font-weight: 700; cursor: pointer;"> |
| 56 | ▶ Run All Tests |
| 57 | </button> |
| 58 | <button id="stop-tests" style="flex: 1; padding: 12px; background: #ff3b30; border: none; border-radius: 8px; color: #000; font-weight: 700; cursor: pointer;" disabled> |
| 59 | ⏹ Stop |
| 60 | </button> |
| 61 | </div> |
| 62 | |
| 63 | <div style="margin-bottom: 16px;"> |
| 64 | <label style="display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer;"> |
| 65 | <input type="checkbox" id="auto-scroll" checked style="width: 18px; height: 18px;"> |
| 66 | Auto-scroll to component |
| 67 | </label> |
| 68 | <label style="display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; margin-top: 8px;"> |
| 69 | <input type="checkbox" id="check-contrast" checked style="width: 18px; height: 18px;"> |
| 70 | Check contrast ratios (WCAG AA) |
| 71 | </label> |
| 72 | </div> |
| 73 | |
| 74 | <div id="test-progress" style="margin-bottom: 16px; padding: 12px; background: #000; border-radius: 8px; font-size: 12px; font-family: monospace;"> |
| 75 | Ready to run tests... |
| 76 | </div> |
| 77 | |
| 78 | <div id="test-results" style="max-height: 300px; overflow-y: auto;"> |
| 79 | <!-- Results will appear here --> |
| 80 | </div> |
| 81 | `; |
| 82 | |
| 83 | document.body.appendChild(this.panel); |
| 84 | |
| 85 | // Keyboard shortcut to toggle |
| 86 | document.addEventListener('keydown', (e) => { |
| 87 | if (e.ctrlKey && e.shiftKey && e.key === 'T') { |
| 88 | this.toggle(); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | // Button handlers |
| 93 | document.getElementById('run-all-tests').addEventListener('click', () => this.runAllTests()); |
| 94 | document.getElementById('stop-tests').addEventListener('click', () => this.stopTests()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Toggle panel visibility |
| 99 | */ |
| 100 | toggle() { |
| 101 | if (this.panel.style.display === 'none') { |
| 102 | this.panel.style.display = 'block'; |
| 103 | } else { |
| 104 | this.panel.style.display = 'none'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Register default component tests |
| 110 | */ |
| 111 | registerDefaultTests() { |
| 112 | // Test semantic-action states |
| 113 | this.addTest({ |
| 114 | name: 'semantic-action: Default State', |
| 115 | description: 'Verify default button has proper contrast and styling', |
| 116 | run: async () => { |
| 117 | const buttons = document.querySelectorAll('semantic-action[intent="primary"]'); |
| 118 | if (buttons.length === 0) return { pass: false, message: 'No primary buttons found' }; |
| 119 | |
| 120 | const button = buttons[0]; |
| 121 | const styles = this.getComputedStyles(button); |
| 122 | const contrast = this.checkContrast(styles.color, styles.backgroundColor); |
| 123 | |
| 124 | return { |
| 125 | pass: contrast.ratio >= 4.5, |
| 126 | message: `Contrast ratio: ${contrast.ratio.toFixed(2)}:1 (${contrast.ratio >= 4.5 ? 'PASS' : 'FAIL WCAG AA'})`, |
| 127 | element: button |
| 128 | }; |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | // Test hover state |
| 133 | this.addTest({ |
| 134 | name: 'semantic-action: Hover State', |
| 135 | description: 'Simulate hover and verify visual feedback', |
| 136 | run: async () => { |
| 137 | const components = document.querySelectorAll('semantic-action[intent="primary"]'); |
| 138 | if (components.length === 0) return { pass: false, message: 'No primary buttons found' }; |
| 139 | |
| 140 | const component = components[0]; |
| 141 | const button = component.shadowRoot?.querySelector('button'); |
| 142 | if (!button) return { pass: false, message: 'Shadow DOM button not found' }; |
| 143 | |
| 144 | // Check if hover styles exist in the stylesheet |
| 145 | const styleSheet = component.shadowRoot.querySelector('style'); |
| 146 | if (!styleSheet) return { pass: false, message: 'No styles found' }; |
| 147 | |
| 148 | const cssText = styleSheet.textContent; |
| 149 | const hasHoverRule = cssText.includes('button:hover') || cssText.includes(':hover'); |
| 150 | const hasTransform = cssText.includes('transform') && cssText.includes('hover'); |
| 151 | const hasShadow = cssText.includes('box-shadow') && cssText.includes('hover'); |
| 152 | |
| 153 | // Manually trigger hover state by forcing focus and using a class |
| 154 | const originalStyles = { |
| 155 | transform: button.style.transform, |
| 156 | boxShadow: button.style.boxShadow |
| 157 | }; |
| 158 | |
| 159 | // Apply hover styles manually to test visual change |
| 160 | button.style.transform = 'translateY(-2px)'; |
| 161 | button.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.15)'; |
| 162 | |
| 163 | await this.wait(100); |
| 164 | |
| 165 | const testStyles = this.getComputedStyles(button); |
| 166 | const stylesApplied = |
| 167 | testStyles.transform.includes('translateY') || |
| 168 | testStyles.boxShadow !== 'none'; |
| 169 | |
| 170 | // Restore original styles |
| 171 | button.style.transform = originalStyles.transform; |
| 172 | button.style.boxShadow = originalStyles.boxShadow; |
| 173 | |
| 174 | const pass = hasHoverRule && (hasTransform || hasShadow) && stylesApplied; |
| 175 | |
| 176 | return { |
| 177 | pass: pass, |
| 178 | message: pass |
| 179 | ? 'Hover state has CSS rules with transform/shadow' |
| 180 | : `Missing hover styles (hasRule: ${hasHoverRule}, hasTransform: ${hasTransform}, hasShadow: ${hasShadow})`, |
| 181 | element: component |
| 182 | }; |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | // Test click feedback |
| 187 | this.addTest({ |
| 188 | name: 'semantic-action: Click Feedback', |
| 189 | description: 'Simulate click and verify active state', |
| 190 | run: async () => { |
| 191 | const components = document.querySelectorAll('semantic-action[intent="primary"]'); |
| 192 | if (components.length === 0) return { pass: false, message: 'No buttons found' }; |
| 193 | |
| 194 | const component = components[0]; |
| 195 | const button = component.shadowRoot?.querySelector('button'); |
| 196 | if (!button) return { pass: false, message: 'Shadow DOM button not found' }; |
| 197 | |
| 198 | // Check if active styles exist in CSS |
| 199 | const styleSheet = component.shadowRoot.querySelector('style'); |
| 200 | if (!styleSheet) return { pass: false, message: 'No styles found' }; |
| 201 | |
| 202 | const cssText = styleSheet.textContent; |
| 203 | const hasActiveRule = cssText.includes('button:active') || cssText.includes(':active'); |
| 204 | const hasScaleTransform = cssText.includes('scale') && cssText.includes('active'); |
| 205 | |
| 206 | // Manually apply active style to verify it works |
| 207 | const originalTransform = button.style.transform; |
| 208 | button.style.transform = 'scale(0.96)'; |
| 209 | |
| 210 | await this.wait(50); |
| 211 | |
| 212 | const testStyles = this.getComputedStyles(button); |
| 213 | const scaleApplied = testStyles.transform.includes('scale') || testStyles.transform.includes('matrix'); |
| 214 | |
| 215 | // Restore |
| 216 | button.style.transform = originalTransform; |
| 217 | |
| 218 | const pass = hasActiveRule && hasScaleTransform && scaleApplied; |
| 219 | |
| 220 | return { |
| 221 | pass: pass, |
| 222 | message: pass |
| 223 | ? 'Active state has CSS rules with scale transform' |
| 224 | : `Missing active styles (hasRule: ${hasActiveRule}, hasScale: ${hasScaleTransform})`, |
| 225 | element: component |
| 226 | }; |
| 227 | } |
| 228 | }); |
| 229 | |
| 230 | // Test disabled state |
| 231 | this.addTest({ |
| 232 | name: 'semantic-action: Disabled State', |
| 233 | description: 'Verify disabled buttons are visually distinct', |
| 234 | run: async () => { |
| 235 | const disabledButtons = document.querySelectorAll('semantic-action[disabled]'); |
| 236 | if (disabledButtons.length === 0) return { pass: false, message: 'No disabled buttons found for testing' }; |
| 237 | |
| 238 | const button = disabledButtons[0]; |
| 239 | const styles = this.getComputedStyles(button); |
| 240 | |
| 241 | // Check if opacity is reduced or color is greyed |
| 242 | const isVisuallyDisabled = |
| 243 | parseFloat(styles.opacity) < 0.7 || |
| 244 | this.isGreyish(styles.color); |
| 245 | |
| 246 | return { |
| 247 | pass: isVisuallyDisabled, |
| 248 | message: isVisuallyDisabled ? 'Disabled state is visually distinct' : 'Disabled state NOT visually obvious', |
| 249 | element: button |
| 250 | }; |
| 251 | } |
| 252 | }); |
| 253 | |
| 254 | // Test selectable boxes |
| 255 | this.addTest({ |
| 256 | name: 'State Demo: Selection Feedback', |
| 257 | description: 'Test selection box visual changes', |
| 258 | run: async () => { |
| 259 | const boxes = document.querySelectorAll('.state-demo-box.clickable'); |
| 260 | if (boxes.length === 0) return { pass: false, message: 'No selectable boxes found' }; |
| 261 | |
| 262 | // Find one that's not already selected |
| 263 | let box = boxes[0]; |
| 264 | for (let b of boxes) { |
| 265 | if (!b.classList.contains('selected')) { |
| 266 | box = b; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | const beforeStyles = this.getComputedStyles(box); |
| 272 | const beforeBorder = beforeStyles.borderColor; |
| 273 | const beforeBg = beforeStyles.backgroundColor; |
| 274 | |
| 275 | // Simulate click to select |
| 276 | box.click(); |
| 277 | await this.wait(300); // Wait for animation |
| 278 | |
| 279 | const afterStyles = this.getComputedStyles(box); |
| 280 | const isSelected = box.classList.contains('selected'); |
| 281 | |
| 282 | // Check for significant visual changes |
| 283 | const borderChanged = beforeBorder !== afterStyles.borderColor; |
| 284 | const bgChanged = beforeBg !== afterStyles.backgroundColor; |
| 285 | const hasBadge = box.querySelector('::before') || |
| 286 | window.getComputedStyle(box, '::before').content !== 'none'; |
| 287 | |
| 288 | // Check if green color is present (selection color) |
| 289 | const hasGreenBorder = afterStyles.borderColor.includes('52, 199, 89') || |
| 290 | afterStyles.borderColor.includes('#34c759'); |
| 291 | const hasGlow = afterStyles.boxShadow !== 'none' && afterStyles.boxShadow.includes('52, 199, 89'); |
| 292 | |
| 293 | const obviousChange = isSelected && (borderChanged || bgChanged) && (hasGreenBorder || hasGlow); |
| 294 | |
| 295 | return { |
| 296 | pass: obviousChange, |
| 297 | message: obviousChange |
| 298 | ? `Selection is visually obvious (green border: ${hasGreenBorder}, glow: ${hasGlow})` |
| 299 | : `Selection NOT obvious (selected: ${isSelected}, borderChanged: ${borderChanged}, greenBorder: ${hasGreenBorder})`, |
| 300 | element: box |
| 301 | }; |
| 302 | } |
| 303 | }); |
| 304 | |
| 305 | // Test card hover |
| 306 | this.addTest({ |
| 307 | name: 'semantic-card: Hover Elevation', |
| 308 | description: 'Verify cards lift and show preview on hover', |
| 309 | run: async () => { |
| 310 | const components = document.querySelectorAll('semantic-card'); |
| 311 | if (components.length === 0) return { pass: false, message: 'No cards found' }; |
| 312 | |
| 313 | const component = components[0]; |
| 314 | const card = component.shadowRoot?.querySelector('.card'); |
| 315 | if (!card) return { pass: false, message: 'Shadow DOM card not found' }; |
| 316 | |
| 317 | // Check if hover styles exist in CSS |
| 318 | const styleSheet = component.shadowRoot.querySelector('style'); |
| 319 | if (!styleSheet) return { pass: false, message: 'No styles found' }; |
| 320 | |
| 321 | const cssText = styleSheet.textContent; |
| 322 | const hasHoverRule = cssText.includes('.card:hover') || (cssText.includes(':hover') && cssText.includes('card')); |
| 323 | const hasTransform = cssText.includes('translateY') && cssText.includes('hover'); |
| 324 | const hasShadow = cssText.includes('box-shadow') && cssText.includes('hover'); |
| 325 | |
| 326 | // Manually apply hover styles to test they work |
| 327 | const originalStyles = { |
| 328 | transform: card.style.transform, |
| 329 | boxShadow: card.style.boxShadow, |
| 330 | borderColor: card.style.borderColor |
| 331 | }; |
| 332 | |
| 333 | card.style.transform = 'translateY(-4px)'; |
| 334 | card.style.boxShadow = '0 8px 24px rgba(0, 0, 0, 0.3)'; |
| 335 | card.style.borderColor = '#0066cc'; |
| 336 | |
| 337 | await this.wait(100); |
| 338 | |
| 339 | const testStyles = this.getComputedStyles(card); |
| 340 | const stylesWork = testStyles.transform.includes('translateY') || testStyles.transform.includes('matrix'); |
| 341 | |
| 342 | // Restore |
| 343 | card.style.transform = originalStyles.transform; |
| 344 | card.style.boxShadow = originalStyles.boxShadow; |
| 345 | card.style.borderColor = originalStyles.borderColor; |
| 346 | |
| 347 | const pass = hasHoverRule && (hasTransform || hasShadow) && stylesWork; |
| 348 | |
| 349 | return { |
| 350 | pass: pass, |
| 351 | message: pass |
| 352 | ? 'Card has hover CSS with elevation (transform + shadow)' |
| 353 | : `Missing hover styles (hasRule: ${hasHoverRule}, hasTransform: ${hasTransform})`, |
| 354 | element: component |
| 355 | }; |
| 356 | } |
| 357 | }); |
| 358 | |
| 359 | // Contrast ratio check for all text |
| 360 | this.addTest({ |
| 361 | name: 'Accessibility: Text Contrast', |
| 362 | description: 'Check all visible text meets WCAG AA (4.5:1)', |
| 363 | run: async () => { |
| 364 | const textElements = document.querySelectorAll('p, span, div, h1, h2, h3, label, button'); |
| 365 | const failures = []; |
| 366 | |
| 367 | for (const el of textElements) { |
| 368 | if (el.offsetParent === null) continue; // Skip hidden elements |
| 369 | if (el.textContent.trim() === '') continue; // Skip empty elements |
| 370 | |
| 371 | const styles = this.getComputedStyles(el); |
| 372 | if (!styles.color) continue; |
| 373 | |
| 374 | // Get effective background color (walk up the tree and flatten transparencies) |
| 375 | let effectiveBg = 'rgb(0, 0, 0)'; // Start with black (body bg) |
| 376 | let currentEl = el; |
| 377 | const bgStack = []; |
| 378 | |
| 379 | // Collect all background colors up to the root |
| 380 | while (currentEl && currentEl !== document.body) { |
| 381 | const bg = window.getComputedStyle(currentEl).backgroundColor; |
| 382 | if (bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent') { |
| 383 | bgStack.push(bg); |
| 384 | } |
| 385 | currentEl = currentEl.parentElement; |
| 386 | } |
| 387 | |
| 388 | // Flatten all semi-transparent backgrounds from bottom to top |
| 389 | for (let i = bgStack.length - 1; i >= 0; i--) { |
| 390 | effectiveBg = this.flattenColor(bgStack[i], effectiveBg); |
| 391 | } |
| 392 | |
| 393 | const contrast = this.checkContrast(styles.color, effectiveBg); |
| 394 | if (contrast.ratio < 4.5) { |
| 395 | failures.push({ |
| 396 | element: el, |
| 397 | ratio: contrast.ratio.toFixed(2), |
| 398 | text: el.textContent.substring(0, 30).trim(), |
| 399 | color: styles.color, |
| 400 | bgColor: effectiveBg, |
| 401 | tag: el.tagName.toLowerCase() |
| 402 | }); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Log first 5 failures for debugging |
| 407 | if (failures.length > 0) { |
| 408 | console.log('Contrast failures:', failures.slice(0, 5)); |
| 409 | } |
| 410 | |
| 411 | return { |
| 412 | pass: failures.length === 0, |
| 413 | message: failures.length === 0 |
| 414 | ? 'All text passes WCAG AA contrast' |
| 415 | : `${failures.length} elements fail (first: "${failures[0]?.text}" - ${failures[0]?.ratio}:1, color: ${failures[0]?.color} on ${failures[0]?.bgColor})`, |
| 416 | details: failures |
| 417 | }; |
| 418 | } |
| 419 | }); |
| 420 | |
| 421 | // Test focus indicators |
| 422 | this.addTest({ |
| 423 | name: 'Accessibility: Focus Indicators', |
| 424 | description: 'Verify all interactive elements have focus indicators', |
| 425 | run: async () => { |
| 426 | const focusables = document.querySelectorAll('button, a, semantic-action, semantic-card, .state-demo-box.clickable'); |
| 427 | const failures = []; |
| 428 | |
| 429 | for (const el of focusables) { |
| 430 | el.focus(); |
| 431 | await this.wait(50); |
| 432 | |
| 433 | const styles = this.getComputedStyles(el); |
| 434 | const hasFocusIndicator = |
| 435 | styles.outline !== 'none' || |
| 436 | styles.boxShadow.includes('rgb') || |
| 437 | styles.borderColor !== 'rgb(0, 0, 0)'; |
| 438 | |
| 439 | if (!hasFocusIndicator) { |
| 440 | failures.push(el); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return { |
| 445 | pass: failures.length === 0, |
| 446 | message: failures.length === 0 |
| 447 | ? 'All focusable elements have focus indicators' |
| 448 | : `${failures.length} elements missing focus indicators`, |
| 449 | details: failures |
| 450 | }; |
| 451 | } |
| 452 | }); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Add a test to the suite |
| 457 | */ |
| 458 | addTest(test) { |
| 459 | this.tests.push(test); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Run all tests sequentially |
| 464 | */ |
| 465 | async runAllTests() { |
| 466 | this.isRunning = true; |
| 467 | this.results = []; |
| 468 | this.currentTestIndex = 0; |
| 469 | |
| 470 | document.getElementById('run-all-tests').disabled = true; |
| 471 | document.getElementById('stop-tests').disabled = false; |
| 472 | document.getElementById('test-results').innerHTML = ''; |
| 473 | |
| 474 | const autoScroll = document.getElementById('auto-scroll').checked; |
| 475 | |
| 476 | for (let i = 0; i < this.tests.length; i++) { |
| 477 | if (!this.isRunning) break; |
| 478 | |
| 479 | this.currentTestIndex = i; |
| 480 | const test = this.tests[i]; |
| 481 | |
| 482 | this.updateProgress(`Running: ${test.name} (${i + 1}/${this.tests.length})`); |
| 483 | |
| 484 | try { |
| 485 | const result = await test.run(); |
| 486 | result.testName = test.name; |
| 487 | result.testDescription = test.description; |
| 488 | this.results.push(result); |
| 489 | |
| 490 | this.displayResult(result); |
| 491 | |
| 492 | // Auto-scroll to element if enabled |
| 493 | if (autoScroll && result.element) { |
| 494 | result.element.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 495 | this.highlightElement(result.element); |
| 496 | } |
| 497 | |
| 498 | await this.wait(500); |
| 499 | } catch (error) { |
| 500 | this.results.push({ |
| 501 | testName: test.name, |
| 502 | pass: false, |
| 503 | message: `Error: ${error.message}`, |
| 504 | error: error |
| 505 | }); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | this.isRunning = false; |
| 510 | document.getElementById('run-all-tests').disabled = false; |
| 511 | document.getElementById('stop-tests').disabled = true; |
| 512 | |
| 513 | this.showSummary(); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Stop running tests |
| 518 | */ |
| 519 | stopTests() { |
| 520 | this.isRunning = false; |
| 521 | this.updateProgress('Tests stopped'); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Update progress display |
| 526 | */ |
| 527 | updateProgress(message) { |
| 528 | document.getElementById('test-progress').textContent = message; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Display individual test result |
| 533 | */ |
| 534 | displayResult(result) { |
| 535 | const resultEl = document.createElement('div'); |
| 536 | resultEl.style.cssText = ` |
| 537 | padding: 12px; |
| 538 | margin-bottom: 8px; |
| 539 | background: ${result.pass ? 'rgba(52, 199, 89, 0.1)' : 'rgba(255, 59, 48, 0.1)'}; |
| 540 | border-left: 3px solid ${result.pass ? '#34c759' : '#ff3b30'}; |
| 541 | border-radius: 8px; |
| 542 | font-size: 12px; |
| 543 | `; |
| 544 | |
| 545 | resultEl.innerHTML = ` |
| 546 | <div style="font-weight: 700; margin-bottom: 4px;"> |
| 547 | ${result.pass ? '✓' : '✗'} ${result.testName} |
| 548 | </div> |
| 549 | <div style="color: #98989d; margin-bottom: 4px; font-size: 11px;"> |
| 550 | ${result.testDescription} |
| 551 | </div> |
| 552 | <div style="color: ${result.pass ? '#34c759' : '#ff3b30'};"> |
| 553 | ${result.message} |
| 554 | </div> |
| 555 | `; |
| 556 | |
| 557 | document.getElementById('test-results').prepend(resultEl); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Show test summary |
| 562 | */ |
| 563 | showSummary() { |
| 564 | const passed = this.results.filter(r => r.pass).length; |
| 565 | const failed = this.results.filter(r => !r.pass).length; |
| 566 | const total = this.results.length; |
| 567 | |
| 568 | const summary = document.createElement('div'); |
| 569 | summary.style.cssText = ` |
| 570 | padding: 16px; |
| 571 | margin-bottom: 16px; |
| 572 | background: #000; |
| 573 | border-radius: 8px; |
| 574 | text-align: center; |
| 575 | `; |
| 576 | |
| 577 | summary.innerHTML = ` |
| 578 | <div style="font-size: 24px; font-weight: 900; margin-bottom: 8px;"> |
| 579 | ${passed}/${total} Passed |
| 580 | </div> |
| 581 | <div style="font-size: 14px; color: #98989d;"> |
| 582 | <span style="color: #34c759;">${passed} passed</span> • |
| 583 | <span style="color: #ff3b30;">${failed} failed</span> |
| 584 | </div> |
| 585 | `; |
| 586 | |
| 587 | document.getElementById('test-results').prepend(summary); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Highlight an element temporarily |
| 592 | */ |
| 593 | highlightElement(element) { |
| 594 | const originalOutline = element.style.outline; |
| 595 | const originalOutlineOffset = element.style.outlineOffset; |
| 596 | |
| 597 | element.style.outline = '3px solid #ff3b30'; |
| 598 | element.style.outlineOffset = '4px'; |
| 599 | |
| 600 | setTimeout(() => { |
| 601 | element.style.outline = originalOutline; |
| 602 | element.style.outlineOffset = originalOutlineOffset; |
| 603 | }, 1500); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Get computed styles for an element |
| 608 | */ |
| 609 | getComputedStyles(element) { |
| 610 | return window.getComputedStyle(element); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Check color contrast ratio |
| 615 | */ |
| 616 | checkContrast(color1, color2) { |
| 617 | const rgb1 = this.parseColor(color1); |
| 618 | const rgb2 = this.parseColor(color2); |
| 619 | |
| 620 | const l1 = this.getLuminance(rgb1); |
| 621 | const l2 = this.getLuminance(rgb2); |
| 622 | |
| 623 | const ratio = l1 > l2 |
| 624 | ? (l1 + 0.05) / (l2 + 0.05) |
| 625 | : (l2 + 0.05) / (l1 + 0.05); |
| 626 | |
| 627 | return { |
| 628 | ratio: ratio, |
| 629 | wcagAA: ratio >= 4.5, |
| 630 | wcagAAA: ratio >= 7 |
| 631 | }; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Parse CSS color to RGB(A) |
| 636 | */ |
| 637 | parseColor(color) { |
| 638 | const div = document.createElement('div'); |
| 639 | div.style.color = color; |
| 640 | document.body.appendChild(div); |
| 641 | const computed = window.getComputedStyle(div).color; |
| 642 | document.body.removeChild(div); |
| 643 | |
| 644 | const match = computed.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/); |
| 645 | if (!match) return { r: 0, g: 0, b: 0, a: 1 }; |
| 646 | |
| 647 | return { |
| 648 | r: parseInt(match[1]), |
| 649 | g: parseInt(match[2]), |
| 650 | b: parseInt(match[3]), |
| 651 | a: match[4] ? parseFloat(match[4]) : 1 |
| 652 | }; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Flatten semi-transparent color over a background |
| 657 | */ |
| 658 | flattenColor(foreground, background) { |
| 659 | const fg = this.parseColor(foreground); |
| 660 | const bg = this.parseColor(background); |
| 661 | |
| 662 | // If foreground is fully opaque, return it as-is |
| 663 | if (fg.a === 1) { |
| 664 | return `rgb(${fg.r}, ${fg.g}, ${fg.b})`; |
| 665 | } |
| 666 | |
| 667 | // Alpha blend: result = fg * alpha + bg * (1 - alpha) |
| 668 | const r = Math.round(fg.r * fg.a + bg.r * (1 - fg.a)); |
| 669 | const g = Math.round(fg.g * fg.a + bg.g * (1 - fg.a)); |
| 670 | const b = Math.round(fg.b * fg.a + bg.b * (1 - fg.a)); |
| 671 | |
| 672 | return `rgb(${r}, ${g}, ${b})`; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Calculate relative luminance |
| 677 | */ |
| 678 | getLuminance(rgb) { |
| 679 | const rsRGB = rgb.r / 255; |
| 680 | const gsRGB = rgb.g / 255; |
| 681 | const bsRGB = rgb.b / 255; |
| 682 | |
| 683 | const r = rsRGB <= 0.03928 ? rsRGB / 12.92 : Math.pow((rsRGB + 0.055) / 1.055, 2.4); |
| 684 | const g = gsRGB <= 0.03928 ? gsRGB / 12.92 : Math.pow((gsRGB + 0.055) / 1.055, 2.4); |
| 685 | const b = bsRGB <= 0.03928 ? bsRGB / 12.92 : Math.pow((bsRGB + 0.055) / 1.055, 2.4); |
| 686 | |
| 687 | return 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Check if a color is greyish |
| 692 | */ |
| 693 | isGreyish(color) { |
| 694 | const rgb = this.parseColor(color); |
| 695 | const diff = Math.max(rgb.r, rgb.g, rgb.b) - Math.min(rgb.r, rgb.g, rgb.b); |
| 696 | return diff < 30; // Low saturation = greyish |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Wait utility |
| 701 | */ |
| 702 | wait(ms) { |
| 703 | return new Promise(resolve => setTimeout(resolve, ms)); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // Initialize on page load |
| 708 | if (typeof window !== 'undefined') { |
| 709 | window.addEventListener('DOMContentLoaded', () => { |
| 710 | window.visualTestSuite = new VisualTestSuite(); |
| 711 | window.visualTestSuite.init(); |
| 712 | }); |
| 713 | } |
| 714 | |
| 715 | // Export |
| 716 | if (typeof module !== 'undefined' && module.exports) { |
| 717 | module.exports = VisualTestSuite; |
| 718 | } |