UNPKG

axe-playwright

Version:

Custom Playwright commands to inject axe-core and test for a11y

146 lines (145 loc) 7.21 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultTerminalReporter = exports.checkA11y = exports.reportViolations = exports.getViolations = exports.getAxeResults = exports.configureAxe = exports.injectAxe = void 0; const fs = __importStar(require("fs")); const utils_1 = require("./utils"); const defaultTerminalReporter_1 = __importDefault(require("./reporter/defaultTerminalReporter")); exports.DefaultTerminalReporter = defaultTerminalReporter_1.default; const terminalReporterV2_1 = __importDefault(require("./reporter/terminalReporterV2")); const axe_html_reporter_1 = require("axe-html-reporter"); const junitReporter_1 = __importDefault(require("./reporter/junitReporter")); const path = __importStar(require("path")); /** * Injects axe executable commands in the active window * @param page */ const injectAxe = (page) => __awaiter(void 0, void 0, void 0, function* () { const axe = fs.readFileSync(require.resolve('axe-core/axe.min.js'), { encoding: 'utf8', }); yield page.evaluate((axe) => window.eval(axe), axe); }); exports.injectAxe = injectAxe; /** * Configures axe runtime options * @param page * @param configurationOptions */ const configureAxe = (page_1, ...args_1) => __awaiter(void 0, [page_1, ...args_1], void 0, function* (page, configurationOptions = {}) { yield page.evaluate((configOptions) => window.axe.configure(configOptions), configurationOptions); }); exports.configureAxe = configureAxe; /** * Runs axe-core tools on the relevant page and returns all results * @param page * @param context * @param options */ const getAxeResults = (page, context, options) => __awaiter(void 0, void 0, void 0, function* () { const result = yield page.evaluate(([context, options]) => { return window.axe.run(context || window.document, options); }, [context, options]); return result; }); exports.getAxeResults = getAxeResults; /** * Runs axe-core tools on the relevant page and returns all accessibility violations detected on the page * @param page * @param context * @param options */ const getViolations = (page, context, options) => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, exports.getAxeResults)(page, context, options); return result.violations; }); exports.getViolations = getViolations; /** * Report violations given the reporter. * @param violations * @param reporter */ const reportViolations = (violations, reporter) => __awaiter(void 0, void 0, void 0, function* () { yield reporter.report(violations); }); exports.reportViolations = reportViolations; /** * Performs Axe validations * @param page * @param context * @param axeOptions * @param skipFailures * @param reporter * @param options */ const checkA11y = (page_1, ...args_1) => __awaiter(void 0, [page_1, ...args_1], void 0, function* (page, context = undefined, axeOptions = undefined, skipFailures = false, reporter = 'default', options = undefined) { var _a, _b, _c; const violations = yield (0, exports.getViolations)(page, context, axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.axeOptions); const impactedViolations = (0, utils_1.getImpactedViolations)(violations, axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.includedImpacts); let reporterWithOptions; if (reporter === 'default') { reporterWithOptions = new defaultTerminalReporter_1.default(axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.detailedReport, (_a = axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.detailedReportOptions) === null || _a === void 0 ? void 0 : _a.html, (_b = axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.verbose) !== null && _b !== void 0 ? _b : true); } else if (reporter === 'v2') { reporterWithOptions = new terminalReporterV2_1.default((_c = axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.verbose) !== null && _c !== void 0 ? _c : false); } else if (reporter === 'html') { if (violations.length > 0) { yield (0, axe_html_reporter_1.createHtmlReport)({ results: { violations }, options, }); (0, utils_1.testResultDependsOnViolations)(violations, skipFailures); } else console.log('There were no violations to save in report'); } else if (reporter === 'junit') { // Get the system root directory // Construct the file path const outputFilePath = path.join(process.cwd(), options === null || options === void 0 ? void 0 : options.outputDirPath, options === null || options === void 0 ? void 0 : options.outputDir, options === null || options === void 0 ? void 0 : options.reportFileName); reporterWithOptions = new junitReporter_1.default(axeOptions === null || axeOptions === void 0 ? void 0 : axeOptions.detailedReport, page, outputFilePath); } else { reporterWithOptions = reporter; } if (reporter !== 'html') yield (0, exports.reportViolations)(impactedViolations, reporterWithOptions); if (reporter === 'v2' || (reporter !== 'html' && reporter !== 'junit')) (0, utils_1.testResultDependsOnViolations)(impactedViolations, skipFailures); }); exports.checkA11y = checkA11y;