【问题标题】:Changing final screenshot resolution for (Chrome) Lighthouse report更改 (Chrome) Lighthouse 报告的最终屏幕截图分辨率
【发布时间】:2020-09-02 23:40:47
【问题描述】:

我正在使用 Lighthouse 为我们自己的一些网站创建性能报告。我还需要截屏以查看该网站在台式计算机上的外观。

Lighthouse 似乎默认使用移动分辨率截屏。

我该如何改变呢?

如果可能,我会尽量避免使用 Puppeteer 等其他库。

这是一些运行灯塔的代码

import lighthouse from 'lighthouse';
import chromeLauncher from 'chrome-launcher';

const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
const options = {
    logLevel: 'info',
    output: 'json',
    onlyCategories: ['performance', 'accessibility', 'best-practices', 'seo'],
    port: chrome.port
};
const runnerResult  = await lighthouse(url, options);

Lighthouse 从以下审核中返回显示捕获:

  • screenshot-thumbnails - 120px x 213px
  • final-screenshot - 280px x 498px

我能得到比这更大的东西吗?

【问题讨论】:

  • 您关注的是开发者工具中的 Lighthouse 还是 API / CLI 中的 Lighthouse?此外,您是在寻找更高质量的图像,还是只是在加载时间线缩略图中将宽高比更改为桌面?
  • 谢谢@graham-ritchie 我使用 LightHouse CLI。我正在为“最终屏幕截图”和“幻灯片/时间线缩略图”使用不同的纵横比,请
  • 在你传入的选项中将“策略”改为“桌面”(默认为移动)。

标签: google-chrome lighthouse


【解决方案1】:

简介

您需要使用文档中列出的lighthouse CLI options - 例如参见github 自述文件和node-npm documentation.

有很多选项,但最有趣的选项是:

--screenEmulation

注意:另请参阅--preset 选项,或使用--screenEmulation.disabled 禁用。

否则你可以单独设置这4个参数:

--screenEmulation.mobile
--screenEmulation.width=360 
--screenEmulation.height=640 
--screenEmulation.deviceScaleFactor=2

还请查看设备仿真/emulatedUserAgents,并专门使用如下参数:

lighthouse <url> --chrome-flags="--window-size=1024,768"    
lighthouse <url> --screenEmulation.desktop--screenEmulation.width=1024 --screenEmulation.height=768 --screenEmulation.deviceScaleFactor=2     
lighthouse <url> --screenEmulation.mobile --screenEmulation.width=1024 --screenEmulation.height=768 --screenEmulation.deviceScaleFactor=2  

和

lighthouse <url> --preset=desktop   

还可以查看位于以下位置的一些其他配置:https://github.com/GoogleChrome/lighthouse/tree/master/lighthouse-core/config

通过 Nodejs

在通过nodejs启动方面,我需要找到我的示例,但我确实从灯塔的文档中找到了示例。为了从 nodejs 运行 lighthouse + chrome headless,请使用以下命令:

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');

function launchChromeAndRunLighthouse(url, flags = {}, config = null) {
  return chromeLauncher.launch(flags).then(chrome => {
    flags.port = chrome.port;
    return lighthouse(url, flags, config).then(results =>
      chrome.kill().then(() => results));
  });
}

const flags = {
  chromeFlags: ['--headless']
};

launchChromeAndRunLighthouse('https://github.com', flags).then(results => {
  // Use results!
});

PS:将很快更新更多信息

来源:

【讨论】:

  • 这太棒了!您是否知道通过节点 API 可以通过任何方式实现这一点?
  • 另外,肯定会奖励代表 - 只是在剩余的日子里保持开放以提高知名度 - 如果其他答案有意义,我也会为他们打开另一个赏金
【解决方案2】:

使用full-page-screenshot审核

值得一提的是,在 Lighthouse 7.5 版中(不知道在以前的版本中如何)全尺寸屏幕截图在名为 full-page-screenshot 的审核中。

考虑到这是一个“整页”屏幕截图,因此它的整个网站从上到下(不仅是首屏内容可见)。

除此之外,@menelaos 的回答还描述了有关操作窗口大小和设备模拟的所有内容,您需要获取任何大小的屏幕截图。

【讨论】:

  • 谢谢!我可以通过以下方式获取整页截图:const finalScreenshot = results.lhr.audits['full-page-screenshot'].details.screenshot.data.split(';base64,').pop();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多