generated from hmcts/expressjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patha11y.ts
123 lines (102 loc) · 3.24 KB
/
a11y.ts
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
import fs from 'fs';
import Axios from 'axios';
import puppeteer from 'puppeteer';
import * as urls from '../../main/steps/urls';
import { config } from '../config';
const IGNORED_URLS = [
urls.SIGN_IN_URL,
urls.SIGN_OUT_URL,
urls.CALLBACK_URL,
urls.FIND_OUT_ABOUT_CAFCASS,
urls.FIND_OUT_ABOUT_CAFCASS_CYMRU,
];
const pa11y = require('pa11y');
const axios = Axios.create({ baseURL: config.TEST_URL });
interface Pa11yResult {
documentTitle: string;
pageUrl: string;
issues: PallyIssue[];
}
interface PallyIssue {
code: string;
context: string;
message: string;
selector: string;
type: string;
typeCode: number;
}
function ensurePageCallWillSucceed(url: string): Promise<void> {
return axios.get(url);
}
function runPally(url: string, browser): Promise<Pa11yResult> {
let screenCapture: string | boolean = false;
if (!config.TestHeadlessBrowser) {
const screenshotDir = `${__dirname}/../../../functional-output/pa11y`;
fs.mkdirSync(screenshotDir, { recursive: true });
screenCapture = `${screenshotDir}/${url.replace(/^\/$/, 'home').replace('/', '')}.png`;
}
const fullUrl = `${config.TEST_URL}${url}`;
return pa11y(fullUrl, {
browser,
screenCapture,
hideElements: '.govuk-footer__licence-logo, .govuk-header__logotype-crown',
});
}
function expectNoErrors(messages: PallyIssue[]): void {
const errors = messages.filter(m => m.type === 'error');
if (errors.length > 0) {
const errorsAsJson = `${JSON.stringify(errors, null, 2)}`;
throw new Error(`There are accessibility issues: \n${errorsAsJson}\n`);
}
}
jest.retryTimes(3);
jest.setTimeout(30000);
describe('Accessibility', () => {
let browser;
let cookies;
let hasAfterAllRun = false;
const setup = async () => {
if (hasAfterAllRun) {
return;
}
if (browser) {
await browser.close();
}
browser = await puppeteer.launch({ ignoreHTTPSErrors: true });
browser.on('disconnected', setup);
// Login once only for other pages to reuse session
const page = await browser.newPage();
await page.goto(config.TEST_URL);
await page.type('#username', process.env.CITIZEN_USERNAME);
await page.type('#password', process.env.CITIZEN_PW);
await page.click('input[type="submit"]');
cookies = await page.cookies(config.TEST_URL);
await page.close();
};
beforeAll(setup);
beforeEach(async () => {
const page = await browser.newPage();
await page.goto(config.TEST_URL);
await page.setCookie(...cookies);
await page.goto(`${config.TEST_URL}/info`);
await page.close();
});
afterAll(async () => {
hasAfterAllRun = true;
await browser.close();
});
const urlsNoSignOut = Object.values(urls).filter(url => !IGNORED_URLS.includes(url as string));
describe.each(urlsNoSignOut)('Page %s', url => {
let page;
test('should have no accessibility errors', async () => {
page = await browser.newPage();
await page.goto(config.TEST_URL);
await page.setCookie(...cookies);
const pageUrl = config.TEST_URL + url;
await ensurePageCallWillSucceed(pageUrl as string);
const result = await runPally(pageUrl as string, browser);
expect(result.issues).toEqual(expect.any(Array));
expectNoErrors(result.issues);
});
});
});