#!/bin/bash # ===================================================================== # pa11y-test.sh - Test for WCAG 2.1 AAA compliance using Pa11y # ===================================================================== # This script runs Pa11y against all pages of the website to check for # WCAG 2.1 AAA compliance. # ===================================================================== set -e # Check if base URL is provided if [ -z "$1" ]; then BASE_URL="http://localhost:8080" else BASE_URL="$1" fi # Create reports directory if it doesn't exist REPORTS_DIR="$(dirname "$0")/../reports" mkdir -p "$REPORTS_DIR" # List of pages to test PAGES=( "/" "/stories/" "/stories/open-source-success.html" "/stories/viperwire.html" "/one-pager-tools/csv-tool.html" ) echo "=== Testing WCAG 2.1 AAA Compliance with Pa11y ===" echo "Using base URL: $BASE_URL" # Function to run Pa11y on a single page run_pa11y() { local page="$1" local url="${BASE_URL}${page}" local filename=$(echo "$page" | sed 's/\//-/g' | sed 's/^-//' | sed 's/-$//') if [ -z "$filename" ]; then filename="index" fi echo "Testing $url..." # Run Pa11y with WCAG 2.1 AAA standard if command -v pa11y &> /dev/null; then pa11y --standard WCAG2AAA --reporter json "$url" > "$REPORTS_DIR/pa11y-$filename.json" || true # Count issues issues=$(jq 'length' "$REPORTS_DIR/pa11y-$filename.json") echo "Found $issues issues on $url" # Show summary of issues if [ "$issues" -gt 0 ]; then echo "Issues summary:" jq -r '.[] | "- " + .type + ": " + .message' "$REPORTS_DIR/pa11y-$filename.json" | head -n 5 if [ "$issues" -gt 5 ]; then echo "... and $((issues - 5)) more issues." fi fi return $issues else echo "Pa11y not installed. Install with: npm install -g pa11y" return 1 fi } # Run Pa11y on all pages total_issues=0 failed_pages=0 for page in "${PAGES[@]}"; do run_pa11y "$page" issues=$? if [ "$issues" -gt 0 ]; then failed_pages=$((failed_pages + 1)) total_issues=$((total_issues + issues)) fi echo "---" done # Create summary report cat > "$REPORTS_DIR/pa11y-summary.json" << EOL { "timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")", "baseUrl": "$BASE_URL", "summary": { "totalPages": ${#PAGES[@]}, "failedPages": $failed_pages, "totalIssues": $total_issues } } EOL # Print summary echo "=== Pa11y Test Summary ===" echo "Total pages tested: ${#PAGES[@]}" echo "Pages with issues: $failed_pages" echo "Total issues found: $total_issues" if [ "$total_issues" -gt 0 ]; then echo "=== Pa11y Tests Failed ===" exit 1 else echo "=== All Pa11y Tests Passed ===" exit 0 fi