axe-phantom.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*global window, phantom */
  2. var PATH_TO_AXE = 'node_modules/axe-core/axe.min.js';
  3. var args = require('system').args;
  4. var fs = require('fs');
  5. var page = require('webpage').create();
  6. if (args.length < 2) {
  7. console.log('axe-phantomjs.js accepts 1 argument, the URL to test');
  8. phantom.exit(1);
  9. }
  10. console.log('Testing, please wait...');
  11. page.open(args[1], function(status) {
  12. // Check for page load success
  13. if (status !== 'success') {
  14. console.log('Unable to access network');
  15. return;
  16. }
  17. page.injectJs(PATH_TO_AXE);
  18. page.framesName.forEach(function(name) {
  19. page.switchToFrame(name);
  20. page.injectJs(PATH_TO_AXE);
  21. });
  22. page.switchToMainFrame();
  23. page.evaluateAsync(function() {
  24. /*global axe */
  25. axe.run({ include: ['#page'] }, function(err, results) {
  26. if (err) {
  27. throw err;
  28. }
  29. window.callPhantom(results);
  30. });
  31. });
  32. page.onCallback = function(msg) {
  33. if (args[2]) {
  34. fs.write(args[2], JSON.stringify(msg, null, ' '), 'w');
  35. } else {
  36. if (msg.violations.length) {
  37. console.log(JSON.stringify(msg.violations, null, ' '));
  38. } else {
  39. console.log('No violations found!');
  40. }
  41. }
  42. // NOTE: to fail the test when violations are found, uncomment the line below.
  43. // phantom.exit(msg.violations.length);
  44. phantom.exit(0);
  45. };
  46. });