forked from pa11y/pa11y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (51 loc) · 1.34 KB
/
index.js
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
// An example of running Pa11y programmatically, reusing
// existing Puppeteer browsers and pages
'use strict';
const pa11y = require('../..');
const puppeteer = require('puppeteer');
runExample();
// Async function required for us to use await
async function runExample() {
let browser;
let pages;
try {
// Launch our own browser
browser = await puppeteer.launch();
// Create a page for the test runs
// (Pages cannot be used in multiple runs)
pages = [
await browser.newPage(),
await browser.newPage()
];
// Test http://example.com/ with our shared browser
const result1 = await pa11y('http://example.com/', {
browser: browser,
page: pages[0]
});
// Test http://example.com/otherpage/ with our shared browser
const result2 = await pa11y('http://example.com/otherpage/', {
browser: browser,
page: pages[1]
});
// Output the raw result objects
console.log(result1);
console.log(result2);
// Close the browser instance and pages now we're done with it
for (const page of pages) {
await page.close();
}
await browser.close();
} catch (error) {
// Output an error if it occurred
console.error(error.message);
// Close the browser instance and pages if theys exist
if (pages) {
for (const page of pages) {
await page.close();
}
}
if (browser) {
await browser.close();
}
}
}