You can execute Lighthouse audits on the LambdaTest platform while running Playwright tests. This allows you to generate multiple reports within a single test execution on their infrastructure.
Prerequisites
- Install the library:
npm install playwright-lighthouse
- Set the
LIGHTHOUSE_LAMBDATEST environment variable to true in your project environment:export LIGHTHOUSE_LAMBDATEST='true'
Implementation Pattern
Connect to LambdaTest using chromium.connect with the appropriate capabilities, perform your browser actions, and then call playAudit. You can use page.evaluate to communicate the test status (passed/failed) back to the LambdaTest dashboard based on whether playAudit meets your defined thresholds.
import { chromium } from 'playwright';
import { playAudit } from 'playwright-lighthouse';
(async () => {
let browser, page;
try {
const capabilities = {
browserName: 'Chrome', // Browsers allowed: `Chrome`, `MicrosoftEdge` and `pw-chromium`
browserVersion: 'latest',
'LT:Options': {
platform: 'Windows 11',
build: 'Web Performance testing',
name: 'Lighthouse test',
user: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY,
network: true,
video: true,
console: true,
},
};
browser = await chromium.connect({
wsEndpoint: `wss://cdp.lambdatest.com/playwright?capabilities=${encodeURIComponent(JSON.stringify(capabilities))}`,
});
page = await browser.newPage();
await page.goto('https://duckduckgo.com');
// ... perform actions ...
try {
await playAudit({
url: 'https://duckduckgo.com',
page: page,
thresholds: {
performance: 50,
accessibility: 50,
'best-practices': 50,
seo: 50,
pwa: 10,
},
reports: {
formats: {
json: true,
html: true,
csv: true,
},
},
});
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'passed', remark: 'Web performance metrics are are above the thresholds.' } })}`
);
} catch (e) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'failed', remark: e.stack } })}`
);
console.error(e);
}
} catch (e) {
await page.evaluate(
(_) => {},
`lambdatest_action: ${JSON.stringify({ action: 'setTestStatus', arguments: { status: 'failed', remark: e.stack } })}`
);
} finally {
await page.close();
await browser.close();
}
})();