Accessibility (a11y)

Check public sites for accessibility issues, using Pa11y.

Pa11y checks against WCAG 2.1 level AA by default.

Dependencies

Install pa11y-ci and puppeteer. If using package.json:

pnpm add --save-dev pa11y-ci puppeteer

Configuration

Create a pa11y.default.js file for the default configuration, and a pa11y.mobile.js file for a mobile viewport. (Use the .cjs extension if package.json sets "type": "module".)

Default

The file reads environment variables to control behavior, and sets these options:

runners

Use both HTML CodeSniffer (htmlcs) and axe-core (axe).

levelCapWhenNeedsReview

By default, the axe runner reports issues that need manual review as errors. Cap these at warning, to reserve error for unambiguous issues.

includeWarnings

By default, Pa11y excludes warnings. The PA11Y_INCLUDE_WARNINGS environment variable controls this option.

hideElements and ignore

See Suppressions.

Suppressions

Pa11y CI fails if any issue is reported. Every issue must therefore be either fixed or suppressed.

Both runners report false positives. For example, they report insufficient contrast if an element’s text is placed on a background image.

To not report false positives, Pa11y can hide elements (hideElements) or ignore rules (ignore), but it can’t ignore a specific rule for a specific element. Either option can shadow a real issue:

  • hideElements hides the elements and their descendants, along with any other issues they have.

  • ignore ignores the rules on all elements, including any other elements added later.

To not miss real issues, we run Pa11y CI with each option. The hideElements and ignore options are set according to the PA11Y_STRATEGY environment variable. Set the PA11Y_SUPPRESS_KNOWN_WARNINGS environment variable to not report false positives in CI; omit it in development to review all warnings.

Each suppression in knownErrors and knownWarnings is a list of rules and the maximally narrow selectors to which they apply.

Sometimes, an element reports both a false positive and a real issue. Hiding the element would hide both. In that case, set no selectors, and comment why.

For example:

pa11y.default.js
const strategy = process.env.PA11Y_STRATEGY;
const includeWarnings = "PA11Y_INCLUDE_WARNINGS" in process.env;
const suppressKnownWarnings = "PA11Y_SUPPRESS_KNOWN_WARNINGS" in process.env;

const knownErrors = [
  {
    // "This form does not contain a submit button." (the submit button is inside a noscript element)
    // https://www.w3.org/WAI/WCAG21/Techniques/html/H32
    rules: ["WCAG2AA.Principle3.Guideline3_2.3_2_2.H32.2"],
    selectors: ["header form[method='post']"],
  },
];

const knownWarnings = [
  {
    // "This element's text is placed on a background image." (the real ratio is ~15:1, #212529 on #fff)
    // https://www.w3.org/WAI/WCAG21/Techniques/general/G18
    rules: ["WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.BgImage", "color-contrast"],
    selectors: ["select.form-select"],
  },
  {
    // "This element's text or background contains transparency." (the real ratio is ~5.2:1, #1d70b8 on #fff)
    // https://www.w3.org/WAI/WCAG21/Techniques/general/G18
    // These links can also report a "color-contrast" error from the axe runner.
    rules: ["WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.Alpha", "WCAG2AA.Principle1.Guideline1_4.1_4_3.G145.Alpha"],
    selectors: [],
  },
];

function createDefaults(extraKnownWarnings = []) {
  const suppressions = [
    ...knownErrors,
    ...(includeWarnings && suppressKnownWarnings ? [...knownWarnings, ...extraKnownWarnings] : []),
  ];

  const withoutSelectors = suppressions.filter((suppression) => !suppression.selectors.length);
  const withSelectors = suppressions.filter((suppression) => suppression.selectors.length);

  const hideElements =
    strategy === "hideElements" ? withSelectors.flatMap((suppression) => suppression.selectors) : [];
  const ignore = [
    ...withoutSelectors.flatMap((suppression) => suppression.rules),
    ...(strategy === "ignore" ? withSelectors.flatMap((suppression) => suppression.rules) : []),
  ];

  return {
    runners: ["htmlcs", "axe"],
    levelCapWhenNeedsReview: "warning",
    includeWarnings: includeWarnings,
    ...(hideElements.length ? { hideElements: hideElements.join(", ") } : {}),
    ...(ignore.length ? { ignore: ignore } : {}),
  };
}

module.exports = {
  createDefaults,
  defaults: createDefaults(),
};

URLs

If the site has a sitemap, use Pa11y CI’s --sitemap option, and set the urls key to cover templates that a sitemap excludes, like noindexed pages, search results and 404 pages.

Otherwise, set the urls key. Cover each template, each language, and the pages whose content exercises the most markup. For example:

module.exports = {
  // ...
  urls: [
    // The Medicine extension has examples, tables, schema and codelists.
    "http://127.0.0.1:8000/en/extensions/medicine/master/",
    "http://127.0.0.1:8000/es/extensions/medicine/master/",
  ],
};

Mobile

The mobile configuration reuses the default configuration:

pa11y.mobile.js
const base = require("./pa11y.default.js");

const knownWarnings = [];

module.exports = {
  ...base,
  defaults: {
    ...base.createDefaults(knownWarnings),
    viewport: {
      width: 320,
      height: 480,
      deviceScaleFactor: 2,
      isMobile: true,
    },
  },
};

