Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(axe): Add aXe testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
contolini authored and sjelin committed Oct 28, 2016
1 parent c6ac939 commit a4f3b03
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
@@ -1,8 +1,9 @@
Accessibility Plugin
====================

Protractor comes with support for two accessibility testing options:
Protractor comes with support for three accessibility testing options:
* Accessibility Developer Tools
* aXe Accessibility Engine
* Tenon.io

Protractor will run each set of audits (depending on your configuration) on your existing end-to-end
Expand All @@ -14,15 +15,18 @@ To understand how each of these tools can be used, see this support matrix:

| Testing Library | Pricing | API Key | External Request | No. of Tests | Info |
|--------------------------------------|-------------------------------------------|---------|------------------|--------------|-------------------------------------------------------------------------|
| Chrome Accessibility Developer Tools | Free | No | No | 14 | [Github](https://github.com/GoogleChrome/accessibility-developer-tools) |
| Tenon.io | Free limited accounts, paid subscriptions | Yes | Yes | 63 | [Tenon.io](http://tenon.io/) |
| Chrome Accessibility Developer Tools | Free | No | No | 14 | [GitHub](https://github.com/GoogleChrome/accessibility-developer-tools) |
| aXe Accessibility Engine | Free | No | No | 53 | [GitHub](https://github.com/dequelabs/axe-core/)
| Tenon.io | Free limited accounts, paid subscriptions | Yes | Yes | 63 | [Tenon.io](http://tenon.io/) | |

Protractor now supports the [Accessibility Developer Tools](https://github.com/GoogleChrome/accessibility-developer-tools), the same audit library used by the [Chrome browser extension](https://chrome.google.com/webstore/detail/accessibility-developer-t/fpkknkljclfencbdbgkenhalefipecmb?hl=en). Protractor
[runs an audit](https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules)
locally by injecting the Dev Tools script into WebDriver pages, and it can diagnose issues including
missing labels, incorrect ARIA attributes and color contrast. This is a great starting point if
you can't send source code over the wire through an API.

[aXe](https://github.com/dequelabs/axe-core) operates similarly to Chrome's Accessibility Developer Tools by injecting axe-core into all frames and checking the content against 53 [accessibility rules](https://github.com/dequelabs/axe-core/blob/master/doc/rule-descriptions.md).

[Tenon.io](http://www.tenon.io) has a more robust set of tests to help you find
accessibility issues, but it requires [registering](http://tenon.io/register.php) for an API key
and making an external request for each test, which may not work for everyone. Some people use
Expand Down Expand Up @@ -65,6 +69,16 @@ Enable this plugin in your config file:
}]
}
```
```js
// aXe only:
exports.config = {
...
plugins: [{
axe: true,
package: 'protractor-accessibility-plugin'
}]
}
```
```js
// Tenon.io only:
exports.config = {
Expand Down
53 changes: 53 additions & 0 deletions index.js
Expand Up @@ -3,6 +3,7 @@ var q = require('q'),
path = require('path'),
_ = require('lodash'),
request = require('request'),
AxeBuilder = require('axe-webdriverjs'),
Entities = require('html-entities').XmlEntities;

/**
Expand Down Expand Up @@ -57,6 +58,10 @@ function teardown() {

var audits = [];

if (this.config.axe) {
audits.push(runAxe(this));
}

if (this.config.chromeA11YDevTools) {
audits.push(runChromeDevTools(this));
}
Expand Down Expand Up @@ -226,6 +231,54 @@ function runChromeDevTools(context) {
});
});
});

}

/**
* Audits page source against aXe: https://github.com/dequelabs/axe-core
*
* @param {Object} context The plugin context object
* @return {q.Promise} A promise which resolves when the audit is finished
* @private
*/
function runAxe(context) {

var deferred = q.defer();

AxeBuilder(browser.driver)
.analyze(function(results) {
deferred.resolve(results);
});

return deferred.promise.then(function(results) {
return processAxeResults(results);
});

function processAxeResults(results) {

var testHeader = 'aXe - '

var numResults = results.violations.length;

if (numResults === 0) {
return context.addSuccess();
}

results.passes.forEach(function(result) {
context.addSuccess({specName: testHeader + result.help});
});

return results.violations.forEach(function(result) {
var label = result.nodes.length === 1 ? ' element ' : ' elements ';
var msg = result.nodes.reduce(function(msg, node) {
return msg + '\t\t' + node.html + '\n';
}, '\n');
msg = '\n\t\t' + result.nodes.length + label + 'failed:' + msg + '\n\n\t\t' + result.helpUrl
context.addFailure(msg, {specName: testHeader + result.help});
});

}

}

// Export
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -29,6 +29,8 @@
"homepage": "https://github.com/angular/protractor-accessibility-plugin",
"dependencies": {
"accessibility-developer-tools": "^2.9.0-rc.0",
"axe-core": "^2.0.5",
"axe-webdriverjs": "^0.2.0",
"html-entities": "^1.2.0",
"lodash": "^3.10.1",
"q": "^1.4.1",
Expand Down
1 change: 1 addition & 0 deletions spec/failureConfig.js
Expand Up @@ -16,6 +16,7 @@ exports.config = {
chromeA11YDevTools: {
treatWarningsAsFailures: true
},
axe: true,
path: '../index.js'
}]
};
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -26,6 +26,15 @@ executor.addCommandlineTest(
{
message: '1 element failed:'
},
{
message: '1 element failed:'
},
{
message: '3 elements failed:'
},
{
message: '1 element failed:'
},
{
message: '1 element failed:'
}]);
Expand Down

0 comments on commit a4f3b03

Please sign in to comment.