|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import { Audit, Logging, Rules } from "@siteimprove/alfa-test-utils"; |
| 3 | +import { Playwright } from "@siteimprove/alfa-playwright"; |
| 4 | +import fetch from "node-fetch"; |
| 5 | +import path from "path"; |
| 6 | +import fs from "fs"; |
| 7 | +import { storiesToTest } from "./stories-to-test.mjs"; |
| 8 | + |
| 9 | +const STORYBOOK_URL = "http://localhost:9200"; |
| 10 | + |
| 11 | +const reportDir = path.join(process.cwd(), "accessibility-reports"); |
| 12 | +if (!fs.existsSync(reportDir)) { |
| 13 | + fs.mkdirSync(reportDir, { recursive: true }); |
| 14 | +} |
| 15 | + |
| 16 | +const timestamp = new Date().toISOString().replace(/:/g, "-"); |
| 17 | + |
| 18 | +test.describe("Storybook Accessibility Tests with Siteimprove", () => { |
| 19 | + let storyIndex; |
| 20 | + |
| 21 | + test.beforeAll(async () => { |
| 22 | + try { |
| 23 | + const response = await fetch(`${STORYBOOK_URL}/index.json`); |
| 24 | + if (!response.ok) { |
| 25 | + throw new Error( |
| 26 | + `Failed to fetch storybook index: ${response.statusText}` |
| 27 | + ); |
| 28 | + } |
| 29 | + storyIndex = await response.json(); |
| 30 | + } catch (error) { |
| 31 | + console.error("Error fetching storybook index:", error); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + test( |
| 36 | + "Components should pass Siteimprove accessibility tests", |
| 37 | + async ({ browser, page }) => { |
| 38 | + if (!storyIndex) { |
| 39 | + console.error( |
| 40 | + "Skipping test because storybook index could not be fetched" |
| 41 | + ); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const accessibilityViolations = []; |
| 46 | + const reportFilePath = path.join( |
| 47 | + reportDir, |
| 48 | + `siteimprove-report-${timestamp}.json` |
| 49 | + ); |
| 50 | + |
| 51 | + const allResults = []; |
| 52 | + |
| 53 | + let count = 0; |
| 54 | + const stories = Object.entries(storyIndex.entries).filter(([key]) => |
| 55 | + storiesToTest.some(story => key.includes(story)) |
| 56 | + ); |
| 57 | + const totalStories = stories.length; |
| 58 | + |
| 59 | + for (const [storyId, story] of stories) { |
| 60 | + const encodedStoryId = encodeURIComponent(story.id); |
| 61 | + const storyUrl = `${STORYBOOK_URL}/iframe.html?id=${encodedStoryId}&viewMode=story`; |
| 62 | + |
| 63 | + count++; |
| 64 | + console.log(`Testing (${count}/${totalStories}): ${story.title}`); |
| 65 | + |
| 66 | + const context = await browser.newContext(); |
| 67 | + const page = await context.newPage(); |
| 68 | + |
| 69 | + try { |
| 70 | + await page.goto(storyUrl); |
| 71 | + |
| 72 | + const document = await page.evaluateHandle(() => window.document); |
| 73 | + const alfaPage = await Playwright.toPage(document); |
| 74 | + |
| 75 | + const alfaResult = await Audit.run(alfaPage, { |
| 76 | + rules: { include: Rules.wcag21aaFilter }, |
| 77 | + }); |
| 78 | + |
| 79 | + Logging.fromAudit(alfaResult).print(); |
| 80 | + |
| 81 | + const failingRules = alfaResult.resultAggregates.filter( |
| 82 | + aggregate => aggregate.failed > 0 |
| 83 | + ); |
| 84 | + |
| 85 | + const violations = |
| 86 | + Logging.fromAudit(alfaResult).toJSON().logs[1].logs; |
| 87 | + |
| 88 | + if (failingRules.size > 0) { |
| 89 | + console.log( |
| 90 | + `Found ${violations.length} violations in ${story.title}` |
| 91 | + ); |
| 92 | + |
| 93 | + const result = { |
| 94 | + component: story.title, |
| 95 | + storyId: storyId, |
| 96 | + url: storyUrl, |
| 97 | + violations: violations, |
| 98 | + }; |
| 99 | + |
| 100 | + accessibilityViolations.push(result); |
| 101 | + allResults.push(result); |
| 102 | + } else { |
| 103 | + allResults.push({ |
| 104 | + component: story.title, |
| 105 | + storyId: storyId, |
| 106 | + url: storyUrl, |
| 107 | + violations: [], |
| 108 | + }); |
| 109 | + } |
| 110 | + } catch (error) { |
| 111 | + console.error(`Error testing ${story.title}:`, error); |
| 112 | + accessibilityViolations.push({ |
| 113 | + component: story.title, |
| 114 | + storyId: storyId, |
| 115 | + error: error.message, |
| 116 | + }); |
| 117 | + |
| 118 | + allResults.push({ |
| 119 | + component: story.title, |
| 120 | + storyId: storyId, |
| 121 | + url: storyUrl, |
| 122 | + error: error.message, |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Save the full report as JSON only in local development |
| 128 | + if (!process.env.CI) { |
| 129 | + fs.writeFileSync(reportFilePath, JSON.stringify(allResults, null, 2)); |
| 130 | + console.log(`Saved detailed JSON report to: ${reportFilePath}`); |
| 131 | + } |
| 132 | + |
| 133 | + if (accessibilityViolations.length > 0) { |
| 134 | + console.error("Accessibility violations found:"); |
| 135 | + |
| 136 | + const totalViolations = accessibilityViolations.reduce( |
| 137 | + (acc, result) => |
| 138 | + acc + (result.violations ? result.violations.length : 0), |
| 139 | + 0 |
| 140 | + ); |
| 141 | + |
| 142 | + console.error( |
| 143 | + `\nTotal components with issues: ${accessibilityViolations.length}` |
| 144 | + ); |
| 145 | + console.error(`Total violations: ${totalViolations}`); |
| 146 | + |
| 147 | + expect( |
| 148 | + accessibilityViolations.length, |
| 149 | + `The test found ${accessibilityViolations.length} components with accessibility issues` |
| 150 | + ).toBe(0); |
| 151 | + } else { |
| 152 | + console.log("No accessibility violations found! 🎉"); |
| 153 | + } |
| 154 | + }, |
| 155 | + { timeout: 30000 } |
| 156 | + ); |
| 157 | +}); |
0 commit comments