playwright-lighthouse
Advanced tools
Comparing version 3.1.0 to 3.2.0
{ | ||
"name": "playwright-lighthouse", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Playwright Lighthouse Audit", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -423,2 +423,92 @@ ## Lighthouse Playwright - NPM Package | ||
## Generating Lighthouse reports on LambdaTest | ||
You can execute Lighthouse reports on LambdaTest platform while executing Playwright tests with the following steps. You can generate multiple lighthouse reports in a single test. | ||
#### Install playwright-lighthouse library | ||
```sh | ||
npm install playwright-lighthouse | ||
``` | ||
#### Export the LIGTHHOUSE_LAMBDATEST environment variable to your project environment | ||
```sh | ||
export LIGHTHOUSE_LAMBDATEST='true' | ||
``` | ||
#### Sample script | ||
```js | ||
import { chromium } from "playwright"; | ||
import { playAudit } from "playwright-lighthouse"; | ||
(async () => { | ||
let browser, page; | ||
try { | ||
const capabilities = { | ||
browserName: "Chrome", | ||
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"); | ||
let element = await page.locator('[name="q"]'); | ||
await element.click(); | ||
await element.type("Playwright"); | ||
await element.press("Enter"); | ||
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(); | ||
} | ||
})(); | ||
``` | ||
#### Viewing test results | ||
You can view your tests on [LambdaTest Web Automation dashboard](https://automation.lambdatest.com). | ||
![Lighthouse report on LambdaTest](./docs/LambdaTest_Playwright_LighthouseReport.png) | ||
## Tell me your issues | ||
@@ -425,0 +515,0 @@ |
@@ -24,3 +24,9 @@ import { lighthouse } from './task.js'; | ||
export const playAudit = async function (auditConfig = {}) { | ||
if (!auditConfig.port || (!auditConfig.page && !auditConfig.url)) { | ||
if (process.env.LIGHTHOUSE_LAMBDATEST === 'true') { | ||
if (!auditConfig.page) { | ||
throw new Error( | ||
`Please pass the page object to generate lighthouse report on LambdaTest.` | ||
); | ||
} | ||
} else if (!auditConfig.port || (!auditConfig.page && !auditConfig.url)) { | ||
throw new Error( | ||
@@ -64,2 +70,3 @@ `port, page or url is not set in playwright lighthouse config. Refer to https://github.com/abhinaba-ghosh/playwright-lighthouse to have more information and set it by yourself :). ` | ||
const { comparison, results } = await lighthouse({ | ||
page: auditConfig.page, | ||
url, | ||
@@ -66,0 +73,0 @@ thresholds: auditConfig.thresholds || defaultThresholds, |
@@ -38,2 +38,3 @@ import fs from 'fs/promises'; | ||
export const lighthouse = async ({ | ||
page, | ||
url, | ||
@@ -52,7 +53,37 @@ thresholds, | ||
const results = await lighthouseLib( | ||
url, | ||
{ disableStorageReset: true, ...opts }, | ||
config | ||
); | ||
let results = {}; | ||
// Execution on LambdaTest | ||
if (process.env.LIGHTHOUSE_LAMBDATEST === 'true') { | ||
const lambdatestExecutionError = | ||
'Failed to generate Lighthouse report on LambdaTest'; | ||
console.log('Generating Lighthouse report on LambdaTest for url:: ', url); | ||
const LAMBDATEST_ARGUMENTS = { | ||
action: 'lighthouseReport', | ||
arguments: { | ||
url, | ||
lighthouseOptions: opts, | ||
lighthouseConfig: config, | ||
source: 'playwright-lighthouse', | ||
}, | ||
}; | ||
const ltResponse = await page.evaluate(() => {}, | ||
`lambdatest_action: ${JSON.stringify(LAMBDATEST_ARGUMENTS)}`); | ||
try { | ||
const { error, data } = JSON.parse(ltResponse); | ||
results.lhr = JSON.parse(data); | ||
if (error) throw lambdatestExecutionError; | ||
} catch (error) { | ||
throw new Error(`${lambdatestExecutionError}: ${ltResponse}`); | ||
} | ||
} else { | ||
results = await lighthouseLib( | ||
url, | ||
{ disableStorageReset: true, ...opts }, | ||
config | ||
); | ||
} | ||
const newValues = Object.keys(results.lhr.categories).reduce( | ||
@@ -59,0 +90,0 @@ (acc, curr) => ({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26495
13
256
522
3