-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathaxePuppeteer.ts
274 lines (231 loc) · 6.21 KB
/
axePuppeteer.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import * as Axe from 'axe-core';
import { ElementHandle, Frame, JSONObject, Page } from 'puppeteer';
import { pageIsLoaded, runAxe, configureAxe } from './browser';
import { AnalyzeCB } from './types';
function arrayify<T>(src: T | T[]): T[] {
if (!Array.isArray(src)) {
return [src];
}
return src;
}
interface IInjectAxeArgs {
source?: string | Function;
selector: string;
logOnError?: boolean;
args?: any[];
}
function injectJSModule(frame: Frame): Promise<void> {
return frame.addScriptTag({
path: require.resolve('axe-core')
});
}
function injectJSSource(
frame: Frame,
source: string | Function,
args: any[] = []
): Promise<void> {
return frame.evaluate(source as any, ...args);
}
async function injectJS(
frame: Frame | undefined,
{ source, selector, logOnError, args }: IInjectAxeArgs
): Promise<void> {
if (!frame) {
return;
}
const frames = await frame.$$(selector);
const injections = [];
for (const frameElement of frames) {
const subFrame = await frameElement.contentFrame();
const p = injectJS(subFrame as Frame, {
source,
selector,
args,
logOnError: true
});
injections.push(p);
}
const reportError = (): void => {
// tslint:disable-next-line:no-console
console.error(`Failed to inject axe-core into frame (${frame.url()})`);
};
let injectP: Promise<void>;
if (!source) {
injectP = injectJSModule(frame);
} else {
injectP = injectJSSource(frame, source, args);
}
if (logOnError) {
// Just print diagnostic if a child frame fails to load.
// Don't fully error since we aren't the top-level frame
injectP = injectP.catch(reportError);
}
injections.push(injectP);
// Fix return type since we don't care about the value
return Promise.all(injections).then(() => undefined);
}
function isPage(pageFrame: Page | Frame): pageFrame is Page {
return (pageFrame as any).mainFrame !== undefined;
}
function getFrame(pageFrame: Page | Frame): Frame {
if (isPage(pageFrame)) {
return pageFrame.mainFrame();
}
return pageFrame;
}
async function ensureFrameReady(frame: Frame): Promise<void> {
// Wait so that we know there is an execution context.
// Assume that if we have an html node we have an execution context.
await frame.waitForSelector('html');
// Check if the page is loaded.
const pageReady = await frame.evaluate(pageIsLoaded);
if (!pageReady) {
throw new Error('Page/Frame is not ready');
}
}
function normalizeContext(
includes: string[][],
excludes: string[][]
): Axe.ElementContext | null {
if (!excludes.length && !includes.length) {
return null;
}
const ctx: Axe.ElementContext = {};
if (excludes.length) {
ctx.exclude = excludes;
}
if (includes.length) {
ctx.include = includes;
}
return ctx;
}
export class AxePuppeteer {
private frame: Frame;
private source?: string;
private includes: string[][];
private excludes: string[][];
private axeOptions: Axe.RunOptions | null;
private config: Axe.Spec | null;
private disabledFrameSelectors: string[];
constructor(pageFrame: Page | Frame, source?: string) {
this.frame = getFrame(pageFrame);
this.source = source;
this.includes = [];
this.excludes = [];
this.axeOptions = null;
this.config = null;
this.disabledFrameSelectors = [];
}
public include(selector: string | string[]): this {
selector = arrayify(selector);
this.includes.push(selector);
return this;
}
public exclude(selector: string | string[]): this {
selector = arrayify(selector);
this.excludes.push(selector);
return this;
}
public options(options: Axe.RunOptions): this {
this.axeOptions = options;
return this;
}
public withRules(rules: string | string[]): this {
rules = arrayify(rules);
if (!this.axeOptions) {
this.axeOptions = {};
}
this.axeOptions.runOnly = {
type: 'rule',
values: rules
};
return this;
}
public withTags(tags: string | string[]): this {
tags = arrayify(tags);
if (!this.axeOptions) {
this.axeOptions = {};
}
this.axeOptions.runOnly = {
type: 'tag',
values: tags
};
return this;
}
public disableRules(rules: string | string[]): this {
rules = arrayify(rules);
if (!this.axeOptions) {
this.axeOptions = {};
}
interface IRulesObj {
[id: string]: {
enabled: boolean;
};
}
const newRules: IRulesObj = {};
for (const rule of rules) {
newRules[rule] = {
enabled: false
};
}
this.axeOptions.rules = newRules;
return this;
}
public configure(config: Axe.Spec): this {
// Cast to any because we are asserting for javascript provided argument.
if (typeof (config as any) !== 'object') {
throw new Error(
'AxePuppeteer needs an object to configure. See axe-core configure API.'
);
}
this.config = config;
return this;
}
public disableFrame(selector: string): this {
this.disabledFrameSelectors.push(selector);
return this;
}
public async analyze(): Promise<Axe.AxeResults>;
public async analyze<T extends AnalyzeCB>(
callback?: T
): Promise<Axe.AxeResults | null>;
public async analyze<T extends AnalyzeCB>(
callback?: T
): Promise<Axe.AxeResults | null> {
try {
await ensureFrameReady(this.frame);
await injectJS(this.frame, {
source: this.source,
selector: this.iframeSelector()
});
await injectJS(this.frame, {
source: configureAxe,
selector: this.iframeSelector(),
args: [this.config]
});
const context = normalizeContext(this.includes, this.excludes);
const axeResults = await this.frame.evaluate(
runAxe,
context as JSONObject,
this.axeOptions as JSONObject
);
if (callback) {
callback(null, axeResults);
}
return axeResults;
} catch (err) {
if (callback) {
callback(err);
return null;
}
throw err;
}
}
private iframeSelector(): string {
let selector = 'iframe';
for (const disabledFrameSelector of this.disabledFrameSelectors) {
selector += `:not(${disabledFrameSelector})`;
}
return selector;
}
}