Skip to content

Commit

Permalink
updates dependencies make some rework to actualize it
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvain Liechti committed Jul 9, 2018
1 parent c5704e8 commit 2d37e1b
Show file tree
Hide file tree
Showing 12 changed files with 7,196 additions and 252 deletions.
50 changes: 27 additions & 23 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
{
"extends": "eslint:recommended",
"rules": {
"no-console": "off"
},
"env": {
"browser": true
},
"plugins": [
"html"
],
"globals": {
"require" : false,
"__dirname" : false,
"describe" : false,
"process" : false,
"beforeEach" : false,
"afterEach" : false,
"module" : false,
"it" : false,
"xit": false,
"xdescribe": false
}
}
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"no-console": "off"
},
"env": {
"node": true,
"browser": false
},
"plugins": [
"html"
],
"globals": {
"require" : false,
"__dirname" : false,
"describe" : false,
"process" : false,
"beforeEach" : false,
"afterEach" : false,
"module" : false,
"it" : false,
"xit": false,
"xdescribe": false
}
}
206 changes: 104 additions & 102 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
'use strict';
var path = require('path');
var fs = require('fs-path');
var glob = require('glob');
var AxeBuilder = require('axe-webdriverjs');
var WebDriver = require('selenium-webdriver');
var Promise = require('promise');
var fileUrl = require('file-url');
var reporter = require('./lib/reporter');
var chalk = require('chalk');
var request = require('then-request');
const path = require('path');
const fs = require('fs-extra');
const glob = require('glob');
const AxeBuilder = require('axe-webdriverjs');
const WebDriver = require('selenium-webdriver');
const Promise = require('promise');
const fileUrl = require('file-url');
const reporter = require('./lib/reporter');
const chalk = require('chalk');
const request = require('then-request');
require('chromedriver');

