-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrun-axe.ts
More file actions
156 lines (135 loc) · 4.02 KB
/
run-axe.ts
File metadata and controls
156 lines (135 loc) · 4.02 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
import { AxeBuilder } from '@axe-core/playwright';
import ansis from 'ansis';
import type { AxeResults } from 'axe-core';
import { createRequire } from 'node:module';
import path from 'node:path';
import { type Browser, type Page, chromium } from 'playwright-core';
import type { AuditOutputs } from '@code-pushup/models';
import {
executeProcess,
formatAsciiTable,
indentLines,
logger,
pluralizeToken,
} from '@code-pushup/utils';
import { toAuditOutputs } from './transform.js';
/* eslint-disable functional/no-let */
let browser: Browser | undefined;
let browserChecked = false;
/* eslint-enable functional/no-let */
export type AxeUrlArgs = {
url: string;
urlIndex: number;
urlsCount: number;
ruleIds: string[];
timeout: number;
};
export type AxeUrlResult = {
url: string;
axeResults: AxeResults;
auditOutputs: AuditOutputs;
};
export async function runAxeForUrl(args: AxeUrlArgs): Promise<AxeUrlResult> {
const { url, urlIndex, urlsCount } = args;
if (!browser) {
await ensureBrowserInstalled();
browser = await logger.task('Launching Chromium browser', async () => ({
message: 'Launched Chromium browser',
result: await chromium.launch({ headless: true }),
}));
}
const prefix = ansis.gray(`[${urlIndex + 1}/${urlsCount}]`);
return await logger.task(`${prefix} Analyzing URL ${url}`, async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const context = await browser!.newContext();
try {
const page = await context.newPage();
try {
const axeResults = await runAxeForPage(page, args);
const auditOutputs = toAuditOutputs(axeResults, url);
return {
message: `${prefix} Analyzed URL ${url}`,
result: { url, axeResults, auditOutputs },
};
} finally {
await page.close();
}
} finally {
await context.close();
}
});
}
async function runAxeForPage(
page: Page,
{ url, ruleIds, timeout }: AxeUrlArgs,
): Promise<AxeResults> {
await page.goto(url, {
waitUntil: 'networkidle',
timeout,
});
const axeBuilder = new AxeBuilder({ page });
// Use withRules() to include experimental/deprecated rules
if (ruleIds.length > 0) {
axeBuilder.withRules(ruleIds);
}
const results = await axeBuilder.analyze();
logger.debug(
formatAsciiTable({
columns: ['left', 'right'],
rows: [
['Passes', results.passes.length],
['Violations', results.violations.length],
['Incomplete', results.incomplete.length],
['Inapplicable', results.inapplicable.length],
],
}),
);
if (results.incomplete.length > 0) {
logger.warn(
`Axe returned ${pluralizeToken('incomplete result', results.incomplete.length)}`,
);
logger.debug(
results.incomplete
.flatMap(res => [
`• ${res.id}`,
indentLines(
res.nodes
.flatMap(node => [...node.all, ...node.any])
.map(check => `- ${check.message}`)
.join('\n'),
2,
),
])
.join('\n'),
);
}
return results;
}
export async function closeBrowser(): Promise<void> {
if (browser) {
await browser.close();
browser = undefined;
logger.debug('Closed Chromium browser');
}
}
/**
* Ensures Chromium browser binary is installed before running accessibility audits.
*
* Uses Node's module resolution and npm's bin specification to locate playwright-core CLI,
* working reliably with all package managers (npm, pnpm, yarn).
*/
async function ensureBrowserInstalled(): Promise<void> {
if (browserChecked) {
return;
}
logger.debug('Checking Chromium browser installation ...');
const require = createRequire(import.meta.url);
const pkgPath = require.resolve('playwright-core/package.json');
const pkg = require(pkgPath);
const cliPath = path.join(path.dirname(pkgPath), pkg.bin['playwright-core']);
await executeProcess({
command: 'node',
args: [cliPath, 'install', 'chromium'],
});
browserChecked = true;
}