forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessibility-actions.js
More file actions
164 lines (136 loc) · 6.25 KB
/
Copy pathaccessibility-actions.js
File metadata and controls
164 lines (136 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* accessibility-actions.js
*
* Playwright + @axe-core/playwright accessibility scanner.
* Navigates every major page route and scans for WCAG 2.1 AA violations.
* Exits non-zero if critical violations are found (blocks CI).
*
* Usage:
* node scripts/accessibility-actions.js [--base-url http://localhost:3000]
*
* Output:
* accessibility-report.json — full violation details
* Console summary with pass/fail per route
*/
import { chromium } from 'playwright';
import AxeBuilder from '@axe-core/playwright';
import { writeFile, mkdir } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// ── Configuration ─────────────────────────────────────────────────────────────
const BASE_URL = (() => {
const idx = process.argv.indexOf('--base-url');
return idx !== -1
? process.argv[idx + 1]
: (process.env.PLAYWRIGHT_BASE_URL ?? 'http://localhost:3000');
})();
/** Pages to scan. Dynamic segments use a representative fixture ID. */
const ROUTES = [
{ name: 'Landing', path: '/' },
{ name: 'Dashboard', path: '/dashboard' },
{ name: 'Explorer', path: '/explorer' },
{ name: 'Escrow Details', path: '/escrow/demo-escrow-id' },
{ name: 'Profile', path: '/profile/GDEMO000000000000000000000000000000000000000000000000000' },
];
/**
* Violation impact levels that will fail the CI run.
* "moderate" and above are blocked; "minor" / "cosmetic" are warnings only.
*/
const BLOCKING_IMPACTS = new Set(['critical', 'serious']);
// ── Helpers ───────────────────────────────────────────────────────────────────
function countByImpact(violations) {
return violations.reduce((acc, v) => {
acc[v.impact] = (acc[v.impact] ?? 0) + 1;
return acc;
}, {});
}
function formatViolation(v) {
const nodes = v.nodes
.slice(0, 3)
.map((n) => ` • ${n.html}`)
.join('\n');
return ` [${v.impact?.toUpperCase()}] ${v.id}: ${v.description}\n${nodes}`;
}
// ── Main ──────────────────────────────────────────────────────────────────────
async function run() {
const browser = await chromium.launch();
const context = await browser.newContext();
const report = {
generated_at: new Date().toISOString(),
base_url: BASE_URL,
summary: { total_violations: 0, blocking: 0, warnings: 0, pages_scanned: 0 },
pages: [],
};
let hasBlockingViolations = false;
for (const route of ROUTES) {
const url = `${BASE_URL}${route.path}`;
const page = await context.newPage();
try {
await page.goto(url, { waitUntil: 'networkidle', timeout: 30_000 });
} catch {
console.warn(`⚠ Could not load ${url} — skipping`);
await page.close();
continue;
}
const results = await new AxeBuilder({ page })
.withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
.analyze();
const { violations } = results;
const blocking = violations.filter((v) => BLOCKING_IMPACTS.has(v.impact));
const warnings = violations.filter((v) => !BLOCKING_IMPACTS.has(v.impact));
const counts = countByImpact(violations);
report.pages.push({
name: route.name,
url,
violation_count: violations.length,
by_impact: counts,
violations: violations.map((v) => ({
id: v.id,
impact: v.impact,
description: v.description,
help_url: v.helpUrl,
nodes: v.nodes.map((n) => ({ html: n.html, target: n.target })),
})),
});
report.summary.total_violations += violations.length;
report.summary.blocking += blocking.length;
report.summary.warnings += warnings.length;
report.summary.pages_scanned += 1;
const status = blocking.length > 0 ? '❌' : violations.length > 0 ? '⚠ ' : '✅';
console.log(`${status} ${route.name} (${url})`);
if (violations.length > 0) {
console.log(` ${violations.length} violation(s): ${JSON.stringify(counts)}`);
violations.forEach((v) => console.log(formatViolation(v)));
}
if (blocking.length > 0) hasBlockingViolations = true;
await page.close();
}
await browser.close();
// ── Write report ────────────────────────────────────────────────────────────
const reportDir = path.join(__dirname, '..', 'accessibility-reports');
await mkdir(reportDir, { recursive: true });
const reportPath = path.join(reportDir, 'accessibility-report.json');
await writeFile(reportPath, JSON.stringify(report, null, 2));
// ── Print summary ────────────────────────────────────────────────────────────
console.log('\n─────────────────────────────────────────');
console.log('Accessibility Scan Summary');
console.log('─────────────────────────────────────────');
console.log(`Pages scanned : ${report.summary.pages_scanned}`);
console.log(`Total violations: ${report.summary.total_violations}`);
console.log(` Blocking (critical/serious): ${report.summary.blocking}`);
console.log(` Warnings (moderate/minor) : ${report.summary.warnings}`);
console.log(`Report written to: ${reportPath}`);
console.log('─────────────────────────────────────────\n');
if (hasBlockingViolations) {
console.error(
'❌ Blocking accessibility violations found. Fix critical/serious issues before merging.',
);
process.exit(1);
}
console.log('✅ No blocking accessibility violations.');
}
run().catch((err) => {
console.error('Fatal error during accessibility scan:', err);
process.exit(1);
});