Automating accessibility tests: running a check every time a page is rendered

91 views
Skip to first unread message

Kevin

unread,
Apr 19, 2021, 9:22:24 AM4/19/21
to Capybara
Hi there, and thank you for Capybara, which I've been using daily for many years now!

Inspired by an accessibility talk at the 2021 RailsConf, I recently started looking into ways to automatically run basic accessibility checks in my existing test suite. This work resulted in a writeup describing a quick way to accomplish a11y testing of Ruby on Rails apps with Rspec and Capybara: https://github.com/KevinBongart/automated_accessibility_testing_with_ruby_on_rails/#-automated-accessibility-testing--with-ruby-on-rails

My next step in making accessibility testing as easy and standard as possible is to run accessibility checks on every single page that is rendered in the test suite, and I'd love some pointers from Capybara experts to figure out how to integrate new (optional) logic with Capybara.

Ideally, the setup would be:
  1. Add a gem to the Rails app, add "require capybara-accessibility" or something in the rails_helper.rb file
  2. Every time Capybara sees a page (whether it's after a `visit`, a `click`, any `browser.navigate`, or even when a page element changes without a full reload), it runs the accessibility checks with `axe-core-capybara`
  3. Accessibility failures are logged as warnings, maybe this can be configured to be spec failures
  4. Adding "check_accessibility: false" to the example skips the a11y checks for the example
That's the ideal setup, but to get there I'm looking for callbacks or hooks or something that could trigger these checks without requiring the developer to manually add "expect(page).to be_axe_clean". Unfortunately I looked into the capybara code and from what I gathered (which is possibly not accurate!), I might need to monkey-patch some of it to make it work.

Any pointers to running code after each "visit" and each page change would be truly appreciated!

Thank you,

Kevin

Thomas Walpole

unread,
Apr 19, 2021, 1:17:47 PM4/19/21
to Capybara
Capybara doesn't really know anything about what the browser is doing, so knowing "something" changed really isn't possible unless you tell Capybara to look for that thing.  For `visit` you could prepend on to the session and wrap `visit` but note that `visit` is not guaranteed to be synchronous, so there is no guarantee the page has loaded when it returns.
Reply all
Reply to author
Forward
0 new messages