-
Notifications
You must be signed in to change notification settings - Fork 68
/
axe-test-urls.ts
115 lines (97 loc) · 2.98 KB
/
axe-test-urls.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
import * as WebDriver from 'selenium-webdriver';
import AxeBuilder from '@axe-core/webdriverjs';
import { AxeResults } from 'axe-core';
import { EventResponse, ConfigParams } from '../types';
const testPages = async (
urls: string | string[],
config: ConfigParams,
events?: EventResponse
): Promise<AxeResults[] | AxeResults> => {
const driver: WebDriver.WebDriver = await config.driver;
if (urls.length === 0) {
await driver.quit();
return Promise.resolve([]);
}
return new Promise((resolve, reject) => {
const currentUrl = urls[0].replace(/[,;]$/, '');
if (events?.onTestStart) {
events?.onTestStart(currentUrl);
}
if (config.timer) {
events?.startTimer('axe page load time');
}
driver
.get(currentUrl)
.then(() => {
return driver.executeAsyncScript(`
const callback = arguments[arguments.length - 1];
const script = document.createElement('script');
script.innerHTML = 'document.documentElement.classList.add("deque-axe-is-ready");'
document.documentElement.appendChild(script);
callback();
`);
})
.then(() => {
return driver.wait(
WebDriver.until.elementsLocated(
WebDriver.By.css('.deque-axe-is-ready')
),
10000
);
})
.then(() => {
if (config.timer) {
events?.endTimer('axe page load time');
}
if (config.loadDelay) {
events?.waitingMessage(config.loadDelay);
}
return new Promise(resolve => {
setTimeout(resolve, config.loadDelay);
});
})
.then(() => {
const axe = new AxeBuilder(driver, config.axeSource);
if (Array.isArray(config.include)) {
config.include.forEach((include: string) => axe.include(include));
}
if (Array.isArray(config.exclude)) {
config.exclude.forEach((exclude: string) => axe.exclude(exclude));
}
if (config.tags) {
axe.withTags(config.tags);
} else if (config.rules) {
axe.withRules(config.rules);
}
/* istanbul ignore if */
if (config.disable) {
axe.disableRules(config.disable);
}
if (config.timer) {
events?.startTimer('axe-core execution time');
}
axe.analyze((err: string, results: AxeResults) => {
if (config.timer) {
events?.endTimer('axe-core execution time');
}
/* istanbul ignore if */
if (err) {
return reject(err);
}
// Notify about the update
if (events?.onTestComplete) {
events?.onTestComplete(results);
}
// Move to the next item
testPages(urls.slice(1), config, events).then((out: AxeResults) => {
resolve([results].concat(out));
});
});
})
.catch(async e => {
await driver.quit();
reject(e);
});
});
};
export default testPages;