首页 > 解决方案 > 从 puppeteer 登录后如何为主页运行灯塔

问题描述

我添加了两个 npm "@lhci/cli" 和 puppeteer。之后我添加了两个配置文件

  1. lighthouserc.js :配置细节是:

module.exports = {
  ci: {
    upload: {
      target: 'temporary-public-storage'
    },
    collect: {
      puppeteerScript: 'puppeteer-script.js',
      chromePath: puppeteer.executablePath(),
     url: ["https://myWebsite.com/abc"],
      headful: true,
      numberOfRuns: 1,
      disableStorageReset: true,
      setting: {
      disableStorageReset: true
      },
      puppeteerLaunchOptions: {
         slowMo: 20,
        headless: false,
        disableStorageReset: true
      }

    },
    assert: {
      assertions: {
        'categories:performance': ['warn', { minScore: 1 }],
        'categories:accessibility': ['error', { minScore: 0.5 }]
      }
    }
  }
};

  1. puppeteer-script.js

module.exports = async (browser, context) => {
  await page.setDefaultNavigationTimeout(90000);
  await page.goto(context.url);
  await page.type('input[type=text]', 'abc');
  await page.type('input[type=email]', 'abc@abc.com');
  await page.type('input[type=password]', 'abc@100');
  await page.click('[type="button"]');
  await page.waitForNavigation({ waitUntil: "networkidle2" })
  await page.close();
};

在 package.json 我添加了脚本命令:

"test:lighthouse": "lhci autorun --collect.settings.chromeFlags='--no-sandbox'" 

现在登录工作正常,但我想为我在 lighthouserc.js ( https://myWebsite.com/abc )中指定的 url 运行灯塔。但是登录后它试图访问 url 并且再次登录屏幕来了,灯塔正在测量登录页面的性能。

是否可以在我在配置中指定的 url 上运行灯塔。请帮助我。 https://myWebsite.com/abc是我的 reactjs 应用程序

标签: reactjspuppeteerlighthouse

解决方案


我没有关于您网站工作流程的完整信息,但正如配置指南中提到的,puppeteer 脚本针对 lhci 配置文件中提到的每个 url 运行。

运行 puppeteer 脚本后,lighthouse 将打开 URL。现在,如果您的网站再次打开登录页面,则很可能是您的应用或配置存在问题。您的应用程序未正确设置 cookie 或登录过程以某种方式失败,您需要检查。

此外,由于 puppeteer 脚本将为配置中的每个 url 运行,如果您已经登录过一次,最好不要重新登录,请在 Github 上查看此问题。


推荐阅读