test(a11y): alt 判据只盯漏标,取消「允许 2 张」兜底
旧实现把 `alt=""` 也计成「没有 alt」(getAttribute 返回空串即 falsy),再用 `toBeLessThanOrEqual(2)` 放行——首页影像版式落地后装饰图有十几张,这条用例 于是同时做两件错事:误报正确写法的装饰图,又给真正漏标的 <img> 留了 2 张额度。 现判据只覆盖机械可判的那一维:既无 alt 属性、又未用 aria-hidden/role 声明装饰性 的图片一律判红,额度 0。「信息图的 alt 语义是否正确」不在本用例的能力范围内, 由全站 axe 节点计数(npm run check:axe)承担。 4 project(chromium / chromium-mobile / firefox / webkit)首页用例全绿。
This commit is contained in:
1 parent
b125d16a2e
commit
3f11d7ee22
1 file changed
+13
-5
@@ -696,14 +696,22 @@ test.describe("无障碍性 - A11y 基础检查", { tag: "@regression" }, () =>
|
||||
const images = page.locator("img");
|
||||
const count = await images.count();
|
||||
|
||||
let imagesWithoutAlt = 0;
|
||||
// 2026-10-20:旧判据把 `alt=""` 也计成「没有 alt」,再用「允许 2 张」兜底。
|
||||
// 但 `alt=""` 正是装饰图的正确写法,于是这条用例一边误报装饰图、一边给真正
|
||||
// 漏标的 <img> 留了 2 张额度。现只盯漏标那一维:既无 alt 属性、又未用
|
||||
// aria-hidden/role 声明装饰性的图片一律判红,额度 0。「信息图的 alt 是否语义正确」
|
||||
// 机械上判不出来,由全站 axe 节点计数(npm run check:axe)承担。
|
||||
let informativeWithoutAlt = 0;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const alt = await images.nth(i).getAttribute("alt");
|
||||
if (!alt) imagesWithoutAlt++;
|
||||
const img = images.nth(i);
|
||||
const alt = await img.getAttribute("alt");
|
||||
const hidden = await img.getAttribute("aria-hidden");
|
||||
const role = await img.getAttribute("role");
|
||||
const decorative = hidden === "true" || role === "presentation";
|
||||
if (alt === null && !decorative) informativeWithoutAlt++;
|
||||
}
|
||||
|
||||
// 允许少量装饰性图片没有 alt(使用 alt="" 或 role="presentation")
|
||||
expect(imagesWithoutAlt).toBeLessThanOrEqual(2);
|
||||
expect(informativeWithoutAlt).toBe(0);
|
||||
});
|
||||
|
||||
test("Heading 层级结构合理", async ({ page }) => {
|
||||
|
||||
Reference in new issue
Block a user