probe-e2e-mobile-browser.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { mkdtemp, rm, stat, writeFile } from "node:fs/promises";
  2. import { dirname, join, resolve } from "node:path";
  3. import { chromium } from "playwright-core";
  4. const output = process.argv[2];
  5. if (!output) throw new Error("Usage: node scripts/probe-e2e-mobile-browser.mjs <report.json>");
  6. const reportPath = resolve(output);
  7. const executablePath = chromium.executablePath();
  8. const probeDir = await mkdtemp(join(dirname(reportPath), "playwright-probe-"));
  9. // 创建真实录制页面,才能同时验证调试连接和同版本的 Playwright ffmpeg。
  10. try {
  11. const browser = await chromium.launch({ executablePath, headless: true });
  12. try {
  13. const viewport = { width: 160, height: 120 };
  14. const context = await browser.newContext({
  15. viewport,
  16. recordVideo: { dir: probeDir, size: viewport },
  17. });
  18. const page = await context.newPage();
  19. await page.setContent("<title>ZCode E2E browser probe</title><main>Browser and video</main>");
  20. if ((await page.title()) !== "ZCode E2E browser probe") {
  21. throw new Error("Playwright browser did not return the probe page title");
  22. }
  23. const video = page.video();
  24. await context.close();
  25. const videoBytes = video ? (await stat(await video.path())).size : 0;
  26. if (!videoBytes) throw new Error("Playwright recorder did not produce a video");
  27. const report = { executablePath, browserVersion: browser.version(), videoBytes };
  28. await writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`, "utf8");
  29. console.log(`[e2e] Chromium ${report.browserVersion} recorded ${videoBytes} bytes`);
  30. } finally {
  31. await browser.close();
  32. }
  33. } finally {
  34. await rm(probeDir, { recursive: true, force: true });
  35. }