Lighthouse is a tool developed by Google that analyzes web apps and web pages, collecting modern performance metrics and insights on developer best practices.
In this video, we learn how to use Lighthouse Audit with Playwright and perform performance, accessibility, SEO, and PWA audits on a web page. Let's go through the process step by step.
Install Lighthouse and playwright-lighthouse packages:
npm install lighthouse playwright-lighthouse
Use the playwright.chromium.launch()
method to start a new Chromium instance.
Pass the required port number for remote debugging in chromium.launch()
and playwrightAudit()
functions.
Create a new browser context and a new page using the browser.newContext()
and context.newPage()
methods.
Perform the audit for specific categories and thresholds using playwrightAudit()
.
Example:
await playwrightAudit({
result: {
categories: {
performance: 50,
accessibility: 100,
bestpractices: 100,
seo: 100,
pwa: 100,
},
},
ignore: { performance: true },
port: 9222,
page: page,
});
Save the audit report in different formats and directories using the mkdir
and fs.writeFile
functions.
You can also run the tests for multiple URLs using the test.use()
and test.each()
methods provided by the Playwright.
If you're interested in auditing only specific categories (e.g., performance), you can use a custom Lighthouse config and pass it to the playwrightAudit()
function.
Example:
import { config as lhDesktopConfig } from "lighthouse/lighthouse-core/config/lr-desktop-config.js";
await playwrightAudit({
config: lhDesktopConfig,
// ...
});
That's it! You can now run Lighthouse Audits for your web applications using the Playwright framework! If you have any questions or need further assistance, feel free to ask in the comments.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].