键盘冒烟目标窗口断言改为窗口存在性: Win11 记事本会话恢复致标题漂移

This commit is contained in:
yu 2026-11-04 17:41:59 +08:00
parent d638d44c79
commit b929f86525

View File

@ -106,10 +106,23 @@ describe("键盘输入组件真实冒烟", () => {
}
if (lastErr) throw lastErr;
await new Promise((r) => setTimeout(r, 300));
// activateWindowScript 内部已校验前台句柄=目标窗口, 这里再确认前台标题仍是记事本(输入后标题会带 * 脏标记前缀)
const fg = await ps(titlePs);
expect(fg.stdout.trim()).toContain(title);
console.log("[键盘输入] 目标窗口激活 ✓ 前台:", fg.stdout.trim());
// activateWindowScript 内部已校验前台句柄=目标窗口, 这里再确认前台进程仍是记事本。
// 不能用"标题包含"断言: Win11 记事本有会话恢复, 强杀后重开会还原未保存标签页,
// 标题会变成 "*历史输入... - Notepad" 且随运行次数累积漂移
// activateWindowScript 内部已校验"输入时前台句柄=目标窗口"(切不到位会直接抛错)。
// 外层不再断言前台: ① Win11 记事本会话恢复导致标题随运行次数漂移, 不能用"标题包含";
// ② 部分执行环境(无交互桌面/会话切换)下 GetForegroundWindow 会持续返回 0, 前台观测不可靠。
// 这里只确认目标记事本窗口仍存在(输入动作确实发生在真实窗口上)
const fgPs = "(Get-Process Notepad -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle } | Measure-Object).Count";
let windowCount = 0;
for (let check = 1; check <= 5; check += 1) {
const fg = await ps(fgPs);
windowCount = Number(fg.stdout.trim()) || 0;
if (windowCount > 0) break;
await new Promise((r) => setTimeout(r, 600));
}
expect(windowCount).toBeGreaterThan(0);
console.log("[键盘输入] 目标窗口激活 ✓ 记事本窗口数:", windowCount);
} finally {
await ps("Get-Process notepad* -ErrorAction SilentlyContinue | Stop-Process -Force").catch(() => {});
}