Files
novalon-website/e2e/nav-keyboard.spec.ts
T
zhangxiang 1b5c75ab64 test(qa): 采用 Playwright Test Agents 做作者侧 AI + 补主导航键盘用例
选型:AI 只用在「写/修/探索」,运行期保持零模型、零外部服务。不引入
tester-army/e2e —— 它是第二套 runner,且与本仓最值钱的三块错配(无像素
基线可比、不模拟 touch/color-scheme/reduced-motion、另起 json/junit 产物
口径)。判据、对照表与边界见 docs/testing/agentic-test-authoring.md。

- init-agents 产物:.claude/agents/{planner,generator,healer}.md + .mcp.json
  指向仓内自带的 run-test-mcp-server(不装 @playwright/mcp;Playwright 自身
  不调 LLM,模型由宿主 loop 提供 ⇒ 无新增密钥依赖,也不外发应用内容)
- 故意不入库 init-agents 生成的 seed.spec.ts:空占位会被 test:functional
  的 4 个 project 当空用例收集
- specs/nav-keyboard-navigation.plan.md:7 个场景,3 个延后并写明原因
- e2e/nav-keyboard.spec.ts(tag @a11y,4 例):补「键盘打不开/走不出主导航
  下拉」。hover 路径早有覆盖,jsdom 单测替不了浏览器层的 relatedTarget 与
  真实焦点顺序;契约逐行取自 src/components/layout/mega-dropdown.tsx:35-76
- 实测读数:dev 四 project 10 passed / 6 skipped(EXIT=0);E2E_TARGET=
  production 10 passed / 2 skipped(EXIT=0);--workers=1 --repeat-each=3
  24/24;type-check 与 lint 均 0 error
- 测到并定性两条「非产品缺陷」:① WebKit 本机不响应真实 Tab 键(连续 4 次
  后 activeElement 仍是 BODY,而 .focus() 有效),与 N-27 同源;② dev 目标
  下触发器 DOM 节点在 hydration 后被整体替换(失败样本 click=1 而 aria 恒
  false,通过样本相反 ⇒ 监听绑在已被换掉的旧节点上),生产产物不复现
  ⇒ openWithKeyboard 用 toPass() 重发交互;跳过范围按引擎收窄,没有用守卫
  把红压掉
- 不在本提交:AGENTS.md 已被 .gitignore 排除,其「85 张基线未提交」的日期
  更正只落本地;该假事实的已入库更正记录在 docs/lessons-learned.md:505
2026-10-08 15:56:20 +08:00

93 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect, type Page } from '@playwright/test';
import { expectHydrated } from './hydrated';
// 契约来源(逐行核对):src/components/layout/mega-dropdown.tsx:35-46(Escape/blur 关闭)、
// :62-76(触发器是 <button onClick>,带 aria-haspopup/aria-expanded)、:94-178(面板仅 isOpen 时挂载)。
// hover 路径已由 e2e/nav-dropdown.spec.ts 覆盖;本 spec 只补「无指针设备」路径(验收 B-10 的浏览器层)。
// jsdom 单测 mega-dropdown.test.tsx:55-74 用 fireEvent 测过 Escape,但 relatedTarget 与真实焦点顺序
// 在 jsdom 里不成立,不能替代浏览器验证。
const NAV_SELECTOR = 'nav[aria-label="主导航"]';
function productTrigger(page: Page) {
return page.getByRole('navigation', { name: '主导航' }).getByRole('button', { name: '产品', exact: true });
}
function viewAllLink(page: Page) {
return page.getByRole('link', { name: '查看全部产品' });
}
async function openWithKeyboard(page: Page) {
const trigger = productTrigger(page);
await page.goto('/');
await expect(trigger).toBeVisible();
await expectHydrated(trigger);
// dev 目标下该按钮的 DOM 节点会在 hydration 后被整体替换(实测:失败样本里 click/keydown
// 计数为 1 但 aria-expanded 恒 false、焦点不在按钮上;通过样本反过来是 click=0 而 aria=true,
// 说明监听挂在已被换掉的旧节点上)。生产产物不复现,属 dev/ Fast Refresh 竞态,
// 与 e2e/primary-nav.ts:11-14 记录的是同一类窗口 ⇒ 按本仓既有写法重发**交互**而不只是重读断言。
await expect(async () => {
await trigger.press('Enter');
await expect(trigger).toHaveAttribute('aria-expanded', 'true', { timeout: 2000 });
}).toPass({ timeout: 30000 });
await expect(viewAllLink(page)).toBeVisible();
return trigger;
}
test.describe('主导航下拉的键盘可操作性', { tag: '@a11y' }, () => {
// 桌面 mega dropdown 在移动断点整体不渲染(chromium-mobile 实测 element(s) not found);
// 移动导航是另一个组件(抽屉),它的键盘契约另立 spec,不在本文件假装覆盖。
test.beforeEach(async ({ viewport }) => {
test.skip(!!viewport && viewport.width < 768, '移动断点不渲染桌面 mega dropdown');
});
// 依赖真实 Tab 遍历的两例在 webkit 引擎上跳过:本机实测(:3120 生产产物)连续 4 次 Tab 后
// document.activeElement 仍是 BODY,而元素 .focus() 生效且 matches(':focus') 为 true。
// 与 e2e/p4-performance-a11y.spec.ts:240 记录的 N-27 同源(macOS WebKit 未开「全键盘访问」),
// 属引擎/系统默认值差异而非本仓缺陷;主配置的 chromium-mobile 同为 webkit 引擎,一并覆盖。
test('Tab 到达触发器后可用 Enter 展开', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'WebKit 本机不响应真实 Tab 键(N-27,见上方注释)');
const trigger = productTrigger(page);
await page.goto('/');
await expect(trigger).toBeVisible();
await expectHydrated(trigger);
await expect(trigger).toHaveAttribute('aria-haspopup', 'true');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
// 焦点顺序实测(chromium + firefox,:3120 生产产物与 dev 目标读数一致):
// 第 1 站「跳转到主要内容」跳转链接,第 2 站 logo「返回首页」,第 3 站才是本触发器。
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await expect(trigger).toBeFocused();
await page.keyboard.press('Enter');
await expect(trigger).toHaveAttribute('aria-expanded', 'true');
await expect(viewAllLink(page)).toBeVisible();
});
test('Tab 焦点进入面板链接而不是跳过面板', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'WebKit 本机不响应真实 Tab 键(N-27,见上方注释)');
await openWithKeyboard(page);
await page.keyboard.press('Tab');
await expect(page.locator(`${NAV_SELECTOR} a[href^="/products/"]:focus`)).toHaveCount(1);
});
test('Escape 关闭面板并把焦点还给触发器', async ({ page }) => {
const trigger = await openWithKeyboard(page);
await page.keyboard.press('Escape');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
await expect(trigger).toBeFocused();
// 卸载断言而非隐藏断言:抓源码注释担心的「看不见但仍在 DOM 里」滞留态
await expect(viewAllLink(page)).toHaveCount(0);
});
test('焦点移出下拉容器即关闭', async ({ page }) => {
const trigger = await openWithKeyboard(page);
await viewAllLink(page).press('Tab');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
await expect(viewAllLink(page)).toHaveCount(0);
});
});