screenshot-homepage.cjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const { chromium } = require('playwright');
  2. const { join } = require('path');
  3. (async () => {
  4. console.log('启动浏览器...');
  5. const browser = await chromium.launch({ headless: true });
  6. const page = await browser.newPage();
  7. // 设置视口大小
  8. await page.setViewportSize({ width: 1920, height: 1080 });
  9. console.log('导航到 http://localhost:8080 ...');
  10. try {
  11. await page.goto('http://localhost:8080', { waitUntil: 'networkidle', timeout: 10000 });
  12. } catch (error) {
  13. console.error('无法连接到 http://localhost:8080');
  14. console.error('请先启动开发服务器: pnpm dev');
  15. await browser.close();
  16. process.exit(1);
  17. }
  18. // 等待页面内容加载
  19. await page.waitForSelector('header', { timeout: 5000 });
  20. await page.waitForTimeout(1000); // 额外等待确保渲染完成
  21. // 截取全屏
  22. const screenshotPath = join(process.cwd(), 'homepage-full-screenshot.png');
  23. console.log('正在截取全屏...');
  24. await page.screenshot({
  25. path: screenshotPath,
  26. fullPage: true,
  27. animations: 'disabled' // 禁用动画以获得更清晰的截图
  28. });
  29. console.log(`截图已保存: ${screenshotPath}`);
  30. const fs = require('fs');
  31. console.log('文件大小:', fs.statSync(screenshotPath).size, 'bytes');
  32. await browser.close();
  33. console.log('完成!');
  34. })();