Home  Puppeteer   How to gene ...

How to generate a Lighthouse report in puppeteer

Generating a Lighthouse report using Puppeteer involves launching a headless Chrome browser and running Lighthouse programmatically. Here’s how you can do it step-by-step:

Step-by-Step Guide to Generate a Lighthouse Report with Puppeteer

  1. Install Puppeteer and Lighthouse:

    Ensure you have Puppeteer and Lighthouse installed as dependencies in your project:

    npm install puppeteer lighthouse
    
  2. Write the Puppeteer Script:

    Create a script that launches Puppeteer, navigates to a URL, and generates a Lighthouse report.

    const puppeteer = require('puppeteer');
    const lighthouse = require('lighthouse');
    const { URL } = require('url');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
    
      // URL to analyze with Lighthouse
      const url = 'https://example.com';
      
      // Launch Lighthouse and get the report
      const report = await lighthouse(url, {
        port: (new URL(browser.wsEndpoint())).port,
        output: 'json',
        logLevel: 'info',
      });
    
      // Output Lighthouse report
      console.log('Lighthouse report:', report.lhr);
    
      await browser.close();
    })();
    
    • Replace 'https://example.com' with the URL of the site you want to analyze.
  3. Run the Script:

    Execute your Puppeteer script to launch a headless Chrome instance and generate the Lighthouse report:

    node lighthouse-report.js
    
  4. Interpret the Lighthouse Report:

    The Lighthouse report (report.lhr) contains detailed information about performance, accessibility, SEO, and best practices for the specified URL.

Published on: Jun 28, 2024, 12:31 AM  
 

Comments

Add your comment