|
| 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 path from "path"; |
| 5 | +import fs from "fs"; |
| 6 | +import { storiesToTest } from "./stories-to-test.mjs"; |
| 7 | +import detailPageJsonData from "../__mocks__/data/degree-search-detail.json" assert { type: "json" }; |
| 8 | + |
| 9 | +const STORYBOOK_URL = "http://localhost:9010"; |
| 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 | + |
| 20 | + test(`Component ${storiesToTest[0]} should pass accessibility tests`, async ({ page }) => { |
| 21 | + await page.route("**/api/**", route => { |
| 22 | + route.fulfill({ |
| 23 | + status: 200, |
| 24 | + contentType: "application/json", |
| 25 | + body: JSON.stringify(detailPageJsonData), |
| 26 | + }); |
| 27 | + }); |
| 28 | + const storyId = storiesToTest[0]; |
| 29 | + const encodedStoryId = encodeURIComponent(storyId); |
| 30 | + const storyUrl = `${STORYBOOK_URL}/iframe.html?id=${encodedStoryId}&viewMode=story`; |
| 31 | + |
| 32 | + await page.goto(storyUrl); |
| 33 | + await page.waitForTimeout(2000); |
| 34 | + |
| 35 | + const document = await page.evaluateHandle(() => window.document); |
| 36 | + const alfaPage = await Playwright.toPage(document); |
| 37 | + |
| 38 | + const alfaResult = await Audit.run(alfaPage, { |
| 39 | + rules: { include: Rules.wcag21aaFilter }, |
| 40 | + }); |
| 41 | + |
| 42 | + await page.screenshot({ |
| 43 | + fullPage: true, |
| 44 | + path: path.join(reportDir, `screenshot-${storyId}-${timestamp}.png`), |
| 45 | + }); |
| 46 | + |
| 47 | + Logging.fromAudit(alfaResult).print(); |
| 48 | + |
| 49 | + const failingRules = alfaResult.resultAggregates.filter( |
| 50 | + aggregate => aggregate.failed > 0 |
| 51 | + ); |
| 52 | + |
| 53 | + if (!process.env.CI) { |
| 54 | + const reportFilePath = path.join(reportDir, `siteimprove-report-${storyId}-${timestamp}.json`); |
| 55 | + fs.writeFileSync( |
| 56 | + reportFilePath, |
| 57 | + JSON.stringify(Logging.fromAudit(alfaResult).toJSON(), null, 2) |
| 58 | + ); |
| 59 | + console.log(`Saved detailed JSON report to: ${reportFilePath}`); |
| 60 | + } |
| 61 | + |
| 62 | + expect(failingRules.size, |
| 63 | + `Found ${failingRules.size} accessibility rule violations in ${storyId}` |
| 64 | + ).toBe(0); |
| 65 | + }); |
| 66 | + |
| 67 | + test(`Component ${storiesToTest[1]} should pass accessibility tests`, async ({ page }) => { |
| 68 | + // await page.route("**/api/**", route => { |
| 69 | + // route.fulfill({ |
| 70 | + // status: 200, |
| 71 | + // contentType: "application/json", |
| 72 | + // body: JSON.stringify(searchPageJsonData), |
| 73 | + // }) |
| 74 | + // }); |
| 75 | + const storyId = storiesToTest[1]; |
| 76 | + const encodedStoryId = encodeURIComponent(storyId); |
| 77 | + const storyUrl = `${STORYBOOK_URL}/iframe.html?id=${encodedStoryId}&viewMode=story`; |
| 78 | + |
| 79 | + await page.goto(storyUrl); |
| 80 | + await page.waitForTimeout(2000); |
| 81 | + |
| 82 | + const document = await page.evaluateHandle(() => window.document); |
| 83 | + const alfaPage = await Playwright.toPage(document); |
| 84 | + |
| 85 | + const alfaResult = await Audit.run(alfaPage, { |
| 86 | + rules: { include: Rules.wcag21aaFilter }, |
| 87 | + }); |
| 88 | + |
| 89 | + await page.screenshot({ |
| 90 | + fullPage: true, |
| 91 | + path: path.join(reportDir, `screenshot-${storyId}-${timestamp}.png`), |
| 92 | + }); |
| 93 | + |
| 94 | + Logging.fromAudit(alfaResult).print(); |
| 95 | + |
| 96 | + const failingRules = alfaResult.resultAggregates.filter( |
| 97 | + aggregate => aggregate.failed > 0 |
| 98 | + ); |
| 99 | + |
| 100 | + if (!process.env.CI) { |
| 101 | + const reportFilePath = path.join(reportDir, `siteimprove-report-${storyId}-${timestamp}.json`); |
| 102 | + fs.writeFileSync( |
| 103 | + reportFilePath, |
| 104 | + JSON.stringify(Logging.fromAudit(alfaResult).toJSON(), null, 2) |
| 105 | + ); |
| 106 | + console.log(`Saved detailed JSON report to: ${reportFilePath}`); |
| 107 | + } |
| 108 | + |
| 109 | + expect(failingRules.size, |
| 110 | + `Found ${failingRules.size} accessibility rule violations in ${storyId}` |
| 111 | + ).toBe(0); |
| 112 | + }); |
| 113 | +}); |
0 commit comments