Skip to content

Instantly share code, notes, and snippets.

@jaffamonkey
Last active July 12, 2021 23:57
Show Gist options
  • Save jaffamonkey/5b4b24bf673c555a038f768d86e11d78 to your computer and use it in GitHub Desktop.
Save jaffamonkey/5b4b24bf673c555a038f768d86e11d78 to your computer and use it in GitHub Desktop.
<!-- image -->
// An example of running Pa11y programmatically
'use strict';
var pa11y = require('pa11y');
var url = 'http://localhost/search';
// Create a test instance with some default options
var test = pa11y({
parameters: {
reporter: 'html'
},
// Log what's happening to the console
log: {
debug: console.log.bind(console),
error: console.error.bind(console),
info: console.log.bind(console)
},
beforeScript: function (page, options, next) {
var waitUntil = function (condition, retries, waitOver) {
page.evaluate(condition, function (error, result) {
if (result || retries < 1) {
waitOver();
} else {
retries -= 1;
setTimeout(function () {
waitUntil(condition, retries, waitOver);
}, 200);
}
});
};
page.evaluate(function () {
document.getElementById("edit-name").value = "testuser";
document.getElementById("edit-pass").value = "password";
document.getElementById("edit-submit").click();
}, function () {
waitUntil(function () {
// Wait until the login has been success and the /account page loaded
return window.location.href === 'http://localhost/account';
}, 20, function () {
// Redirect to the page test page
page.evaluate(function () {
window.location.href = url;
});
waitUntil(function () {
// Wait until the page has been loaded before running pa11y
return window.location.href === url;
}, 20, next);
});
});
}
});
// Test http://example.com/
test.run('localhost/user/login', function (error, result) {
if (error) {
return console.error(error.message);
}
console.log(result);
});
<!-- image -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment