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

Commit

Permalink
test(a11yPlugin): add failing tests
Browse files Browse the repository at this point in the history
Also includes missing Angular map files
  • Loading branch information
Marcy Sutton committed Feb 24, 2015
1 parent 64704e0 commit fedb131
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 3 deletions.
110 changes: 110 additions & 0 deletions plugins/accessibility/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var q = require('q'),
fs = require('fs'),
path = require('path'),
_ = require('lodash');

/**
* You can enable this plugin in your config file:
*
* // The Chrome Accessibility Developer Tools are currently
* // the only integration option.
*
* exports.config = {
* ...
* plugins: [{
* chromeA11YDevTools: true,
* path: 'node_modules/protractor.plugins/accessiblity'
* }]
* }
*
*/

var AUDIT_FILE = path.join(__dirname, '../../node_modules/accessibility-developer-tools/dist/js/axs_testing.js');

/**
* Checks the information returned by the accessibility audit and
* displays passed/failed results as console output.
*
* @param {Object} config The configuration file for the accessibility plugin
* @return {q.Promise} A promise which resolves to the results of any passed or
* failed tests
* @public
*/
function teardown(config) {

if (config.chromeA11YDevTools) {

var data = fs.readFileSync(AUDIT_FILE, 'utf-8');
data = data + ' return axs.Audit.run();';

var testOut = {failedCount: 0, specResults: []},
elementPromises = [];

return browser.executeScript_(data, 'a11y developer tool rules').then(function(results) {

var audit = results.map(function(result) {
var DOMElements = result.elements;
if (DOMElements !== undefined) {

DOMElements.forEach(function(elem) {
// get elements from WebDriver, add to promises array
elementPromises.push(
elem.getOuterHtml().then(function(text) {
return {
code: result.rule.code,
list: text
};
})
);
});
result.elementCount = DOMElements.length;
}
return result;
});

// Wait for element names to be fetched
return q.all(elementPromises).then(function(elementFailures) {

audit.forEach(function(result, index) {
if (result.result === 'FAIL') {
result.passed = false;
testOut.failedCount++;

var label = result.elementCount === 1 ? ' element ' : ' elements ';
result.output = '\n\t\t' + result.elementCount + label + 'failed:';

// match elements returned via promises
// by their failure codes
elementFailures.forEach(function(element, index) {
if (element.code === result.rule.code) {
result.output += '\n\t\t' + elementFailures[index].list;
}
});
result.output += '\n\n\t\t' + result.rule.url;
}
else {
result.passed = true;
result.output = '';
}
console.log(result.output);

testOut.specResults.push({
description: result.rule.heading,
assertions: [{
passed: result.passed,
errorMsg: result.output
}],
duration: 1
});
});

if ((testOut.failedCount > 0) || (testOut.specResults.length > 0)) {
return testOut;
}
});
});
}
}

// Export
exports.teardown = teardown;
5 changes: 5 additions & 0 deletions plugins/accessibility/spec/fail_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('check if accessibility plugin works on bad apps', function() {
it('should have accessibility problems on markup', function() {
browser.get('accessibility/badMarkup.html');
});
});
12 changes: 12 additions & 0 deletions plugins/accessibility/spec/failureConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var env = require('../../../spec/environment.js');

exports.config = {
seleniumAddress: env.seleniumAddress,
framework: 'jasmine2',
specs: ['fail_spec.js'],
baseUrl: env.baseUrl,
plugins: [{
chromeA11YDevTools: true,
path: '../index.js'
}]
};
12 changes: 12 additions & 0 deletions plugins/accessibility/spec/successConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var env = require('../../../spec/environment.js');

exports.config = {
seleniumAddress: env.seleniumAddress,
framework: 'jasmine2',
specs: ['success_spec.js'],
baseUrl: env.baseUrl,
plugins: [{
chromeA11YDevTools: true,
path: "../index.js"
}]
};
9 changes: 9 additions & 0 deletions plugins/accessibility/spec/success_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('accessibility', function() {
it('should get a file to test', function() {
browser.get('accessibility/index.html');

element.all(by.css('input')).then(function(inputs) {
expect(inputs.length).toEqual(2);
});
});
});
15 changes: 15 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,19 @@ executor.addCommandlineTest(
message: 'warning -- Module "xApp" was created but never loaded.'
}]);

// Check accessibility plugin

executor.addCommandlineTest(
'node lib/cli.js plugins/accessibility/spec/failureConfig.js')
.expectExitCode(1)
.expectErrors([{
message: '2 elements failed:'+
'\n\t\t<input ng-model="firstName" type="text" class="ng-pristine ng-valid ng-touched">'+
'\n\t\t<input ng-model="lastName" type="text" class="ng-pristine ng-untouched ng-valid">'
},
{
message: '1 element failed:'+
'\n\t\t<img src="http://example.com/img.jpg">'
}]);

executor.execute();
21 changes: 21 additions & 0 deletions testapp/accessibility/badMarkup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>

<html ng-app ng-hint>
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="../lib/angular_v1.3.13/angular.min.js"></script>
<script src="../lib/angular_v1.3.13/angular-aria.min.js"></script>
<script>
angular.module('xApp', []);
</script>
</head>
<body>
First name:<input ng-model="firstName" type="text"/>
<br>
Last name:<input ng-model="lastName" type="text"/>
<br>
Hello {{firstName}} {{lastName}}
<img src="http://example.com/img.jpg">
</body>
</html>
23 changes: 23 additions & 0 deletions testapp/accessibility/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>

<html ng-app="xApp">
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="../../lib/angular_v1.3.13/angular.min.js"></script>
<script src="../../lib/angular_v1.3.13/angular-aria.min.js"></script>
<script>
angular.module('xApp', []);
</script>
</head>
<body>
<label for="firstName">First name:</label>
<input ng-model="firstName" type="text" id="firstName" />
<br>
<label for="lastName">Last name:</label>
<input ng-model="lastName" type="text" id="lastName" />
<br>
Hello {{firstName}} {{lastName}}
<img src="http://example.com/img.jpg" alt="{{firstName}} {{lastName}}">
</body>
</html>
6 changes: 3 additions & 3 deletions testapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

<div>Angular seed app: v<span app-version></span></div>

<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular/angular-animate.min.js"></script>
<script src="lib/angular/angular-route.min.js"></script>
<script src="lib/angular_v1.3.13/angular.min.js"></script>
<script src="lib/angular_v1.3.13/angular-animate.min.js"></script>
<script src="lib/angular_v1.3.13/angular-route.min.js"></script>

<script src="components/app-version.js"></script>
<script src="async/async.js"></script>
Expand Down
8 changes: 8 additions & 0 deletions testapp/lib/angular_v1.3.13/angular-animate.min.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions testapp/lib/angular_v1.3.13/angular-route.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions testapp/lib/angular_v1.3.13/angular.min.js.map

Large diffs are not rendered by default.

0 comments on commit fedb131

Please sign in to comment.