Skip to content

Commit

Permalink
feat(404): create first approach to check only valid urls
Browse files Browse the repository at this point in the history
  • Loading branch information
felixzapata committed Nov 2, 2016
1 parent 43c19ab commit 3035949
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 33 deletions.
91 changes: 60 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Promise = require('promise');
var fileUrl = require('file-url');
var reporter = require('./lib/reporter');
var chalk = require('chalk');
var request = require('request');
require('chromedriver');

//setup custom phantomJS capability
Expand Down Expand Up @@ -77,48 +78,76 @@ module.exports = function (customOptions, done) {

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

var fileExists = function(filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
return false;
}
}

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
.get(getUrl(url))
.then(function () {
if (options.verbose) {
console.log(chalk.cyan('Analysis start for: ') + url);
}
var startTimestamp = new Date().getTime();
var axeBuilder = new AxeBuilder(driver);

if (options.include) {
axeBuilder.include(options.include);
}

if (options.exclude) {
axeBuilder.exclude(options.exclude);
}

if (tagsAreDefined) {
axeBuilder.withTags(options.tags);
}

if (options.a11yCheckOptions) {
axeBuilder.options(options.a11yCheckOptions);
}

axeBuilder.analyze(function (results) {
results.url = url;
results.timestamp = new Date().getTime();
results.time = results.timestamp - startTimestamp;

var useDriver = function () {
driver
.get(getUrl(url))
.then(function () {
if (options.verbose) {
console.log(chalk.cyan('Analyisis finished for: ') + url);
console.log(chalk.cyan('Analysis start for: ') + url);
}
var startTimestamp = new Date().getTime();
var axeBuilder = new AxeBuilder(driver);

if (options.include) {
axeBuilder.include(options.include);
}

if (options.exclude) {
axeBuilder.exclude(options.exclude);
}
resolve(results);

if (tagsAreDefined) {
axeBuilder.withTags(options.tags);
}

if (options.a11yCheckOptions) {
axeBuilder.options(options.a11yCheckOptions);
}

axeBuilder.analyze(function (results) {
results.url = url;
results.status = 200;
results.timestamp = new Date().getTime();
results.time = results.timestamp - startTimestamp;
if (options.verbose) {
console.log(chalk.cyan('Analyisis finished for: ') + url);
}
resolve(results);
});
});
}

var resourceNotValid = function () {
console.log(chalk.red('Error loading source: ') + url);
resolve({
url: url,
status: 404,
violations: [],
timestamp: new Date().getTime()
});
}

request.get(url)
.on('error', resourceNotValid)
.on('response', useDriver)
.end();
});

})).then(createResults);

};
6 changes: 5 additions & 1 deletion lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ function color(code, str) {
return '\u001b[' + code + 'm' + str + '\u001b[0m';
}

function checkStatusCode(result) {
return result.status !== 404;
}

module.exports = function (results, threshold) {
var violations;
results.forEach(function (result) {
results.filter(checkStatusCode).forEach(function (result) {
violations = result.violations;
console.log(chalk.cyan('File to test: ' + result.url));
if (violations.length) {
Expand Down
18 changes: 17 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('gulp-axe-webdriver', function () {
process.stdout.write = write;
});

describe('using Chrome', function () {
xdescribe('using Chrome', function () {

it('should pass the a11y validation', function (done) {
var options = {
Expand Down Expand Up @@ -164,6 +164,7 @@ describe('gulp-axe-webdriver', function () {
});
});
});

describe('using verbose option', function () {
it('should show information messages about the analysis', function (done) {
var options = {
Expand All @@ -180,6 +181,7 @@ describe('gulp-axe-webdriver', function () {
});
});
});

describe('using a11yCheckOptions', function () {
it('should override the rules', function (done) {
var options = {
Expand All @@ -198,4 +200,18 @@ describe('gulp-axe-webdriver', function () {
});
});
});

describe('detect 404 errors', function() {
it('should show a not valid url or resource', function (done) {
var options = {
urls: ['http://www.estaurlnoexiste.com/'],
browser: 'phantomjs'
};
return axe(options, function () {
assert.notEqual(output.match(/The resource http:\/\/www.estaurlnoexiste.com\/ is not valid/gi), null);
done();
});
});
})

});

0 comments on commit 3035949

Please sign in to comment.