module.exports = function (customOptions, done) {
function flatten(arr) {
return [].concat.apply([], arr);
}

function isRemoteUrl(url) {
return url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0;
}

function getUrl(url) {
return isRemoteUrl(url) ? url : fileUrl(url);
}

function checkNotValidUrls(result) {
return new Promise(function (resolve) {
request('GET', result.url).then((res) => {
result.status = res.statusCode;
resolve(result);
}).catch(() => {
result.status = 404;
resolve(result);
});
});
}

function getRemoteUrls(result) {
if (isRemoteUrl(result.url)) {
return result;
}
}

var defaultOptions = {
function getLocalUrls(result) {
if (!isRemoteUrl(result.url)) {
return result;
}
}

function onlyViolations(item) {
return item.violations.length > 0;
}

function removePassesValues(item) {
delete item.passes;
return item;
}

function mergeArray(coll, item) {
coll.push(item);
return coll;
}

function findGlobPatterns(urls) {
return urls.map((url) => {
return isRemoteUrl(url) ? url : glob.sync(url);
});
}

module.exports = function aXe(customOptions, done) {
const defaultOptions = {
folderOutputReport: 'aXeReports',
showOnlyViolations: false,
verbose: false,
Expand All @@ -24,108 +79,30 @@ module.exports = function (customOptions, done) {
threshold: 0
};

var options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
var chromeCapabilities = WebDriver.Capabilities.chrome();
var chromeOptions = options.headless ? { 'args': ['--headless'] } : {};
const options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
const chromeCapabilities = WebDriver.Capabilities.chrome();
const chromeOptions = options.headless ? { 'args': ['--headless'] } : {};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new WebDriver.Builder().withCapabilities(chromeCapabilities).build();
const driver = new WebDriver.Builder().withCapabilities(chromeCapabilities).build();

try {
driver.manage();
// browser is open
} catch(NoSuchSessionError) {
// browser is closed
driver.manage().timeouts().setScriptTimeout(60000);
}

var tagsAreDefined = (!Array.isArray(options.tags) && options.tags !== null && options.tags !== '') ||
(Array.isArray(options.tags) && options.tags.length > 0);

var isRemoteUrl = function (url) {
return url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0;
};

var flatten = function (arr) {
return [].concat.apply([], arr);
};

var getUrl = function (url) {
return isRemoteUrl(url) ? url : fileUrl(url);
}

var checkNotValidUrls = function (result) {
return new Promise(function (resolve) {
request('GET', result.url, function (error) {
result.status = (error) ? 404 : 200;
resolve(result);
});
});
}

var getRemoteUrls = function (result) {
if (isRemoteUrl(result.url)) {
return result;
}
};

var getLocalUrls = function (result) {
if (!isRemoteUrl(result.url)) {
return result;
}
};

var onlyViolations = function (item) {
return item.violations.length > 0;
};

var removePassesValues = function (item) {
delete item.passes;
return item;
};

var mergeArray = function (coll, item) {
coll.push(item);
return coll;
};

var createResults = function (results) {
var dest = '';
var localUrls = results.filter(getLocalUrls);
var remoteUrls = results.filter(getRemoteUrls);
var promises = remoteUrls.map(checkNotValidUrls);
var resultsForReporter;
Promise.all(promises).then(function (results) {
resultsForReporter = localUrls.reduce(mergeArray, results);
if (options.showOnlyViolations) {
resultsForReporter = resultsForReporter.map(removePassesValues).filter(onlyViolations);
}
if (options.saveOutputIn !== '') {
dest = path.join(options.folderOutputReport, options.saveOutputIn);
fs.writeFileSync(dest, JSON.stringify(resultsForReporter, null, ' '));
}
if (options.verbose) {
console.log(chalk.yellow('Preparing results'));
console.log(chalk.yellow('================='));
}
reporter(resultsForReporter, options.threshold);
driver.quit().then(function () {
done();
});
});
};

var findGlobPatterns = function (urls) {
return urls.map(function (url) {
return isRemoteUrl(url) ? url : glob.sync(url);
});
};
const tagsAreDefined = (!Array.isArray(options.tags) && options.tags !== null && options.tags !== '') ||
(Array.isArray(options.tags) && options.tags.length > 0);

var urls = flatten(findGlobPatterns(options.urls));
const urls = flatten(findGlobPatterns(options.urls));

if (options.verbose) {
console.log(chalk.yellow('Start reading the urls'));
console.log(chalk.yellow('======================'));
}

Promise.all(urls.map(function (url) {
return new Promise(function (resolve) {
driver
Expand Down Expand Up @@ -164,7 +141,32 @@ module.exports = function (customOptions, done) {
});
});
});

})).then(createResults);

})).then(function (results) {
let dest = '';
const localUrls = results.filter(getLocalUrls);
const remoteUrls = results.filter(getRemoteUrls);
const promises = remoteUrls.map(checkNotValidUrls);
let resultsForReporter;

return Promise.all(promises).then(function (results) {
resultsForReporter = localUrls.reduce(mergeArray, results);
if (options.showOnlyViolations) {
resultsForReporter = resultsForReporter.map(removePassesValues).filter(onlyViolations);
}
if (options.saveOutputIn !== '') {
dest = path.join(options.folderOutputReport, options.saveOutputIn);
try {
fs.outputFileSync(dest, JSON.stringify(resultsForReporter, null, ' '));
} catch (error) {
console.log(error);
}
}
if (options.verbose) {
console.log(chalk.yellow('Preparing results'));
console.log(chalk.yellow('================='));
}
reporter(resultsForReporter, options.threshold);
driver.quit().then(done);
});
});
};
10 changes: 3 additions & 7 deletions lib/reporter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict';

var chalk = require('chalk');

function color(code, str) {
return '\u001b[' + code + 'm' + str + '\u001b[0m';
}
const chalk = require('chalk');

function isValidURL(status) {
return status !== 404;
}

module.exports = function (results, threshold) {
results.forEach(function (result) {
var violations = result.violations;
const violations = result.violations;
console.log(chalk.cyan('File to test: ' + result.url));
if (isValidURL(result.status)) {
if (violations.length) {
Expand All @@ -24,7 +20,7 @@ module.exports = function (results, threshold) {
console.log(chalk.green('Found ' + violations.length + ' accessibility violations: (under threshold of ' + threshold + ')'));
}
violations.forEach(function (ruleResult) {
console.log(' ' + color(31, '\u00D7') + ' ' + ruleResult.help);
console.log(' ' + chalk.red('\u00D7') + ' ' + ruleResult.help);

ruleResult.nodes.forEach(function (violation, index) {
console.log(' ' + (index + 1) + '. ' + JSON.stringify(violation.target));
Expand Down
Loading

0 comments on commit 2d37e1b

Please sign in to comment.