e2e-window-target.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. export interface E2EBrowserTargetInfo {
  2. id: string | null;
  3. type: string;
  4. url: string;
  5. }
  6. export interface E2EWebDriverPageHandleCandidate {
  7. handle: string;
  8. targetUrl: string;
  9. }
  10. export function collectE2EWebDriverPageHandleCandidates(
  11. handles: string[],
  12. targets: E2EBrowserTargetInfo[],
  13. ): E2EWebDriverPageHandleCandidate[] {
  14. const pageTargetsById = new Map(
  15. targets
  16. .filter(
  17. (target): target is E2EBrowserTargetInfo & { id: string } =>
  18. target.type === "page" && typeof target.id === "string" && target.id.length > 0,
  19. )
  20. .map((target) => [target.id, target] as const),
  21. );
  22. // 修复原因:CDP targetId 只有同时出现在 ChromeDriver 的 handle 表中时,
  23. // 才能作为 WebDriver 窗口使用;直接把 Puppeteer targetId 传给 switchToWindow
  24. // 会在长批次 target 表失步时静默留在 chrome-error app target。
  25. return handles.flatMap((handle) => {
  26. const target = pageTargetsById.get(handle);
  27. return target ? [{ handle, targetUrl: target.url }] : [];
  28. });
  29. }