-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathaxe-scan.js
More file actions
90 lines (75 loc) · 2.77 KB
/
axe-scan.js
File metadata and controls
90 lines (75 loc) · 2.77 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
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const AxeBuilder = require('@axe-core/webdriverjs');
const fs = require('fs');
// Get URLs from command line args
const urls = process.argv.slice(2);
console.log(`\nStarting Axe scan on ${urls.length} pages...\n`);
(async function scan() {
const options = new chrome.Options();
options.addArguments('--headless=new');
options.addArguments('--no-sandbox');
options.addArguments('--disable-dev-shm-usage');
options.addArguments('--disable-gpu');
// see if these help with the production/shifts page crashing
options.addArguments('--window-size=1920,1080');
options.addArguments('--disable-extensions');
options.addArguments('--disable-infobars');
options.addArguments('--disable-software-rasterizer');
// Initialize Driver
const driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
const fullReport = [];
let hasErrors = false;
try {
// Set a very generous timeout for page loads (3 minutes)
await driver.manage().setTimeouts({ pageLoad: 180000, script: 180000 });
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
console.log(`[${i + 1}/${urls.length}] Testing ${url} ...`);
try {
await driver.get(url);
const results = await new AxeBuilder(driver)
.withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
.analyze();
if (results.violations.length > 0) {
console.log(` FAILED: ${results.violations.length} violations found.`);
results.violations.forEach((violation) => {
console.log(` [${violation.impact.toUpperCase()}] ${violation.id}: ${violation.help}`);
console.log(` Help URL: ${violation.helpUrl}`);
violation.nodes.forEach((node) => {
console.log(` - Selector: ${node.target.join(' ')}`);
});
console.log(''); // Empty line for readability
});
fullReport.push({
url: url,
violations: results.violations
});
hasErrors = true;
} else {
console.log(` PASSED`);
}
} catch (e) {
console.error(` CRASHED: Could not scan ${url}. Skipping.`);
console.error(` Reason: ${e.message}`);
fullReport.push({
url: url,
error: "Browser crashed or timed out on this page",
details: e.message
});
hasErrors = true;
}
}
// Save JSON report
fs.writeFileSync('axe-report.json', JSON.stringify(fullReport, null, 2));
console.log('\nReport saved to axe-report.json');
} finally {
await driver.quit();
}
if (hasErrors) {
process.exit(1);
}
})();