16.3 Playwright 层实现
16.3.1 Playwright 会话管理
连接复用
// src/browser/pw-session.ts — 连接缓存
let cached: ConnectedBrowser | null = null;
let connecting: Promise<ConnectedBrowser> | null = null;页面状态追踪
// src/browser/pw-session.ts — 页面状态
type PageState = {
console: BrowserConsoleMessage[]; // 控制台消息(最多 500 条)
errors: BrowserPageError[]; // 页面错误(最多 200 条)
requests: BrowserNetworkRequest[]; // 网络请求(最多 500 条)
requestIds: WeakMap<Request, string>;
// 角色引用(Role Refs)——页面快照中元素的标识
roleRefs?: Record<string, { role: string; name?: string; nth?: number }>;
roleRefsMode?: "role" | "aria";
roleRefsFrameSelector?: string;
};
const pageStates = new WeakMap<Page, PageState>();Role Refs 缓存
16.3.2 角色快照(Role Snapshot)
三类 ARIA 角色
快照格式
引用分配与去重
快照统计
16.3.3 三种快照模式
1. AI Snapshot(推荐)
2. ARIA Snapshot
3. Role Snapshot
16.3.4 Playwright 工具核心
interactions — 交互操作
函数
用途
downloads — 下载管理
state — 页面状态
storage — 存储管理
trace — 性能追踪
16.3.5 AI 辅助模块聚合
本节小结
Last updated