|
| 1 | +import type { |
| 2 | + Reporter, |
| 3 | + FullConfig, |
| 4 | + Suite, |
| 5 | + TestCase, |
| 6 | + TestResult, |
| 7 | + FullResult |
| 8 | +} from '@playwright/test/reporter' |
| 9 | +import { AxeResults } from 'axe-core' |
| 10 | +import * as fs from 'fs/promises' |
| 11 | +import * as path from 'path' |
| 12 | + |
| 13 | +interface A11yTestResult { |
| 14 | + test: string |
| 15 | + file: string |
| 16 | + line: number |
| 17 | + status: string |
| 18 | + duration: number |
| 19 | + url?: string |
| 20 | + violations: AxeResults['violations'] |
| 21 | + violationCount: number |
| 22 | + passCount: number |
| 23 | + incompleteCount: number |
| 24 | +} |
| 25 | + |
| 26 | +interface A11yReport { |
| 27 | + summary: { |
| 28 | + totalTests: number |
| 29 | + totalViolations: number |
| 30 | + totalPasses: number |
| 31 | + totalIncomplete: number |
| 32 | + timestamp: string |
| 33 | + duration: number |
| 34 | + } |
| 35 | + tests: A11yTestResult[] |
| 36 | +} |
| 37 | + |
| 38 | +class A11yReporter implements Reporter { |
| 39 | + private results: A11yTestResult[] = [] |
| 40 | + private outputFile: string |
| 41 | + private startTime: number = 0 |
| 42 | + |
| 43 | + constructor(options: { outputFile?: string } = {}) { |
| 44 | + this.outputFile = options.outputFile || 'a11y-report.json' |
| 45 | + } |
| 46 | + |
| 47 | + onBegin(_config: FullConfig, _suite: Suite): void { |
| 48 | + this.startTime = Date.now() |
| 49 | + } |
| 50 | + |
| 51 | + onTestEnd(test: TestCase, result: TestResult): void { |
| 52 | + result.attachments.forEach((attachment) => { |
| 53 | + if (attachment.name !== 'accessibility-scan') { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + let axeResults: AxeResults |
| 59 | + |
| 60 | + if (attachment.body) { |
| 61 | + axeResults = JSON.parse(attachment.body.toString('utf-8')) |
| 62 | + } else if (attachment.path) { |
| 63 | + const content = require('fs').readFileSync(attachment.path, 'utf-8') |
| 64 | + axeResults = JSON.parse(content) |
| 65 | + } else { |
| 66 | + throw new Error('No accessibility scan attachment found') |
| 67 | + } |
| 68 | + |
| 69 | + this.results.push({ |
| 70 | + test: test.title, |
| 71 | + file: path.relative(process.cwd(), test.location.file), |
| 72 | + line: test.location.line, |
| 73 | + status: result.status, |
| 74 | + duration: result.duration, |
| 75 | + url: axeResults.url, |
| 76 | + violations: axeResults.violations || [], |
| 77 | + violationCount: axeResults.violations?.length || 0, |
| 78 | + passCount: axeResults.passes?.length || 0, |
| 79 | + incompleteCount: axeResults.incomplete?.length || 0 |
| 80 | + }) |
| 81 | + } catch (error) { |
| 82 | + console.error(`Error parsing accessibility results for test "${test.title}":`, error) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + async onEnd(_result: FullResult): Promise<void> { |
| 88 | + const duration = Date.now() - this.startTime |
| 89 | + const totalViolations = this.results.reduce((sum, test) => sum + test.violationCount, 0) |
| 90 | + |
| 91 | + const report: A11yReport = { |
| 92 | + summary: { |
| 93 | + totalTests: this.results.length, |
| 94 | + totalViolations, |
| 95 | + totalPasses: this.results.reduce((sum, test) => sum + test.passCount, 0), |
| 96 | + totalIncomplete: this.results.reduce((sum, test) => sum + test.incompleteCount, 0), |
| 97 | + timestamp: new Date().toISOString(), |
| 98 | + duration |
| 99 | + }, |
| 100 | + tests: this.results |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + const outputDir = path.dirname(this.outputFile) |
| 105 | + await fs.mkdir(outputDir, { recursive: true }) |
| 106 | + |
| 107 | + await fs.writeFile(this.outputFile, JSON.stringify(report, null, 2), 'utf-8') |
| 108 | + |
| 109 | + console.info(`\n📊 Accessibility Report Generated:`) |
| 110 | + console.info(` File: ${this.outputFile}`) |
| 111 | + console.info(` Tests: ${report.summary.totalTests}`) |
| 112 | + console.warn(` Violations: ${report.summary.totalViolations}`) |
| 113 | + |
| 114 | + if (totalViolations > 0) { |
| 115 | + console.warn(`\n⚠️ Found ${totalViolations} accessibility violations`) |
| 116 | + } else { |
| 117 | + console.info(`\n✅ No accessibility violations found`) |
| 118 | + } |
| 119 | + } catch (error) { |
| 120 | + console.error('Error writing accessibility report:', error) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export default A11yReporter |
0 commit comments