-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integration with playwright? #143
Comments
Okay, there's several issues here. First, anything you pass to Playwright via So, I tried just basically copying the code from this library right into a codeawait injectAxe(page);
await page.evaluate(async () => {
await new Promise((resolve) => {
const script = document.createElement("script");
document.body.append(script);
script.type = "module";
script.src = "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
script.addEventListener("load", resolve);
});
const apcaRule: Rule = {
id: `color-contrast-apca-silver`,
impact: "serious",
selector: "p",
// matches: "color-contrast-matches",
metadata: {
description: `Ensures the contrast between foreground and background colors meets APCA silver level conformance minimums thresholds`,
help: "Elements must meet APCA conformance minimums thresholds",
helpUrl:
"https://readtech.org/ARC/tests/visual-readability-contrast/?tn=criterion",
},
all: [`color-contrast-apca-silver-conformance`],
tags: ["apca", "wcag3", `apca-silver`],
};
const apcaCheck: Check = {
id: `color-contrast-apca-silver-conformance`,
metadata: {
impact: "serious",
messages: {
pass: "Element has sufficient APCA silver level lightness contrast (Lc) of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Expected minimum APCA contrast of ${data.apcaThreshold}}",
fail: {
default:
"Element has insufficient APCA silver level contrast of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Expected minimum APCA lightness contrast of ${data.apcaThreshold}Lc",
increaseFont:
"Element has insufficient APCA silver level contrast of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Increase font size and/or font weight to meet APCA conformance minimums",
},
incomplete: "Unable to determine APCA lightness contrast (Lc)",
},
},
evaluate(node) {
const style = window.getComputedStyle(node);
const fontSize = style.getPropertyValue("font-size");
const fontWeight = style.getPropertyValue("font-weight");
const bgColor = axe.commons.color.getBackgroundColor(node);
const fgColor = axe.commons.color.getForegroundColor(
node,
false,
bgColor,
);
console.log(node, bgColor, fgColor, fontSize, fontWeight);
if (!bgColor || !fgColor || !fontSize || !fontWeight)
return undefined;
const toRGBA = (color) =>
`rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha})`;
const threshold = (fontSize) => {
const size = parseFloat(fontSize);
if (size >= 32) return 45;
if (size >= 16) return 60;
return 75;
};
const apcaContrast = Math.abs(
calcAPCA(toRGBA(fgColor), toRGBA(bgColor)),
);
const apcaThreshold = threshold(fontSize);
this.data({
fgColor: fgColor.toHexString(),
bgColor: bgColor.toHexString(),
fontSize: `${(parseFloat(fontSize) * (72 / 96)).toFixed(1)}pt (${parseFloat(fontSize)}px)`,
fontWeight,
apcaContrast: Math.round(apcaContrast * 100) / 100,
apcaThreshold,
messageKey: apcaThreshold === null ? "increaseFont" : "default",
});
return apcaThreshold ? apcaContrast >= apcaThreshold : false;
},
};
const config: Spec = {
checks: [apcaCheck],
rules: [apcaRule, { id: "color-contrast", enabled: false }],
};
await window.axe.configure(config);
await window.axe.run();
}); So, one thing that might help is if this const apcaScriptString = readFileSync(resolve("apca-check/apca-check.min.js"));
await page.evaluate(
(scriptString) => window.eval(scriptString),
scriptString
); But, even when I did the above monstrosity, I still ran into issues. It was at least able to register the rules and checks. Running Playwright in But I got no violations on a test where it definitely should've failed: a paragraph with white text and a light beige background. Note the Finally I forced it to So....!!! I've wasted way too much time on this, so I'm just giving up. Someone else is going to have to figure this out. I'll check color contrast manually with Lighthouse whenever I have the time. Or maybe I'll try moving to the Lighthouse CLI tool. |
I use https://www.npmjs.com/package/axe-playwright to run my tests, which requires you to do...
Perhaps it would be enough to export
rules
andchecks
from this library so we could use them.Edit 1
I tried to figure out how to get the rules and checks this plugin has registered back from axe core, like
axe.getRules()
oraxe._audit.data.checks
oraxe._audit.checks
, but I've had no luck.Edit 2
More info. I tried patching this package in my node_modules to return the rules and checks. When passing them to
axe-playwright
, I get the following error:Edit 3
I tried switching from
axe-playwright
to@axe-core/playwright
, and am still running into an issue:The text was updated successfully, but these errors were encountered: