-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathscan-for-accessibility-issues.ts
More file actions
69 lines (58 loc) · 2.15 KB
/
scan-for-accessibility-issues.ts
File metadata and controls
69 lines (58 loc) · 2.15 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { source as axeCoreSource } from 'axe-core';
import { AppController } from 'tests/electron/common/view-controllers/app-controller';
import { SpectronAsyncClient } from 'tests/electron/common/view-controllers/spectron-async-client';
import {
prettyPrintAxeViolations,
PrintableAxeResult,
} from 'tests/end-to-end/common/pretty-print-axe-violations';
declare let axe;
export async function scanForAccessibilityIssuesInAllModes(app: AppController): Promise<void> {
await scanForAccessibilityIssues(app, true);
await scanForAccessibilityIssues(app, false);
}
async function scanForAccessibilityIssues(
app: AppController,
enableHighContrast: boolean,
): Promise<void> {
await app.setHighContrastMode(enableHighContrast);
await app.waitForHighContrastMode(enableHighContrast);
const violations = await runAxeScan(app.client);
expect(violations).toStrictEqual([]);
}
async function runAxeScan(
spectronClient: SpectronAsyncClient,
selector?: string,
): Promise<PrintableAxeResult[]> {
await injectAxeIfUndefined(spectronClient);
const axeRunOptions = {
runOnly: {
type: 'tag',
values: ['wcag2a', 'wcag21a', 'wcag2aa', 'wcag21aa'],
},
};
const axeResults = await spectronClient.executeAsync(
(options, selectorInEvaluate, done) => {
const elementContext =
selectorInEvaluate === null ? document : { include: [selectorInEvaluate] };
axe.run(elementContext, options, function (err: Error, results: any): void {
if (err) {
throw err;
}
done(results);
});
},
axeRunOptions,
selector,
);
return prettyPrintAxeViolations(axeResults);
}
async function injectAxeIfUndefined(spectronClient: SpectronAsyncClient): Promise<void> {
const axeIsUndefined = await spectronClient.execute(() => {
return (window as any).axe === undefined;
});
if (axeIsUndefined) {
await spectronClient.execute(axeCoreSource);
}
}