|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import AxeBuilder from '@axe-core/playwright'; |
| 3 | + |
| 4 | +test.describe('Accessibility', () => { |
| 5 | + test('homepage should have no WCAG AA violations', async ({ page }) => { |
| 6 | + await page.goto('./'); |
| 7 | + |
| 8 | + await page.waitForSelector('.skills-grid a'); |
| 9 | + |
| 10 | + const accessibilityScanResults = await new AxeBuilder({ page }) |
| 11 | + .withTags(['wcag2a', 'wcag2aa']) |
| 12 | + .analyze(); |
| 13 | + |
| 14 | + if (accessibilityScanResults.violations.length > 0) { |
| 15 | + console.log('Accessibility violations:', JSON.stringify(accessibilityScanResults.violations, null, 2)); |
| 16 | + } |
| 17 | + |
| 18 | + expect(accessibilityScanResults.violations).toEqual([]); |
| 19 | + }); |
| 20 | + |
| 21 | + test('skip-to-content link works', async ({ page }) => { |
| 22 | + await page.goto('./'); |
| 23 | + |
| 24 | + await page.keyboard.press('Tab'); |
| 25 | + |
| 26 | + const skipLink = page.locator('a[href="#main-content"], .skip-link'); |
| 27 | + |
| 28 | + if (await skipLink.isVisible()) { |
| 29 | + await skipLink.click(); |
| 30 | + |
| 31 | + const mainContent = page.locator('#main-content, main'); |
| 32 | + await expect(mainContent).toBeVisible(); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + test('all images have alt text', async ({ page }) => { |
| 37 | + await page.goto('./'); |
| 38 | + |
| 39 | + const images = page.locator('img'); |
| 40 | + const imageCount = await images.count(); |
| 41 | + |
| 42 | + for (let i = 0; i < imageCount; i++) { |
| 43 | + const img = images.nth(i); |
| 44 | + const alt = await img.getAttribute('alt'); |
| 45 | + const role = await img.getAttribute('role'); |
| 46 | + |
| 47 | + const hasAccessibleLabel = alt !== null || role === 'presentation' || role === 'none'; |
| 48 | + expect(hasAccessibleLabel).toBeTruthy(); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + test('interactive elements are keyboard accessible', async ({ page }) => { |
| 53 | + await page.goto('./'); |
| 54 | + |
| 55 | + for (let i = 0; i < 10; i++) { |
| 56 | + await page.keyboard.press('Tab'); |
| 57 | + |
| 58 | + const focusedElement = page.locator(':focus'); |
| 59 | + const isVisible = await focusedElement.isVisible().catch(() => false); |
| 60 | + |
| 61 | + if (isVisible) { |
| 62 | + const tagName = await focusedElement.evaluate(el => el.tagName.toLowerCase()); |
| 63 | + const role = await focusedElement.getAttribute('role'); |
| 64 | + const tabIndex = await focusedElement.getAttribute('tabindex'); |
| 65 | + |
| 66 | + const isInteractive = ['a', 'button', 'input', 'select', 'textarea'].includes(tagName) || |
| 67 | + ['button', 'link', 'tab', 'menuitem'].includes(role || '') || |
| 68 | + tabIndex === '0'; |
| 69 | + |
| 70 | + expect(isInteractive).toBeTruthy(); |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test('color contrast meets WCAG AA standards', async ({ page }) => { |
| 76 | + await page.goto('./'); |
| 77 | + |
| 78 | + const accessibilityScanResults = await new AxeBuilder({ page }) |
| 79 | + .withTags(['wcag2aa']) |
| 80 | + .options({ rules: { 'color-contrast': { enabled: true } } }) |
| 81 | + .analyze(); |
| 82 | + |
| 83 | + const contrastViolations = accessibilityScanResults.violations.filter( |
| 84 | + v => v.id === 'color-contrast' |
| 85 | + ); |
| 86 | + |
| 87 | + expect(contrastViolations).toEqual([]); |
| 88 | + }); |
| 89 | + |
| 90 | + test('focus indicators are visible', async ({ page }) => { |
| 91 | + await page.goto('./'); |
| 92 | + |
| 93 | + await page.keyboard.press('Tab'); |
| 94 | + |
| 95 | + const focusedElement = page.locator(':focus'); |
| 96 | + |
| 97 | + if (await focusedElement.isVisible()) { |
| 98 | + const outline = await focusedElement.evaluate(el => { |
| 99 | + const styles = window.getComputedStyle(el); |
| 100 | + return { |
| 101 | + outlineStyle: styles.outlineStyle, |
| 102 | + outlineWidth: styles.outlineWidth, |
| 103 | + boxShadow: styles.boxShadow, |
| 104 | + }; |
| 105 | + }); |
| 106 | + |
| 107 | + const hasFocusIndicator = |
| 108 | + (outline.outlineStyle !== 'none' && outline.outlineWidth !== '0px') || |
| 109 | + outline.boxShadow !== 'none'; |
| 110 | + |
| 111 | + expect(hasFocusIndicator).toBeTruthy(); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + test('command palette is keyboard navigable', async ({ page }) => { |
| 116 | + await page.goto('./'); |
| 117 | + await page.waitForLoadState('networkidle'); |
| 118 | + |
| 119 | + await page.keyboard.press('Control+k'); |
| 120 | + |
| 121 | + const searchInput = page.locator('[cmdk-input]'); |
| 122 | + await searchInput.waitFor({ state: 'visible', timeout: 10000 }); |
| 123 | + await expect(searchInput).toBeVisible(); |
| 124 | + |
| 125 | + await page.keyboard.type('azure'); |
| 126 | + await page.waitForTimeout(200); |
| 127 | + |
| 128 | + await page.keyboard.press('ArrowDown'); |
| 129 | + |
| 130 | + const results = page.locator('[cmdk-item]'); |
| 131 | + if (await results.count() > 0) { |
| 132 | + const selectedItem = page.locator('[cmdk-item][aria-selected="true"]'); |
| 133 | + const count = await selectedItem.count(); |
| 134 | + expect(count).toBeGreaterThanOrEqual(0); |
| 135 | + } |
| 136 | + |
| 137 | + await page.keyboard.press('Escape'); |
| 138 | + await expect(searchInput).not.toBeVisible(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments