System specs are the recommended and most reliable way to run accessibility checks in your Rails application. This guide shows you how to set up continuous accessibility testing using RSpec system specs.
✅ More Reliable - Runs in the same test environment as your other specs
✅ Faster - No need to wait for external server processes
✅ Better Integration - Works seamlessly with your existing test suite
✅ Automatic - Checks run automatically after each visit in system specs
✅ Clear Feedback - Detailed error messages with file locations and fix instructions
The generator creates spec/system/all_pages_accessibility_spec.rb which automatically tests all GET routes in your application with smart change detection. This spec only tests pages when their related files (views, controllers, helpers) have changed, making it fast and focused.
You can also create custom system specs for specific pages. Name them with _accessibility_spec.rb suffix for clarity:
# spec/system/my_page_accessibility_spec.rb
require 'rails_helper'
RSpec.describe 'My Page Accessibility', type: :system do
it 'loads the page and runs comprehensive accessibility checks' do
visit root_path
# Run comprehensive accessibility checks
# This will fail the test if any accessibility issues are found
check_comprehensive_accessibility
# ✅ If all checks pass, you'll see: "All comprehensive accessibility checks passed! (11 checks)"
end
endThe gem automatically runs comprehensive accessibility checks after each visit in system specs. You don't need to call check_comprehensive_accessibility manually unless you want to run checks at a specific point in your test.
The generator automatically adds a static accessibility scanner to your Procfile.dev:
web: bin/rails server
css: bin/rails dartsass:watch
a11y: bundle exec a11y_static_scannerThis provides fast, continuous feedback by scanning view files directly without browser rendering. The scanner:
- Scans all files on startup
- Only re-scans files that have changed
- Shows errors with exact file locations and line numbers
- Watches for file changes continuously
See the Getting Started Guide for more details on static scanner configuration.
This will run your accessibility specs every 30 seconds while you develop. The all_pages_accessibility_spec.rb uses smart change detection to only test pages when their related files change, making it fast and focused.
# spec/system/my_page_accessibility_spec.rb
require 'rails_helper'
RSpec.describe 'My Page Accessibility', type: :system do
it 'runs accessibility checks on the home page' do
visit root_path
# ✅ Comprehensive accessibility checks run automatically after this test!
# The test will fail if any accessibility issues are found
end
end# spec/system/pages_accessibility_spec.rb
require 'rails_helper'
RSpec.describe 'Pages Accessibility', type: :system do
it 'runs accessibility checks on home page' do
visit root_path
# Checks run automatically - test fails if issues found
end
it 'runs accessibility checks on about page' do
visit about_path
# Checks run automatically - test fails if issues found
end
it 'runs accessibility checks on contact page' do
visit contact_path
# Checks run automatically - test fails if issues found
end
end# spec/system/dashboard_accessibility_spec.rb
require 'rails_helper'
RSpec.describe 'Dashboard Accessibility', type: :system do
before do
user = FactoryBot.create(:user)
sign_in user
end
it 'runs accessibility checks on dashboard' do
visit dashboard_path
# ✅ Comprehensive accessibility checks run automatically after authentication!
# The test will fail if any accessibility issues are found
end
endit 'does something without accessibility checks', skip_a11y: true do
visit some_path
# Accessibility checks won't run for this test
endThe gem automatically runs 11 comprehensive accessibility checks:
- ✅ Form Labels - All form inputs have associated labels
- ✅ Image Alt Text - All images have descriptive alt attributes
- ✅ Interactive Elements - Buttons, links have accessible names
- ✅ Heading Hierarchy - Proper h1-h6 structure
- ✅ Keyboard Accessibility - All interactive elements keyboard accessible
- ✅ ARIA Landmarks - Proper use of ARIA landmark roles
- ✅ Form Error Associations - Errors linked to form fields
- ✅ Table Structure - Tables have proper headers
- ✅ Duplicate IDs - No duplicate ID attributes
- ✅ Skip Links - Skip navigation links present
- ✅ Color Contrast - Text meets contrast requirements (optional, disabled by default)
When all checks pass, you'll see:
✅ All comprehensive accessibility checks passed! (11 checks)
When issues are found, you get detailed, actionable errors:
======================================================================
❌ ACCESSIBILITY ERROR: Page missing H1 heading
======================================================================
📄 Page Being Tested:
URL: http://127.0.0.1:54384/
Path: /
📝 Likely View File: app/views/home/about.html.erb
📍 Element Details:
Tag: <page>
ID: (none)
Classes: (none)
Visible text: Page has no H1 heading
🔧 HOW TO FIX:
Add an <h1> heading to your page:
<h1>Main Page Title</h1>
Or in Rails ERB:
<h1><%= @page_title || 'Default Title' %></h1>
💡 Best Practice: Every page should have exactly one <h1>.
It should describe the main purpose of the page.
📖 WCAG Reference: https://www.w3.org/WAI/WCAG21/Understanding/
======================================================================
bundle exec rspec spec/system/*_accessibility_spec.rbbundle exec rspec spec/system/all_pages_accessibility_spec.rbbundle exec rspec spec/system/*_accessibility_spec.rb --format documentationAdd to your CI configuration:
# .github/workflows/ci.yml
- name: Run Accessibility Tests
run: bundle exec rspec spec/system/*_accessibility_spec.rb- Name your specs clearly - Use
_accessibility_spec.rbsuffix - Test critical paths - Focus on user-facing pages
- Keep specs simple - One page per spec is often enough
- Use Procfile.dev - For continuous testing during development
- Run in CI - Catch issues before they reach production
Make sure:
- Your spec has
type: :system - You call
visitin your test - The gem is properly configured in
spec/rails_helper.rb
The success message appears when all checks pass. If you don't see it, there may be silent failures. Check your RSpec output for any exceptions.
Disable color contrast checking in development:
# config/accessibility.yml
development:
checks:
color_contrast: false- See Getting Started Guide for initial setup
- See Continuous Integration Guide for CI/CD setup
- See Writing Accessible Views for best practices