Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Accessibility tests to @axe-core/puppeteer #3522

Merged
merged 10 commits into from
Apr 21, 2023
Merged

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Apr 18, 2023

This PR closes:

All components + examples are now tested in axe-core@4.7.0 via Puppeteer

The tests run locally via npm test or separately with:

npx jest --selectProjects "Accessibility tests"

Colour contrast checks are built in ✅

Known issues

This switch has flagged a few known issues which are now ignored + documented, including some tests with positive tabindex and "landmark-inside-landmark" issues due to preview HTML

Test reports

I've kept the jest-axe reporter but generated the report using @axe-core/puppeteer instead

GitHub Actions will log failures as below:

"Elements must meet minimum color contrast ratio thresholds (color-contrast)"

Fix any of the following:
  Element has insufficient color contrast of 2.03 (foreground color: #aaa9a9, background color: #eeefef, font size: 12.0pt (16px), font weight: bold). Expected contrast ratio of 4.5:1

You can find more information on this issue here:
https://dequeuniversity.com/rules/axe/4.7/color-contrast?application=axe-puppeteer

Test workflow

Test progress appears on the GitHub Actions "Checks" tab (see example Accessibility tests)

Accessibility tests in GitHub Actions workflow

@colinrotherham colinrotherham added documentation User requests new documentation or improvements to existing documentation accessibility tooling labels Apr 18, 2023
@colinrotherham colinrotherham requested a review from a team as a code owner April 18, 2023 12:07
@colinrotherham colinrotherham added the dependencies Pull requests that update a dependency file label Apr 18, 2023
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 18, 2023 12:08 Inactive
@colinrotherham
Copy link
Contributor Author

We were previously on axe-core@4.5.1 which is bundled with jest-axe

<div class="govuk-width-container">
{% block beforeContent %}{% endblock %}
</div>
<main class="govuk-main-wrapper {{ mainClasses }}" id="main-content" role="main">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created full-width.njk (without wrapping landmarks) alongside full-width-landmarks.njk

This is so Axe can pass some of the "landmark-in-landmark" violations

You'll see some Percy related diff changes as a result

Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ace @colinrotherham, thanks! Seems like a sensible approach, and great commit structure.

I think it makes sense to keep a accessibility.test.mjs for each component whilst we have individual rule exceptions, but is there any other reason for doing it that way? Wondering if in the future we might move to having this in a single place, like we have for HTML validation.

Can we make sure we add the accessibility tests to the required status checks for the main branch after this is merged?

@colinrotherham
Copy link
Contributor Author

Thanks @36degrees 🙌

Ahh yep, it's to align with Running individual tests in testing-and-linting.md

The entire test suite is quite slow, so if you're only working on one component you can narrow things down:

Run Accordion tests only

Total time: 5s

npx jest --watch src/govuk/components/accordion

You can try npx jest --watch src/govuk/components/accordion --listFiles to see what's matched:

src/govuk/components/accordion/accessibility.test.mjs
src/govuk/components/accordion/accordion.test.js
src/govuk/components/accordion/template.test.js

Run Accessibility tests only

Total time: 55s

npx jest --selectProjects "Accessibility tests"

Run all non-build tests

Total time: 1m 20s (previously 25s)

npm test

Running npx jest will include build tasks but dist and package directories need building first

The added test time was a concern but well worth it

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 19, 2023 10:18 Inactive
@36degrees
Copy link
Contributor

That makes sense, thanks for explaining 👍🏻

@colinrotherham
Copy link
Contributor Author

colinrotherham commented Apr 19, 2023

Can we make sure we add the accessibility tests to the required status checks for the main branch after this is merged?

@36degrees Great idea

I've just fixed the conflicts and pushed again

Some extra comments added to jest.config.mjs plus maxWorkers: '50%' to ensure Jest uses the same default across single project runs, multi-project runs, and with flags like --watch and --watchAll

Addresses a minor concern that 12x concurrent Chromium processes on my machine was a bit much!

npx jest --selectProjects "Accessibility tests"

Support for macOS ARM CPUs

Out of interest, but performance related, can you test this for me? I've got an Intel Mac

Enabling the ARM CPU experiment to see how much faster things run:

See config puppeteer.config.js

module.exports = {
  cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
  experiments: {
    macArmChromiumEnabled: true
  }
}

Thanks

Update From @domoscargin: "Accessibility tests" total time 40% reduced to 24 seconds (~30% for all tests)

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 19, 2023 11:17 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 19, 2023 15:39 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 19, 2023 15:41 Inactive
@colinrotherham
Copy link
Contributor Author

colinrotherham commented Apr 19, 2023

Thought it best to push more commits to explain how to see these tests in action

Since our headless browser tests can be made visible with { headless: false } I've added:

  1. Documentation for jestPuppeteer.debug() to pause tests
  2. Jest config changes to only open 1x browser window
  3. Jest teardown changes to keep browsers open

Update: With video demo

Radio.button.accessibility.tests.mp4

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 20, 2023 06:36 Inactive
Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits look good to me, I'm just not sure about the documentation.

@@ -76,6 +76,8 @@ We write functional tests for every component to check the output of our Nunjuck

If a component uses JavaScript, we also write functional tests in a `[component name].test.js` file, for example [checkboxes.test.js](../../src/govuk/components/checkboxes/checkboxes.test.js). These component tests check that interactions, such as a mouse click, have the expected result.

When running tests in [headless Chrome](https://developers.google.com/web/updates/2017/04/headless-chrome) it's useful to configure [`{ headless: false }`](../../jest-puppeteer.config.js) and run `await jestPuppeteer.debug()` from the test. This pauses an open browser for inspection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit backwards to me as it's not clear when you'd want to use it when you start reading the sentence. Would it make sense to flip it? Maybe something like:

If you want to inspect a test that's running in the browser, configure Jest Puppeteer to use non-headless mode by setting headless: false in the config and then use Jest Puppeteer's debug mode to pause the test execution.

Not blocking – just a suggestion.

Also, it might be nice to be able to change mode without editing the config – the default config seems to allow changing it using a HEADLESS env variable. Could we do similar? (Again, not-blocking, could be a follow up PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fab let's do it

This makes “headful” browser inspection possible by preventing:

1. Multiple browser windows opening concurrently
2. Tests from closing down browsers after they complete
Includes tip regarding `await jestPuppeteer.debug()` with ESLint config changes to allow `jestPuppeteer` as a known global
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3522 April 21, 2023 16:35 Inactive
@colinrotherham
Copy link
Contributor Author

I've made the suggested documentation change but with HEADLESS=false instead of { headless: false }

Branch protection settings also have Accessibility tests added 👍

(Other open PRs will need updating to include the new tests)

@colinrotherham colinrotherham merged commit 6233eaa into main Apr 21, 2023
@colinrotherham colinrotherham deleted the axe-puppeteer branch April 21, 2023 16:41
dav-idc added a commit to alphagov/govuk-design-system that referenced this pull request May 17, 2023
These updates reflect the changes to our automated testing processes. These can be explored in detail in the PR: alphagov/govuk-frontend#3522

- Added reference to @axe-core/puppeteer
- Removed notice about known limitations of our automated testing setup
- Added section explaining 4 of the recent updates we've made
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility dependencies Pull requests that update a dependency file documentation User requests new documentation or improvements to existing documentation tooling
Projects
Development

Successfully merging this pull request may close these issues.

Improve our use of automated accessibility tools
3 participants