web-accessibility-test
Version:
web accessibility testing using axe-core and webdriver.io
382 lines (336 loc) • 13.3 kB
JavaScript
// Contains libraries for for Web Accessibility Testing
// All custom methods requires a `browser` object. This `browser` object can be from the webdriver.io or
// any selenium webdriver instant. As we will be using webdriver.io hence the driver instance here is referrrd as `browser`
;
const axeSource = require('axe-core').source;
const axe = require('axe-core');
class axeConfig {
getViolations() {
try {
let resultAxeArray = [];
let resultAxeObject = {};
let returnObject = {};
returnObject.pageUrl = browser.getUrl();
returnObject.pageTitle = browser.getTitle();
browser.execute(axeSource);
let result = browser.executeAsync(function (done) {
axe.run({
runOnly: {
type: 'tags',
values: ['wcag2a', 'wcag2aa']
}
}, function (err, result) {
if (err) done(err);
done(result);
});
});
try {
if (result.violations.length > 0) {
result.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
} catch (e) {
if (result.value.violations.length > 0) {
result.value.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.value.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
}
returnObject.violations = resultAxeArray;
return returnObject;
} catch (e) {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
getBestPractice() {
try {
let resultAxeArray = [];
let resultAxeObject = {};
let returnObject = {};
returnObject.pageUrl = browser.getUrl();
returnObject.pageTitle = browser.getTitle();
browser.execute(axeSource);
let result = browser.executeAsync(function (done) {
axe.run({
runOnly: {
type: 'tags',
values: ['best-practice']
}
}, function (err, result) {
if (err) done(err);
done(result);
});
});
try {
if (result.violations.length > 0) {
result.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.violations.length === 0) {
resultAxeObject.result = 'Page is aligned with the standards';
resultAxeArray.push(resultAxeObject);
}
} catch (e) {
if (result.value.violations.length > 0) {
result.value.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.value.violations.length === 0) {
resultAxeObject.result = 'Page is aligned with the standards';
resultAxeArray.push(resultAxeObject);
}
}
returnObject.bestPractice = resultAxeArray;
return returnObject;
} catch (e) {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
analyseWithTag(tagArray) {
let response = true;
try {
if (typeof tagArray !== 'object') {
response = false;
throw new Error('wat needs input tags as Array.');
}
let runOnlyConfig = {
runOnly: {
type: 'tags',
values: tagArray
}
};
let resultAxeArray = [];
let resultAxeObject = {};
let returnObject = {};
returnObject.pageUrl = browser.getUrl();
returnObject.pageTitle = browser.getTitle();
browser.execute(axeSource);
let result = browser.executeAsync(function (runOnlyConfig, done) {
axe.run(runOnlyConfig, function (err, result) {
if (err) done(err);
done(result);
});
}, runOnlyConfig);
try {
if (result.violations.length > 0) {
result.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
} catch (e) {
if (result.value.violations.length > 0) {
result.value.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.value.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
}
returnObject.result = resultAxeArray;
return returnObject;
} catch (e) {
if (!response) {
throw new Error('wat needs input tags as Array.');
} else {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
}
analyseWithContext(contextArray) {
let response = true;
try {
if (typeof contextArray !== 'object') {
response = false;
throw new Error('wat needs input tags as Array.');
}
let runOnlyConfig = contextArray[0];
let resultAxeArray = [];
let resultAxeObject = {};
let returnObject = {};
returnObject.pageUrl = browser.getUrl();
returnObject.pageTitle = browser.getTitle();
browser.execute(axeSource);
let result = browser.executeAsync(function (runOnlyConfig, done) {
axe.run(runOnlyConfig, function (err, result) {
if (err) done(err);
done(result);
});
}, runOnlyConfig);
try {
if (result.violations.length > 0) {
result.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
} catch (e) {
if (result.value.violations.length > 0) {
result.value.violations.forEach(value => {
value.nodes.forEach(value1 => {
resultAxeObject.description = value.description;
resultAxeObject.help = value.help;
resultAxeObject.helpUrl = value.helpUrl;
resultAxeObject.id = value.id;
try {
resultAxeObject.impact = value1.any[0].impact;
resultAxeObject.message = value1.any[0].message;
} catch (e) {}
resultAxeObject.failureSummary = value1.failureSummary;
resultAxeObject.html = value1.html;
resultAxeObject.target = JSON.stringify(value1.target);
resultAxeArray.push(resultAxeObject);
});
});
}
if (result.value.violations.length === 0) {
resultAxeObject.result = 'No Violations found in this page';
resultAxeArray.push(resultAxeObject);
}
}
returnObject.result = resultAxeArray;
return returnObject;
} catch (e) {
if (!response) {
throw new Error('wat needs input tags as Array.');
} else {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
}
getRules(tags) {
try {
if (tags) {
browser.execute(axeSource);
return axe.getRules(tags);
} else {
browser.execute(axeSource);
return axe.getRules();
}
} catch (e) {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
runConfig(config) {
try {
browser.execute(axeSource);
if (typeof config !== 'object') {
throw new Error('wat needs an object to configure. See axe-core configure API.');
}
axe.configure(config);
} catch (e) {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
reset() {
try {
browser.execute(axeSource);
axe.reset();
} catch (e) {
throw new Error('wat encountered an error. Check the config and try to re run again.');
}
}
}
module.exports = new axeConfig();