wcag-audit
npx skills add https://github.com/waseemkhan00777/wcag-audit --skill wcag-audit
Agent 安装分布
Skill 文档
WCAG 2.1 AA Accessibility Audit Skill
This skill provides comprehensive instructions for running automated WCAG 2.1 AA accessibility audits on web applications. It uses the wcag-ci-audit.js script which leverages Puppeteer and axe-core to test all application routes for accessibility compliance.
Introduction
WCAG (Web Content Accessibility Guidelines) 2.1 AA is an international standard for web accessibility. Compliance ensures that web applications are usable by people with disabilities, including those using screen readers, keyboard navigation, or other assistive technologies.
This skill enables automated testing of your application’s accessibility by:
- Extracting routes from your React Router configuration
- Testing each route with axe-core accessibility engine
- Reporting violations with specific rule IDs and remediation guidance
- Providing exit codes suitable for CI/CD integration
When to Use
Use this skill in the following scenarios:
- Before deploying to production – Ensure accessibility compliance before release
- During accessibility reviews – Regular audits as part of development workflow
- In CI/CD pipelines – Automated accessibility checks on every commit or PR
- When making UI changes – Verify new components and pages meet standards
- Before compliance audits – Prepare for formal accessibility assessments
- After dependency updates – Ensure UI library updates don’t introduce violations
Prerequisites
Before running the WCAG audit, ensure the following are in place:
Required Software
- Node.js – Version 14 or higher (check with
node --version) - Chrome/Chromium Browser – Required for Puppeteer
- Puppeteer can install Chrome automatically, or you can use an existing installation
- Set
PUPPETEER_EXECUTABLE_PATHif Chrome is in a non-standard location
Required Dependencies
The following npm packages must be installed (check package.json):
puppeteer: ^24.15.0 or compatible versionaxe-core: ^4.10.2 or compatible version
Install missing dependencies:
npm install --save-dev puppeteer axe-core
Required Files
wcag-ci-audit.js– The audit script must exist in the project rootsrc/App.tsx– React Router configuration file for route extraction- Must contain
<Route>components fromreact-router-dom - Script extracts
pathattributes from Route components
- Must contain
Application State
- Frontend application must be running (default:
http://localhost:8080)- The audit script needs a live application to test
- Start your dev server before running the audit
- Use
AUDIT_URLenvironment variable to specify a different URL
How to Run
Option 1: Using NPM Script (Recommended)
If your package.json includes the audit script:
npm run audit:wcag
Option 2: Direct Execution
Run the script directly with Node.js:
node wcag-ci-audit.js
Option 3: With Environment Variables
Customize the audit behavior with environment variables:
AUDIT_URL=http://localhost:3000 APP_FILE=src/routes.tsx node wcag-ci-audit.js
Configuration Options
The audit script supports the following environment variables:
AUDIT_URL
- Default:
http://localhost:8080 - Description: Base URL of the application to audit
- Example:
AUDIT_URL=http://localhost:3000 node wcag-ci-audit.js - Use case: Testing staging or production environments
APP_FILE
- Default:
src/App.tsx - Description: Path to the file containing React Router Route components
- Example:
APP_FILE=src/routes/index.tsx node wcag-ci-audit.js - Use case: Projects with non-standard route file locations
SKIP_DYNAMIC_ROUTES
- Default:
true(skips routes with parameters like:id) - Description: Whether to skip dynamic routes during audit
- Set to
false:SKIP_DYNAMIC_ROUTES=false node wcag-ci-audit.js - Note: Dynamic routes are replaced with placeholder values (e.g.,
/users/:idâ/users/test-id)
SKIP_CATCH_ALL
- Default:
true(skips catch-all routes like*) - Description: Whether to skip catch-all/404 routes
- Set to
false:SKIP_CATCH_ALL=false node wcag-ci-audit.js
PUPPETEER_EXECUTABLE_PATH
- Default: Auto-detected from common installation paths
- Description: Custom path to Chrome/Chromium executable
- Example:
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser node wcag-ci-audit.js - Use case: Custom Chrome installations or CI environments
Instructions for the Agent
When the user requests a WCAG audit, follow this workflow:
Step 1: Pre-flight Checks
Before running the audit, verify:
-
Script Exists: Check that
wcag-ci-audit.jsis present in the project roottest -f wcag-ci-audit.js || ls wcag-ci-audit.js -
Route File Exists: Verify
src/App.tsx(or configuredAPP_FILE) existstest -f src/App.tsx || ls src/App.tsx -
Dependencies Installed: Check
package.jsonforpuppeteerandaxe-core- If missing, suggest:
npm install --save-dev puppeteer axe-core
- If missing, suggest:
-
Frontend Running (if testing localhost):
- Check if dev server is running on the configured port
- If not, inform user they need to start the application first
- Suggest:
npm run devor appropriate start command
-
Chrome Available (optional check):
- Script will attempt to find Chrome automatically
- If issues occur, suggest:
npx puppeteer browsers install chrome
Step 2: Execution
Run the audit script:
npm run audit:wcag
Or directly:
node wcag-ci-audit.js
Monitor the output for:
- Route extraction success/failure
- Each page being audited
- Violation reports per page
- Final summary statistics
Step 3: Post-execution Analysis
After the audit completes:
-
Check Exit Code:
0= All pages passed (no violations)1= Violations found or errors occurred
-
Summarize Results:
- Total routes found
- Pages successfully audited
- Total violations count
- Pages with violations
-
Provide Actionable Feedback:
- List violation types found
- Reference specific rule IDs (e.g.,
color-contrast,aria-required-attr) - Suggest priority fixes (critical violations first)
- Link to axe-core documentation for detailed remediation
-
Next Steps:
- If violations found: Provide guidance on fixing common issues
- If audit passed: Confirm compliance and suggest periodic re-auditing
- For CI/CD: Explain how to integrate into pipeline
Step 4: Error Handling
Handle common errors appropriately:
Browser Launch Failures:
- Error: “Failed to launch browser”
- Solution: Install Chrome via
npx puppeteer browsers install chrome - Alternative: Set
PUPPETEER_EXECUTABLE_PATHto existing Chrome installation
Route Extraction Failures:
- Error: “Could not parse [APP_FILE]”
- Solution: Verify file exists and contains valid React Router
<Route>components - Check: File path matches
APP_FILEenvironment variable
Network/Connection Errors:
- Error: “Failed to audit [URL]”
- Solution: Verify frontend application is running
- Check:
AUDIT_URLmatches actual application URL - Verify: No firewall or network restrictions
Timeout Errors:
- Error: Page load timeout
- Solution: Application may be slow to load
- Consider: Increasing timeout in script (requires script modification)
Authentication Required:
- Observation: Pages redirect to login
- Note: This is expected for protected routes
- Consider: Providing test credentials or bypassing auth for audit
Troubleshooting
Browser Launch Issues
Problem: Script fails with “Failed to launch browser”
Solutions:
-
Install Chrome via Puppeteer:
npx puppeteer browsers install chrome -
Use existing Chrome installation:
export PUPPETEER_EXECUTABLE_PATH="/path/to/chrome" node wcag-ci-audit.js -
On Linux CI environments, may need:
apt-get install -y chromium-browser export PUPPETEER_EXECUTABLE_PATH="/usr/bin/chromium-browser"
Route Extraction Issues
Problem: “No routes found” or “Could not parse [file]”
Solutions:
- Verify
src/App.tsxexists and contains Route components - Check file format – must use JSX/TSX with
<Route path="...">syntax - If routes are in a different file, set
APP_FILEenvironment variable - Ensure React Router v6 syntax (not v5 or earlier)
Application Not Running
Problem: All pages fail with connection errors
Solutions:
-
Start the development server:
npm run dev -
Verify the URL matches
AUDIT_URL(default:http://localhost:8080) -
Check if application is running on a different port
-
For production/staging, set
AUDIT_URLto the correct domain
Authentication-Required Pages
Problem: Pages redirect to login or show authentication errors
Solutions:
- This is expected behavior for protected routes
- Consider creating a test user account for auditing
- Some violations may still be detectable on login pages
- For comprehensive testing, provide authentication credentials (requires script modification)
Missing Dependencies
Problem: “Cannot find module ‘puppeteer'” or “Cannot find module ‘axe-core'”
Solutions:
-
Install missing dependencies:
npm install --save-dev puppeteer axe-core -
Verify
package.jsonincludes these indevDependencies -
Run
npm installto ensure all dependencies are installed
Dynamic Routes Not Tested
Problem: Dynamic routes (with :id) are skipped
Solutions:
- This is default behavior to avoid testing invalid URLs
- To include dynamic routes:
SKIP_DYNAMIC_ROUTES=false node wcag-ci-audit.js - Note: Dynamic routes will use placeholder values (e.g.,
test-id)
Understanding Results
Exit Codes
- 0: Audit completed successfully with no violations
- 1: Violations found or errors occurred during audit
Output Format
The audit provides:
- Route Extraction: Lists all routes found in App.tsx
- Per-Page Results: For each route:
- Success/failure status
- Number of violations (if any)
- Violation details with rule IDs
- Summary: Total statistics including:
- Routes found
- Pages audited
- Pages skipped (errors)
- Total violations
Violation Information
Each violation includes:
- Rule ID: Unique identifier (e.g.,
color-contrast,aria-required-attr) - Description: Human-readable explanation (
helpfield) - Impact: Severity level (critical, serious, moderate, minor)
- Affected Elements: Count of DOM nodes with the violation
Common Violation Types
Color Contrast (color-contrast):
- Text doesn’t meet minimum contrast ratios
- Fix: Adjust text/background colors to meet WCAG AA (4.5:1 for normal text, 3:1 for large text)
ARIA Attributes (aria-required-attr, aria-valid-attr):
- Missing or invalid ARIA attributes
- Fix: Add proper ARIA labels, roles, and properties
Keyboard Navigation (keyboard, focus-order-semantics):
- Elements not keyboard accessible
- Fix: Ensure all interactive elements are focusable and have logical tab order
Image Alt Text (image-alt):
- Images missing alternative text
- Fix: Add descriptive
altattributes to all images
Heading Structure (heading-order, page-has-heading-one):
- Incorrect heading hierarchy
- Fix: Use proper h1-h6 hierarchy, one h1 per page
CI/CD Integration
GitHub Actions
Example workflow for running WCAG audits on every PR:
name: WCAG Accessibility Audit
on:
pull_request:
branches: [main, develop]
push:
branches: [main]
jobs:
wcag-audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Start application
run: |
npm run preview &
sleep 10
env:
PORT: 8080
- name: Run WCAG audit
run: npm run audit:wcag
env:
AUDIT_URL: http://localhost:8080
SKIP_DYNAMIC_ROUTES: false
GitLab CI
Example .gitlab-ci.yml configuration:
wcag-audit:
stage: test
image: node:18
before_script:
- npm ci
- npm run build
- npm run preview &
- sleep 10
script:
- npm run audit:wcag
environment:
AUDIT_URL: http://localhost:8080
artifacts:
when: always
reports:
junit: wcag-report.xml
expire_in: 1 week
Exit Code Handling
Both CI systems will fail the build if the audit script exits with code 1:
- Success (exit 0): Build continues
- Failure (exit 1): Build fails, PR cannot be merged
To allow warnings without failing builds, you would need to modify the script to differentiate between errors and violations.
Best Practices
When to Run Audits
- Before Committing UI Changes: Catch violations early
- In Pre-commit Hooks: Automate accessibility checks
- In CI/CD Pipeline: Prevent violations from reaching production
- Before Major Releases: Comprehensive accessibility review
- After Dependency Updates: Ensure UI libraries don’t introduce violations
Fixing Violations Systematically
-
Prioritize by Impact:
- Critical violations first (blocking keyboard navigation, missing alt text)
- Serious violations next (color contrast, ARIA issues)
- Moderate/minor violations last
-
Fix by Component:
- Group violations by component or page
- Fix all violations in one component before moving to the next
-
Test Incrementally:
- Run audit after fixing each major violation type
- Verify fixes don’t introduce new violations
-
Document Exceptions:
- If a violation cannot be fixed (e.g., third-party component), document why
- Consider alternative solutions or workarounds
Manual Testing Recommendations
Automated testing catches many issues, but manual testing is also important:
- Screen Reader Testing: Test with NVDA, JAWS, or VoiceOver
- Keyboard Navigation: Navigate entire application using only keyboard
- Browser Zoom: Test at 200% zoom level
- High Contrast Mode: Verify readability in high contrast mode
- Color Blindness: Use simulators to check color-dependent information
Integration with Development Workflow
- Pre-commit Hooks: Run quick audits before commits
- PR Requirements: Make WCAG audit passing a PR requirement
- Regular Full Audits: Schedule comprehensive audits monthly or quarterly
- Accessibility Training: Educate team on common violations and fixes