Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #65 from dequelabs/modernize-project
Browse files Browse the repository at this point in the history
Modernize project
  • Loading branch information
stephenmathieson authored Jun 21, 2018
2 parents 11a65ae + f642607 commit 1586dbd
Show file tree
Hide file tree
Showing 18 changed files with 6,033 additions and 2,316 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
output
*.tgz
npm-shrinkwrap.json
.nyc_output/
coverage/
70 changes: 0 additions & 70 deletions Gruntfile.js

This file was deleted.

25 changes: 19 additions & 6 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
machine:
node:
version: v6.1.0
dependencies:
pre:
- npm i selenium-webdriver@2.53.1
version: 2

jobs:
build:
docker:
- image: circleci/node:6-browsers
steps:
- checkout
- restore_cache:
keys:
- npm-{{ checksum "package-lock.json" }}
- npm
- run: npm install
- save_cache:
key: npm-{{ checksum "package-lock.json" }}
paths:
- node_modules
- run: npm run lint
- run: npm run test:unit
80 changes: 45 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function AxeBuilder(driver, source) {
return new AxeBuilder(driver, source);
}

this._driver = driver;
this._driver = driver;
this._source = source || null;
this._includes = [];
this._excludes = [];
Expand All @@ -24,8 +24,8 @@ function AxeBuilder(driver, source) {
* @return {AxeBuilder}
*/
AxeBuilder.prototype.include = function(selector) {
this._includes.push(Array.isArray(selector) ? selector : [selector]);
return this;
this._includes.push(Array.isArray(selector) ? selector : [selector]);
return this;
};

/**
Expand All @@ -34,8 +34,8 @@ AxeBuilder.prototype.include = function(selector) {
* @return {AxeBuilder}
*/
AxeBuilder.prototype.exclude = function(selector) {
this._excludes.push(Array.isArray(selector) ? selector : [selector]);
return this;
this._excludes.push(Array.isArray(selector) ? selector : [selector]);
return this;
};

/**
Expand All @@ -44,16 +44,16 @@ AxeBuilder.prototype.exclude = function(selector) {
* @return {AxeBuilder}
*/
AxeBuilder.prototype.options = function(options) {
this._options = options;
return this;
this._options = options;
return this;
};

/**
* Limit analysis to only the specified rules. Cannot be used with `withTags`.
* @param {Array|String} rules Array of rule IDs, or a single rule ID as a string
* @return {AxeBuilder}
*/
AxeBuilder.prototype.withRules = function (rules) {
AxeBuilder.prototype.withRules = function(rules) {
rules = Array.isArray(rules) ? rules : [rules];
this._options = this._options || {};
this._options.runOnly = {
Expand All @@ -69,7 +69,7 @@ AxeBuilder.prototype.withRules = function (rules) {
* @param {Array|String} rules Array of tags, or a single tag as a string
* @return {AxeBuilder}
*/
AxeBuilder.prototype.withTags = function (tags) {
AxeBuilder.prototype.withTags = function(tags) {
tags = Array.isArray(tags) ? tags : [tags];
this._options = this._options || {};
this._options.runOnly = {
Expand All @@ -90,11 +90,13 @@ AxeBuilder.prototype.disableRules = function(rules) {
this._options = this._options || {};
this._options.rules = {};

rules.forEach(function(rulesConfiguration, ruleToDisable) {
rulesConfiguration[ruleToDisable] = {
enabled: false
};
}.bind(null, this._options.rules));
rules.forEach(
function(rulesConfiguration, ruleToDisable) {
rulesConfiguration[ruleToDisable] = {
enabled: false
};
}.bind(null, this._options.rules)
);

return this;
};
Expand All @@ -105,7 +107,9 @@ AxeBuilder.prototype.disableRules = function(rules) {
*/
AxeBuilder.prototype.configure = function(config) {
if (typeof config !== 'object') {
throw new Error('AxeBuilder needs an object to configure. See axe-core configure API.');
throw new Error(
'AxeBuilder needs an object to configure. See axe-core configure API.'
);
}

this._config = config;
Expand All @@ -118,31 +122,37 @@ AxeBuilder.prototype.configure = function(config) {
* @return {Promise}
*/
AxeBuilder.prototype.analyze = function(callback) {
var context = normalizeContext(this._includes, this._excludes),
var context = normalizeContext(this._includes, this._excludes),
driver = this._driver,
options = this._options,
config = this._config,
source = this._source;

return new Promise(function(resolve, reject) {
inject(driver, source, config, function() {
driver
.executeAsyncScript(function(context, options, config) {
/*global document, axe */
if (config !== null) {
window.axe.configure(config);
}
window.axe.run(context || document, options || {})
.then(arguments[arguments.length - 1]);
}, context, options, config)
.then(function(results) {
if (callback) {
callback(results);
}
resolve(results);
});
});
});
return new Promise(function(resolve) {
inject(driver, source, config, function() {
driver
.executeAsyncScript(
function(context, options, config) {
/* eslint-env browser */
if (config !== null) {
window.axe.configure(config);
}
window.axe
.run(context || document, options || {})
.then(arguments[arguments.length - 1]);
},
context,
options,
config
)
.then(function(results) {
if (callback) {
callback(results);
}
resolve(results);
});
});
});
};

exports = module.exports = AxeBuilder;
83 changes: 44 additions & 39 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@
* @param {WebDriver} driver The driver to inject into
*/
function findFramesAndInject(parent, script, driver) {
driver
.findElements({
tagName: 'iframe'
})
.then(function(results) {
results.forEach(function(frame) {
driver.switchTo().defaultContent();
if (parent) {
parent.forEach(function(p) {
driver.switchTo().frame(p);
});
}
driver.switchTo().frame(frame)
.then(function() {
driver
.executeScript(script)
.then(function() {
findFramesAndInject((parent || []).concat(frame), script, driver);
});
}).catch(function (e) {
console.log('Failed to inject axe-core into one of the iframes!');
driver.switchTo().defaultContent();
});
});
});
driver
.findElements({
tagName: 'iframe'
})
.then(function(results) {
results.forEach(function(frame) {
driver.switchTo().defaultContent();
if (parent) {
parent.forEach(function(p) {
driver.switchTo().frame(p);
});
}
driver
.switchTo()
.frame(frame)
.then(function() {
driver.executeScript(script).then(function() {
findFramesAndInject((parent || []).concat(frame), script, driver);
});
})
.catch(function() {
// eslint-disable-next-line no-console
console.log('Failed to inject axe-core into one of the iframes!');
driver.switchTo().defaultContent();
});
});
});
}

/**
Expand All @@ -42,21 +44,24 @@ function findFramesAndInject(parent, script, driver) {
* @param {Function} callback Callback to execute when aXe has been injected
*/
module.exports = function(driver, axeSource, config, callback) {
axeSource = axeSource || require('axe-core').source;
axeSource = axeSource || require('axe-core').source;

var configSrc = config !== null ? 'axe.configure(' + JSON.stringify(config) + ');' : '';
var script = axeSource + configSrc + 'axe.configure({branding:{application:"webdriverjs"}});';
var configSrc =
config !== null ? 'axe.configure(' + JSON.stringify(config) + ');' : '';
var script =
axeSource +
configSrc +
'axe.configure({branding:{application:"webdriverjs"}});';

driver
.switchTo().defaultContent();
driver.switchTo().defaultContent();

driver
.executeScript(script)
.then(function() {
findFramesAndInject(null, script, driver);
})
.then(function() {
driver.switchTo().defaultContent();
callback();
});
driver
.executeScript(script)
.then(function() {
findFramesAndInject(null, script, driver);
})
.then(function() {
driver.switchTo().defaultContent();
callback();
});
};
37 changes: 18 additions & 19 deletions lib/normalize-context.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@

/**
* Normalizes context parameter with includes and excludes
* @private
* @param {Array} include Array of selectors to include
* @param {Array} exclude Array of selectors to exclude
* @return {Object}
*/
exports = module.exports = function (include, exclude) {
if (!exclude.length) {
if (!include.length) {
return null;
}
exports = module.exports = function(include, exclude) {
if (!exclude.length) {
if (!include.length) {
return null;
}

return {
include: include
};
}
return {
include: include
};
}

if (!include.length) {
return {
exclude: exclude
};
}
if (!include.length) {
return {
exclude: exclude
};
}

return {
include: include,
exclude: exclude
};
return {
include: include,
exclude: exclude
};
};
Loading

0 comments on commit 1586dbd

Please sign in to comment.