UNPKG

@axe-core/cli

Version:

A CLI for accessibility testing using axe-core

175 lines (169 loc) 7.24 kB
"use strict"; 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 }); const colors_1 = __importDefault(require("colors")); const utils_1 = require("../lib/utils"); const axe_test_urls_1 = __importDefault(require("../lib/axe-test-urls")); const events_1 = __importDefault(require("../lib/events")); const webdriver_1 = require("../lib/webdriver"); const selenium_webdriver_1 = require("selenium-webdriver"); const cli = (args, url) => __awaiter(void 0, void 0, void 0, function* () { const { save, stdout, dir, exit, timer, reporter: noReporter, chromeOptions, verbose, timeout, include, exclude, tags, rules, disable, loadDelay, chromedriverPath, chromePath } = args; const showErrors = args.showErrors === true; const silentMode = !!stdout; args.axeSource = (0, utils_1.getAxeSource)(args.axeSource); if (!args.axeSource) { console.error((0, utils_1.error)('Unable to find the axe-core source file')); process.exit(2); } args.browser = (0, utils_1.parseBrowser)(args.browser); /* istanbul ignore if */ if (chromeOptions) { /* istanbul ignore if */ if (args.browser !== 'chrome-headless') { console.error((0, utils_1.error)('You may only provide --chrome-options when using headless chrome')); process.exit(2); } } const driverConfigs = { browser: args.browser, timeout, chromeOptions, chromedriverPath, chromePath }; args.driver = (0, webdriver_1.startDriver)(driverConfigs); const cliReporter = (0, utils_1.reporter)(noReporter, silentMode); const axeVersion = (0, utils_1.getAxeVersion)(args.axeSource); if (!silentMode) { console.log(colors_1.default.bold('Running axe-core ' + axeVersion + ' in ' + args.browser)); } const urls = url.args.map(utils_1.parseUrl); /* istanbul ignore if */ if (urls.length === 0) { console.error((0, utils_1.error)('No url was specified. Check `axe --help` for help\n')); } const events = (0, events_1.default)({ silentMode, timer, cliReporter, verbose, exit }); const testPageConfigParams = { driver: args.driver, timer, loadDelay, axeSource: args.axeSource, include, exclude, tags, rules, disable }; let outcome; try { try { outcome = yield (0, axe_test_urls_1.default)(urls, testPageConfigParams, events); } catch (e) { if (e instanceof selenium_webdriver_1.error.ScriptTimeoutError) { console.error((0, utils_1.error)('Error: %s'), e.message); console.log(`The timeout is currently configured to be ${timeout} seconds (you can change it with --timeout).`); process.exit(2); } // Provide a more user-friendly error message when there's a ChromeDriver/Chrome version mismatch. else if (e instanceof selenium_webdriver_1.error.SessionNotCreatedError && e.message.includes('This version of ChromeDriver only supports' // This string has to match the error message printed by chromedriver, see // https://chromium.googlesource.com/chromium/src/+/refs/tags/110.0.5481.194/chrome/test/chromedriver/chrome_launcher.cc#300. )) { console.error((0, utils_1.error)('Error: %s'), e.message); console.log(`\nPlease use browser-driver-manager to install matching versions of Chrome and ChromeDriver: $ npx browser-driver-manager install chrome This will install the latest synced versions. You may install specific synced versions using $ npx browser-driver-manager install chrome@<version> where <version> is a specific version, e.g. 123.45.67, or a channel, e.g. canary. You may also pass the \`--chromedriver-path\` option to axe: $ axe --chromedriver-path <path/to/chromedriver-executable>`); process.exit(2); } else { throw e; } } if (silentMode) { process.stdout.write(JSON.stringify(outcome, null, 2)); return; } if (timer) { console.timeEnd('Total test time'); } /* istanbul ignore if */ if (Array.isArray(outcome)) { console.log((0, utils_1.bold)('Testing complete of %d pages\n'), outcome.length); } if (save || dir) { try { const fileName = (0, utils_1.saveOutcome)(outcome, save, dir); console.log('Saved file at', fileName, '\n'); } catch (e) { /* istanbul ignore next */ console.error((0, utils_1.error)('Unable to save file!\n') + e); process.exit(1); } } if (exit) { let exitErr = false; /* istanbul ignore if */ if (Array.isArray(outcome)) { for (const res of outcome) { if (res.violations.length > 0) { exitErr = true; break; } } } else { exitErr = outcome.violations.length > 0; } if (exitErr) { process.exit(1); } } /* istanbul ignore if */ if (silentMode) { return; } console.log((0, utils_1.italics)('Please note that only 20% to 50% of all accessibility ' + 'issues can automatically be detected. \nManual testing is ' + 'always required. For more information see:\n%s\n'), (0, utils_1.link)('https://dequeuniversity.com/class/testing')); } catch (e) { /* istanbul ignore else */ if (!showErrors) { console.error((0, utils_1.error)('An error occurred while testing this page.')); } else { console.error((0, utils_1.error)('Error: %s'), e); } console.error('Please report the problem to: ' + (0, utils_1.link)('https://github.com/dequelabs/axe-core-npm/issues/') + '\n'); process.exit(1); } }); exports.default = cli; //# sourceMappingURL=index.js.map