【问题标题】:how to integrate lighthouse with testcafe?如何将灯塔与 testcafe 集成?
【发布时间】:2020-10-14 13:28:16
【问题描述】:

我需要在调用lighthouse 时传递connection 参数

https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/index.js#L41

async function lighthouse(url, flags = {}, configJSON, connection) {
  // verify the url is valid and that protocol is allowed
  if (url && (!URL.isValid(url) || !URL.isProtocolAllowed(url))) {
    throw new LHError(LHError.errors.INVALID_URL);
  }

  // set logging preferences, assume quiet
  flags.logLevel = flags.logLevel || 'error';
  log.setLevel(flags.logLevel);

  const config = generateConfig(configJSON, flags);

  connection = connection || new ChromeProtocol(flags.port, flags.hostname);

  // kick off a lighthouse run
  return Runner.run(connection, {url, config});
}

在我的 testcafe 中,我的测试看起来像

test('Run lighthouse, async t => {
  lighthouse('https://www.youtube.com', {}, {}, ????)
})

我无法检索 testcafe 已打开的 chrome 实例的 connection,而不是生成新的 chromeRunner

【问题讨论】:

  • 我不知道将“灯塔”与 TestCafe 集成的简单方法,需要对此进行调查。我在 TestCafe 存储库中创建了一个问题 - github.com/DevExpress/testcafe/issues/3493。跟踪它以获取有关进度的通知。

标签: google-chrome audit testcafe lighthouse


【解决方案1】:

有一个名为 testcafe-lighthouse 的 npm 库,它有助于使用 TestCafe 审核网页。它还具有生成 HTML 详细报告的能力。

通过以下方式安装插件:

$ yarn add -D testcafe-lighthouse
# or 
$ npm install --save-dev testcafe-lighthouse
  • 使用默认阈值审核
import { testcafeLighthouseAudit } from 'testcafe-lighthouse';

fixture(`Audit Test`).page('http://localhost:3000/login');

test('user performs lighthouse audit', async () => {
  const currentURL = await t.eval(() => document.documentURI);
  await testcafeLighthouseAudit({
    url: currentURL,
    cdpPort: 9222,
  });
});
  • 使用自定义阈值进行审核:
import { testcafeLighthouseAudit } from 'testcafe-lighthouse';

fixture(`Audit Test`).page('http://localhost:3000/login');

test('user page performance with specific thresholds', async () => {

  const currentURL = await t.eval(() => document.documentURI);

  await testcafeLighthouseAudit({
    url: currentURL,
    thresholds: {
      performance: 50,
      accessibility: 50,
      'best-practices': 50,
      seo: 50,
      pwa: 50,
    },
    cdpPort: 9222,
  });
});
  • 您需要像下面这样开始测试:
# headless mode, preferable for CI
npx testcafe chrome:headless:cdpPort=9222 test.js

# non headless mode
npx testcafe chrome:emulation:cdpPort=9222   test.js

我希望它对您的自动化之旅有所帮助。

【讨论】:

    【解决方案2】:

    我做了类似的事情,我使用 CLI 在特定端口上使用 google chrome 启动 lig​​thouse

    npm run testcafe -- chrome:headless:cdpPort=1234
    

    然后我制作灯塔函数以获取端口作为参数

    export default async function lighthouseAudit(url, browser_port){
        let result = await lighthouse(url, {
            port: browser_port,     // Google Chrome port Number
            output: 'json',
            logLevel: 'info',
        });
        return result;
    };
    

    然后您可以像

    一样简单地运行审核
        test(`Generate Light House Result `, async t => {
            auditResult = await lighthouseAudit('https://www.youtube.com',1234);
        });
    

    希望对你有帮助

    【讨论】:

    • 你能详细解释一下npm run testcafe -- chrome:headless:cdpPort=1234吗?这是您为此编写的特殊脚本吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2011-01-20
    • 1970-01-01
    • 2021-06-04
    • 2021-10-16
    • 2011-06-28
    相关资源
    最近更新 更多