test(visual): 截图前等显影落定,dwell 120ms→260ms 并排两帧
旧 helper 只靠固定 120ms 步长等 whileInView 提交,4 worker 抢 CPU 时中间某屏会被 IO 回调漏掉,截图留下一条 opacity:0 的空白带(lessons §5.51 签名 1)。这是装配竞态, 不该靠「多等一会儿」赌,也不该把整层钉成 --workers=1 用串行遮掉。 - 每步 dwell 提到 260ms 并排两帧(rAF×2)后再进下一屏。 - 滚动结束后等**状态**而不是时长:`main` 下 inline `opacity:0` 且仍有高度的元素必须 归零;8s 超时则把前 5 个 offender 的标签写进错误,让红按名字出现。 判据用 inline style 而非计算样式——framer 未开始时写 `opacity:0`、完成后写 `1`, 两者只差这一笔;同时排除 display:none 与零高度节点,避免误判折叠菜单。 - 顺带修掉 `document.fonts?.ready` 只取 promise 不 await 的老问题(等于没等字体)。 - 截图前断言首页的 `data-cms-state`:CMS 退化按名字失败,不留给像素比对去猜。
This commit is contained in:
1 parent
a5ce27c6ec
commit
ec30f508fa
1 file changed
+51
-4
@@ -51,11 +51,16 @@ async function waitForPageStable(page: Page) {
|
||||
// 不能直接 `scrollTo(scrollHeight)` 一跳到底:IntersectionObserver 只会回调当前视口
|
||||
// 内的目标,中间各屏被跳过 ⇒ 截图出来是空白段(2026-10-20 移除路由级 AnimatePresence
|
||||
// 后首屏不再替子树屏蔽 initial,这条路径才真正被走到)。
|
||||
// dwell 由 120ms 提到 260ms 并排掉两帧:120ms 在 4 worker 抢 CPU 时不足以让
|
||||
// IO 回调 + framer 提交动画,中间某屏会被跳过(lessons §5.51)。
|
||||
await page.evaluate(async () => {
|
||||
const nextFrame = () =>
|
||||
new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
|
||||
const step = Math.max(400, Math.floor(window.innerHeight * 0.8));
|
||||
for (let y = 0; y <= document.body.scrollHeight; y += step) {
|
||||
window.scrollTo({ top: y, behavior: 'instant' });
|
||||
await new Promise((resolve) => setTimeout(resolve, 120));
|
||||
await new Promise((resolve) => setTimeout(resolve, 260));
|
||||
await nextFrame();
|
||||
}
|
||||
window.scrollTo({ top: document.body.scrollHeight, behavior: 'instant' });
|
||||
await new Promise((resolve) => setTimeout(resolve, 400));
|
||||
@@ -71,10 +76,52 @@ async function waitForPageStable(page: Page) {
|
||||
});
|
||||
});
|
||||
await page.waitForTimeout(1000);
|
||||
await page.evaluate(() => {
|
||||
document.fonts?.ready;
|
||||
});
|
||||
await page.evaluate(() => document.fonts?.ready);
|
||||
await page.waitForTimeout(500);
|
||||
|
||||
await assertCmsStateOk(page);
|
||||
await waitForRevealsSettled(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等 whileInView 真正提交:framer 把未开始的动画写成 inline `opacity: 0`,
|
||||
* 完成后改写成 1。两者只差这一笔,所以按 inline style 判定即可。
|
||||
* 排除 display:none 与零高度元素(折叠菜单、屏外测距节点),避免把「本来就不可见」判成未提交。
|
||||
* 表达式以字符串传入:它在浏览器上下文求值,闭包里的 Node 侧变量带不过去。
|
||||
*/
|
||||
const PENDING_REVEALS_EXPR =
|
||||
"Array.from(document.querySelectorAll('main *')).filter((el) => el.style.opacity === '0' && getComputedStyle(el).display !== 'none' && el.getBoundingClientRect().height > 0)";
|
||||
|
||||
async function waitForRevealsSettled(page: Page) {
|
||||
try {
|
||||
await page.waitForFunction(`${PENDING_REVEALS_EXPR}.length === 0`, undefined, {
|
||||
timeout: 8000,
|
||||
polling: 100,
|
||||
});
|
||||
} catch {
|
||||
const total: number = await page.evaluate(`${PENDING_REVEALS_EXPR}.length`);
|
||||
const pending: string[] = await page.evaluate(
|
||||
`${PENDING_REVEALS_EXPR}.slice(0, 5).map((el) => el.tagName.toLowerCase() + '.' + String(el.className).split(' ').slice(0, 2).join('.'))`,
|
||||
);
|
||||
throw new Error(
|
||||
`${total} 个元素仍停在 inline opacity:0(whileInView 未提交):${pending.join(' , ')}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页把 CMS 读数状态写在 `data-cms-state` 上。退化时页面照常 200、只是整套落到常量版式,
|
||||
* 像素比对只会给出一团无从归因的差异 —— 在这里先按名字判一次。
|
||||
*/
|
||||
async function assertCmsStateOk(page: Page) {
|
||||
const marker = page.locator('main [data-cms-state]').first();
|
||||
if ((await marker.count()) === 0) return;
|
||||
const state = await marker.getAttribute('data-cms-state');
|
||||
if (state !== 'ok') {
|
||||
throw new Error(
|
||||
`CMS 数据退化:data-cms-state=${state};页面已回落到常量版式,像素比对无意义(lessons §5.51)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
test.describe('L1: 全页面视觉回归测试', () => {
|
||||
|
||||
Reference in new issue
Block a user