reader-layout-check.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { chromium } from "playwright-core";
  2. import fs from "node:fs/promises";
  3. import path from "node:path";
  4. const baseUrl = "http://127.0.0.1:3000/reader/lan-archive";
  5. const edgePath = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe";
  6. const outDir = path.resolve("artifacts", "reader-layout");
  7. const cases = ["narrow", "medium", "wide"];
  8. await fs.mkdir(outDir, { recursive: true });
  9. const browser = await chromium.launch({
  10. executablePath: edgePath,
  11. headless: true
  12. });
  13. const page = await browser.newPage({
  14. viewport: { width: 1600, height: 1200 },
  15. deviceScaleFactor: 1
  16. });
  17. const results = [];
  18. for (const width of cases) {
  19. await page.goto(`${baseUrl}?width=${width}`, { waitUntil: "networkidle" });
  20. const metrics = await page.evaluate(() => {
  21. const layout = document.querySelector(".reader-desktop-layout");
  22. const shell = document.querySelector(".reader-qq-shell");
  23. const left = document.querySelector(".reader-float--left");
  24. const right = document.querySelector(".reader-float--right");
  25. if (!(layout instanceof HTMLElement) || !(shell instanceof HTMLElement) || !(left instanceof HTMLElement) || !(right instanceof HTMLElement)) {
  26. throw new Error("reader desktop layout nodes not found");
  27. }
  28. const layoutRect = layout.getBoundingClientRect();
  29. const shellRect = shell.getBoundingClientRect();
  30. const leftRect = left.getBoundingClientRect();
  31. const rightRect = right.getBoundingClientRect();
  32. return {
  33. bodyWidth: document.body.clientWidth,
  34. layoutLeft: layoutRect.left,
  35. layoutWidth: layoutRect.width,
  36. shellLeft: shellRect.left,
  37. shellWidth: shellRect.width,
  38. leftColumnLeft: leftRect.left,
  39. leftColumnWidth: leftRect.width,
  40. rightColumnLeft: rightRect.left,
  41. rightColumnWidth: rightRect.width,
  42. leftGapToShell: shellRect.left - (leftRect.left + leftRect.width),
  43. rightGapToShell: rightRect.left - (shellRect.left + shellRect.width)
  44. };
  45. });
  46. const imagePath = path.join(outDir, `${width}.png`);
  47. await page.screenshot({ path: imagePath, fullPage: true });
  48. results.push({ width, imagePath, metrics });
  49. }
  50. await browser.close();
  51. const reportPath = path.join(outDir, "report.json");
  52. await fs.writeFile(reportPath, JSON.stringify(results, null, 2), "utf8");
  53. console.log(JSON.stringify({ reportPath, results }, null, 2));