Skip to content

Commit

Permalink
Add: Verify page option (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
andygout authored and rowanmanning committed Dec 15, 2016
1 parent 7b4c3f8 commit f432107
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -102,6 +102,7 @@ Usage: pa11y [options] <url>
-p, --port <port> the port to run PhantomJS on
-t, --timeout <ms> the timeout in milliseconds
-w, --wait <ms> the time to wait before running tests in milliseconds
-v, --verify-page <string> HTML string to verify is present in the page source HTML
-d, --debug output debug messages
-H, --htmlcs <url> the URL or path to source HTML_CodeSniffer from
-e, --phantomjs <path> the path to the phantomjs executable
Expand Down Expand Up @@ -549,6 +550,18 @@ pa11y({

Defaults to `0`.

### `verifyPage` (string)

HTML string to verify is present in the page source HTML. Could be used to ascertain that intended page is being tested (as opposed to error page) by using `<title>` tags and content (as below), or that a specific element is present.

```js
pa11y({
verifyPage: '<title>Nature Research: science journals, jobs, information and services.</title>'
});
```

Defaults to `null`.


Examples
--------
Expand Down Expand Up @@ -764,7 +777,7 @@ If you're opening issues related to these, please mention the version that the i
License
-------

Pa11y is licensed under the [Lesser General Public License (LGPL-3.0)][info-license].
Pa11y is licensed under the [Lesser General Public License (LGPL-3.0)][info-license].<br>
Copyright &copy; 2016, Springer Nature


Expand Down
7 changes: 6 additions & 1 deletion bin/pa11y
Expand Up @@ -63,6 +63,10 @@ function configureProgram(program) {
'-w, --wait <ms>',
'the time to wait before running tests in milliseconds'
)
.option(
'-v, --verify-page <string>',
'HTML string to verify is present in the page source HTML'
)
.option(
'-d, --debug',
'output debug messages'
Expand Down Expand Up @@ -123,7 +127,8 @@ function processOptions(program) {
rootElement: program.rootElement,
standard: program.standard,
timeout: program.timeout,
wait: program.wait
wait: program.wait,
verifyPage: program.verifyPage
});

if (!program.debug) {
Expand Down
7 changes: 7 additions & 0 deletions lib/inject.js
Expand Up @@ -3,6 +3,13 @@
/* eslint-disable max-statements */
function injectPa11y(window, options, done) {

if (options.verifyPage) {
var windowHtml = window.document.documentElement.outerHTML;
if (!windowHtml.match(new RegExp(options.verifyPage))) {
return reportError('Page not verified - HTML did not contain: "' + options.verifyPage + '"');
}
}

var messageTypeMap = {
1: 'error',
2: 'warning',
Expand Down
6 changes: 4 additions & 2 deletions lib/pa11y.js
Expand Up @@ -37,7 +37,8 @@ module.exports.defaults = {
rootElement: null,
standard: 'WCAG2AA',
allowedStandards: ['Section508', 'WCAG2A', 'WCAG2AA', 'WCAG2AAA'],
wait: 0
wait: 0,
verifyPage: null
};

function pa11y(options) {
Expand Down Expand Up @@ -141,7 +142,8 @@ function testPage(browser, page, options, done) {
ignore: options.ignore,
rootElement: options.rootElement,
standard: options.standard,
wait: options.wait
wait: options.wait,
verifyPage: options.verifyPage
}, next);
}

Expand Down
21 changes: 21 additions & 0 deletions test/unit/lib/inject.js
Expand Up @@ -541,5 +541,26 @@ describe('lib/inject', function() {
});
});

describe('verifyPage flag', function() {

it('should not error when verifyPage value exists in HTML', function(done) {
options.verifyPage = '<title>Foo</title>';
inject(window, options, function(result) {
assert.isUndefined(result.error);
done();
});
});

it('should error when verifyPage value does not exist in HTML', function(done) {
options.verifyPage = '<title>Bar</title>';
inject(window, options, function(result) {
assert.isDefined(result.error);
assert.strictEqual(result.error, 'Page not verified - HTML did not contain: "<title>Bar</title>"');
done();
});
});

});

});
/* eslint-enable one-var, max-len, max-statements */
1 change: 1 addition & 0 deletions test/unit/lib/pa11y.js
Expand Up @@ -340,6 +340,7 @@ describe('lib/pa11y', function() {
],
standard: 'Section508',
wait: 0,
verifyPage: null,
rootElement: null
});
assert.isFunction(phantom.mockPage.evaluate.firstCall.args[2]);
Expand Down
3 changes: 3 additions & 0 deletions test/unit/mock/window.js
Expand Up @@ -5,6 +5,9 @@ var sinon = require('sinon');
module.exports = {
callPhantom: sinon.spy(),
document: {
documentElement: {
outerHTML: '<title>Foo</title>'
},
querySelector: sinon.stub().returns(null),
querySelectorAll: sinon.stub().returns([])
},
Expand Down

0 comments on commit f432107

Please sign in to comment.