import AxeBuilder from '@axe-core/playwright';
// Bootstrap is the only look (Material and Tailwind were removed in 0.2.0): the loops over
// THEMES keep the test names ("bootstrap dark") and run every check in both modes.
export const THEMES = ['bootstrap'];
export const MODES = ['light', 'dark'];
export const WCAG_TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa'];
// Opens the test page and waits until every [data-caelrik] component has fired
// caelrik:ready. Console errors are collected and returned.
export async function openComponents(page, {
path = '/', theme = 'bootstrap', mode = 'light', custom = false,
} = {}) {
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
page.on('console', (message) => {
if (message.type() === 'error') {
errors.push(`${message.text()} ${message.location().url}`.trim());
}
});
await page.addInitScript(() => {
window.caelrikReady = new Set();
document.addEventListener('caelrik:ready', (event) => window.caelrikReady.add(event.target));
});
await page.goto(`${path}?theme=${theme}&mode=${mode}${custom ? '&custom=1' : ''}`);
await page.waitForFunction(
() => window.caelrikReady.size === document.querySelectorAll('[data-caelrik]').length,
null,
{ timeout: 30_000 },
);
await settled(page);
return errors;
}
// Waits until the page's CSS animations and transitions (toasts, fades) have finished,
// instead of a fixed delay: axe checks contrast on what is painted, so a half-faded toast
// would fail it. Infinite animations (spinners) never finish and are ignored. Charts
// animate on a canvas, which axe and the DOM assertions do not read.
export async function settled(page) {
await page.waitForFunction(
() => document.getAnimations().every((animation) => animation.playState !== 'running'
|| animation.effect?.getComputedTiming().endTime === Infinity),
null,
{ timeout: 10_000 },
);
}
// Runs axe-core with color-contrast and returns readable violations, one string per node.
export async function axeViolations(page, { include = 'main', tags = WCAG_TAGS } = {}) {
const results = await new AxeBuilder({ page }).include(include).withTags(tags).analyze();
return results.violations.flatMap((violation) => violation.nodes.map(
(node) => `${violation.id}: ${node.target.join(' ')} (${node.failureSummary?.split('\n').slice(1).join(' ').trim()})`,
));
}
|