|
| 1 | +import AxeBuilder from '@axe-core/playwright'; |
| 2 | +import { expect, test, type Locator, type Page } from '@playwright/test'; |
| 3 | +import { gotoComponent, openFromDemo, previewByTitle, scrollDemoIntoView } from '../visual/helpers'; |
| 4 | + |
| 5 | +const WCAG_TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']; |
| 6 | + |
| 7 | +let scanId = 0; |
| 8 | + |
| 9 | +const formatViolations = (violations: Awaited<ReturnType<AxeBuilder['analyze']>>['violations']) => |
| 10 | + violations.map(({ id, impact, description, nodes }) => ({ |
| 11 | + id, |
| 12 | + impact, |
| 13 | + description, |
| 14 | + targets: nodes.slice(0, 5).map((node) => node.target.join(' ')), |
| 15 | + })); |
| 16 | + |
| 17 | +const markLocator = async (locator: Locator) => { |
| 18 | + const marker = `a11y-scan-${++scanId}`; |
| 19 | + await locator.evaluate((node, value) => { |
| 20 | + node.setAttribute('data-a11y-scan', value); |
| 21 | + }, marker); |
| 22 | + |
| 23 | + return `[data-a11y-scan="${marker}"]`; |
| 24 | +}; |
| 25 | + |
| 26 | +const scan = async (page: Page, target: Locator | string) => { |
| 27 | + const selector = typeof target === 'string' ? target : await markLocator(target); |
| 28 | + const results = await new AxeBuilder({ page }) |
| 29 | + .withTags(WCAG_TAGS) |
| 30 | + .disableRules(['region']) |
| 31 | + .include(selector) |
| 32 | + .analyze(); |
| 33 | + |
| 34 | + expect(formatViolations(results.violations)).toEqual([]); |
| 35 | +}; |
| 36 | + |
| 37 | +test.describe('component accessibility checks', () => { |
| 38 | + test('form controls and table demos have no WCAG violations', async ({ page }) => { |
| 39 | + await gotoComponent(page, 'form'); |
| 40 | + await scrollDemoIntoView(page, 'Basic usage'); |
| 41 | + await scan(page, previewByTitle(page, 'Basic usage')); |
| 42 | + |
| 43 | + await gotoComponent(page, 'table'); |
| 44 | + await scrollDemoIntoView(page, 'Basic'); |
| 45 | + await scan(page, previewByTitle(page, 'Basic')); |
| 46 | + |
| 47 | + await scrollDemoIntoView(page, 'Row Selection'); |
| 48 | + await scan(page, previewByTitle(page, 'Row Selection')); |
| 49 | + }); |
| 50 | + |
| 51 | + test('overlay components have no WCAG violations when open', async ({ page }) => { |
| 52 | + await gotoComponent(page, 'modal'); |
| 53 | + await openFromDemo(page, 'Basic', 'button'); |
| 54 | + await expect(page.locator('.ty-modal__content')).toBeVisible(); |
| 55 | + await scan(page, '.ty-modal__content'); |
| 56 | + |
| 57 | + await gotoComponent(page, 'drawer'); |
| 58 | + await openFromDemo(page, 'Basic', 'button'); |
| 59 | + await expect(page.locator('.ty-drawer__content')).toBeVisible(); |
| 60 | + await scan(page, '.ty-drawer__content'); |
| 61 | + |
| 62 | + await gotoComponent(page, 'tour'); |
| 63 | + await openFromDemo(page, 'Basic', 'button:has-text("Start Tour")'); |
| 64 | + await expect(page.locator('.ty-tour')).toBeVisible(); |
| 65 | + await scan(page, '.ty-tour'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('select and picker popups have no WCAG violations when open', async ({ page }) => { |
| 69 | + await gotoComponent(page, 'select'); |
| 70 | + await openFromDemo(page, 'Search', '.ty-select__selector'); |
| 71 | + await expect(page.locator('.ty-select__dropdown')).toBeVisible(); |
| 72 | + await scan(page, '.ty-select__dropdown'); |
| 73 | + |
| 74 | + await gotoComponent(page, 'date-picker'); |
| 75 | + await openFromDemo(page, 'Date Range', '.ty-date-picker__input'); |
| 76 | + await expect(page.locator('.ty-date-picker__dropdown')).toBeVisible(); |
| 77 | + await scan(page, '.ty-date-picker__dropdown'); |
| 78 | + |
| 79 | + await gotoComponent(page, 'cascader'); |
| 80 | + await openFromDemo(page, 'Change On Select', '.ty-cascader__selector'); |
| 81 | + await expect(page.locator('.ty-cascader__dropdown')).toBeVisible(); |
| 82 | + await scan(page, '.ty-cascader__dropdown'); |
| 83 | + }); |
| 84 | +}); |
0 commit comments