Local checks

Serve the site, then check its URLs. Repeat each command with pa11y.mobile.js.

  1. Check for errors:

    env PA11Y_STRATEGY=ignore \
      pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml
    env PA11Y_STRATEGY=hideElements \
      pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml
    
  2. Review all warnings, with no suppression:

    env PA11Y_INCLUDE_WARNINGS=1 PA11Y_STRATEGY=ignore \
      pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml
    env PA11Y_INCLUDE_WARNINGS=1 PA11Y_STRATEGY=hideElements \
      pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml
    
  3. Fix the real issues. Add false positives to knownErrors and/or knownWarnings, and then repeat with PA11Y_SUPPRESS_KNOWN_WARNINGS=1, to check that nothing else is reported.

    Tip

    If an issue is hard to locate, validate the markup.

    Pa11y checks the DOM, not your source. For example, if there’s an unbalanced <a> tag in a translated string, the browser’s parser can create an <a> tag with a href attribute but no inner text, which is an accessibility issue.

To check individual URLs, pass them as arguments, without the --sitemap option:

env PA11Y_STRATEGY=ignore pnpm exec pa11y-ci -c pa11y.default.js http://127.0.0.1:8000/en/

Continuous integration

Create a .github/workflows/a11y.yml file, and adapt the template below.

  • Load fixtures, so that pages have content: a page with no content reports no issues.

  • Build and serve the site using its production configuration (NODE_ENV, DJANGO_ENV, collectstatic), because static file handling and minification differ from development.

  • Rewrite the URLs in the sitemap to the local server, using the --sitemap-find (-f) and --sitemap-replace (-r) options, because the sitemap contains URLs with the site’s canonical domain.

Run four checks, in this order:

  1. Errors, with pa11y.default.js, using the ignore strategy.

  2. Errors, with pa11y.mobile.js, using the ignore strategy.

  3. Warnings, with pa11y.default.js, using the ignore strategy.

  4. Warnings, with pa11y.default.js, using the hideElements strategy.

  5. Warnings, with pa11y.mobile.js, using the ignore strategy.

The errors commands run without PA11Y_SUPPRESS_KNOWN_WARNINGS, because a rule like color-contrast can report both errors (no review needed) and warnings (review needed), so suppressing its warnings would suppress its errors, too.

The errors commands use only the ignore strategy, because the warnings commands also report errors. The warnings command for the mobile viewport also uses ignore, because hideElements guards against an ignored rule shadowing a real issue, a risk that is rarely specific to a viewport.

name: Accessibility
on: [push, pull_request]
permissions:
  contents: read
jobs:
  build:
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
    runs-on: ubuntu-latest
    steps:
      - name: Disable AppArmor  # https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md
        shell: bash
        run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
      - uses: actions/checkout@v7
      - uses: pnpm/action-setup@v6
      - uses: actions/setup-node@v7
        with:
          node-version: 22
          cache: pnpm
      - uses: actions/setup-python@v7
        with:
          python-version-file: .python-version
          cache: pip
          cache-dependency-path: requirements*.txt
      - run: pnpm install --frozen-lockfile --ignore-scripts
      - run: pnpm exec puppeteer browsers install chrome
      - run: pip install -r requirements.txt
      - name: Build
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres
          NODE_ENV: production
        shell: bash
        run: |
          node build.mjs
          env DJANGO_ENV=production python manage.py collectstatic --noinput -v2
          python manage.py migrate
          python manage.py loaddata tests/fixtures/a11y.json
      - name: Serve
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres
          DJANGO_ENV: production
          SERVE_STATIC: 'True'
        shell: bash
        run: |
          python manage.py runserver &
          sleep 5
      - env:
          PA11Y_STRATEGY: ignore
        run: pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml -f https://SUBDOMAIN.open-contracting.org -r http://127.0.0.1:8000
      - env:
          PA11Y_STRATEGY: ignore
        run: pnpm exec pa11y-ci -c pa11y.mobile.js -s http://127.0.0.1:8000/sitemap.xml -f https://SUBDOMAIN.open-contracting.org -r http://127.0.0.1:8000
      - env:
          PA11Y_INCLUDE_WARNINGS: 1
          PA11Y_SUPPRESS_KNOWN_WARNINGS: 1
          PA11Y_STRATEGY: ignore
        run: pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml -f https://SUBDOMAIN.open-contracting.org -r http://127.0.0.1:8000
      - env:
          PA11Y_INCLUDE_WARNINGS: 1
          PA11Y_SUPPRESS_KNOWN_WARNINGS: 1
          PA11Y_STRATEGY: hideElements
        run: pnpm exec pa11y-ci -c pa11y.default.js -s http://127.0.0.1:8000/sitemap.xml -f https://SUBDOMAIN.open-contracting.org -r http://127.0.0.1:8000
      - env:
          PA11Y_INCLUDE_WARNINGS: 1
          PA11Y_SUPPRESS_KNOWN_WARNINGS: 1
          PA11Y_STRATEGY: ignore
        run: pnpm exec pa11y-ci -c pa11y.mobile.js -s http://127.0.0.1:8000/sitemap.xml -f https://SUBDOMAIN.open-contracting.org -r http://127.0.0.1:8000
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432/tcp