/** * Accessibilità automatica con axe-core (brief §11.3): tutte le pagine (lette dalla sitemap) * a 1440 e 390 px, più gli stati interattivi (menu mobile e sottomenu aperti, mappa caricata). * Criterio: zero violazioni «serious» o «critical» (WCAG 2.0/2.1 A e AA e buone pratiche); * le violazioni minori sono comunque riportate nel messaggio d'errore e nelle annotazioni. */ import AxeBuilder from '@axe-core/playwright'; import { expect, test, type Page } from '@playwright/test'; import { ALL_PAGES, stubGoogle } from '../helpers/site'; const VIEWPORTS = [ { name: 'desktop', width: 1440, height: 900 }, { name: 'mobile', width: 390, height: 844 }, ]; const TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'best-practice']; async function audit(page: Page, where: string, include?: string, exclude?: string) { let builder = new AxeBuilder({ page }).withTags(TAGS); if (include) builder = builder.include(include); if (exclude) builder = builder.exclude(exclude); const results = await builder.analyze(); const blocking = results.violations.filter( (v) => v.impact === 'serious' || v.impact === 'critical', ); const report = results.violations.map( (v) => `[${v.impact}] ${v.id}: ${v.help} — ${v.nodes .slice(0, 5) .map((n) => n.target.join(' ')) .join(' | ')}`, ); if (report.length > 0) { test.info().annotations.push({ type: 'axe', description: `${where}\n${report.join('\n')}` }); } expect(blocking, `violazioni gravi di accessibilità in ${where}:\n${report.join('\n')}`).toEqual( [], ); } for (const vp of VIEWPORTS) { test.describe(`${vp.name} ${vp.width}px`, () => { test.use({ viewport: { width: vp.width, height: vp.height } }); for (const path of ALL_PAGES) { test(`axe: ${path}`, async ({ page }) => { await page.goto(path, { waitUntil: 'networkidle' }); await audit(page, `${path} a ${vp.width}px`); }); } }); } test.describe('stati interattivi', () => { test('menu mobile aperto con sottomenu', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await page.goto('/'); await page.locator('[data-menu-open]').click(); await page.locator('#menu-mobile [data-submenu-toggle]').click(); await audit(page, 'menu mobile aperto', '#menu-mobile'); }); test('sottomenu desktop aperto', async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); await page.goto('/'); await page.locator('.nav [data-submenu-toggle]').click(); await audit(page, 'sottomenu desktop aperto', 'header'); }); test('mappa caricata', async ({ page }) => { await stubGoogle(page); await page.goto('/contatti/'); await page.locator('[data-map-load]').first().click(); await expect(page.locator('[data-map-consent] iframe')).toHaveCount(1); // il contenuto dell'iframe è di Google (terze parti): si verifica solo il nostro markup await audit(page, 'mappa caricata', '[data-map-consent]', '[data-map-consent] iframe'); }); });