DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Unit Testing Accessibility

Unit Testing Accessibility

You know that having automated test that guard against regressions always pays off in a long run.

Jacob Jedryszek user avatar by
Jacob Jedryszek
·
Sep. 22, 16 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
6.65K Views

Join the DZone community and get the full member experience.

Join For Free

in web accessibility hacker way , i mentioned that “only 20% of accessibility requirements can be verified by tools”. nevertheless, it is worth to cover this 20%. especially, when it is not very hard. you know that having automated test that guard against regressions always pays off in a long run.

as of today the best automatic verification tool for accessibility is axe .

there is an axe chrome plugin and an axe firefox plugin that enables you to run accessibility audit manually:

axe - results

running the automated tool manually is useful, but it is better to run it automatically as unit test and incorporate it into your continuous integration to run it automatically after every commit.

running accessibility audit with axe

you can install axe with npm:

npm i axe-core

the axe has a function a11ycheck that performs accessibility audit on specified html element. you may run it against widgets or partial views on your web app. that function takes 2 parameters:

  1. htmlelement to be audited
  2. callback function that is invoked with results parameter
axe.a11ycheck($("#myelement")[0], function (results) {
    expect(results.violations.length).tobe(0);
    results.violations.length && console.error(results.violations);
});

it is useful to print errors to console, as the results.violations is an array of nested objects with different properties. many of them are helpful to diagnose the issue.

axe - console errors

*a11y is abbreviation for accessibility (similar like i18n for internationalization), 11 is number of letters between ‘a’ and ‘y’

running axe with jasmine 2.x

in order to run axe with jasmine , you need to take into account that a11ycheck is asynchronous.

thus, you need to pass and invoke done function:

describe("a11y check", function() {
  it("has no accessbility violations (check console for errors)", function(done) {
    axe.a11ycheck($("#myelement")[0], function (results) {
        expect(results.violations.length).tobe(0);
        if (results.violations.length>0) {
            console.error(results.violations);
        }
        done();
    });
  });
});

running axe with qunit 2.x

it is similar in qunit . you also need to invoke done function, but first you need to get it by calling assert.async() :

qunit.test("a11y check", function(assert) {
    var done = assert.async();

    axe.a11ycheck($("#myelement")[0], function (results) {
        assert.strictequal(results.violations.length, 0, "there should be no a11y violations (check console for errors)");
        if (results.violations.length>0) {
            console.error(results.violations);
        }
        done();
    });
});

sample

i created a sample with button and input tag:

<div id="fixture">
  <button>my button</button> 
  <input type="text" />
</div>

this sample is not accessible because input tag does not have a label, and a11ycheck should report violations. the sample code, with jasmine and qunit tests, is available on github: axe-unittests .

summary

while automated accessibility unit tests are great, you probably still want to use axe plugin for chrome to investigate reported violations. it’s more convenient, and it has a neat user interface, while in unit tests you need to dig in into console errors.

when you start adding accessibility checks for different parts of your system, you may encounter many violations at first. it is better to still add tests, that ignore known violations, and then incrementally fix the issues. this approach prevents regressions while delaying adding test until all violations are fixed may cause introducing new ones while fixing others.

unit test

Published at DZone with permission of Jacob Jedryszek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Enough Already With ‘Event Streaming’
  • How to Test JavaScript Code in a Browser
  • 7 Traits of an Effective Software Asset Manager
  • Ultra-Fast Microservices: When Microstream Meets Wildfly

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo