playwright-lighthouse
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -1,12 +0,19 @@ | ||
import { Page } from 'playwright'; | ||
import { Page } from 'playwright-core'; | ||
export interface playwrightLighthouseConfig { | ||
page: Page; | ||
page?: Page; | ||
url?: string; | ||
port: number; | ||
thresholds?: any; | ||
opts?: any; | ||
config?: any; | ||
reports?: {}; | ||
thresholds?: Record<string, number>; | ||
opts?: Record<string, any>; | ||
config?: Record<string, any>; | ||
reports?: { | ||
html?: boolean; | ||
json?: boolean; | ||
csv?: boolean; | ||
}; | ||
directory?: string; | ||
name?: string; | ||
ignoreError?: boolean; | ||
disableLogs?: boolean; | ||
} | ||
@@ -13,0 +20,0 @@ |
{ | ||
"name": "playwright-lighthouse", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Playwright Lighthouse Audit", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test --timeout 15000", | ||
"test": "mocha --parallel --timeout 30000", | ||
"lint": "eslint --ext .js --ignore-path .gitignore .", | ||
"prettier": "prettier --check \"**/*.{js,md,yml}\"", | ||
@@ -26,15 +27,17 @@ "prettier:fix": "prettier --write \"**/*.{js,md,yml}\"" | ||
"dependencies": { | ||
"chalk": "^4.0.0", | ||
"ua-parser-js": "^0.7.21" | ||
"chalk": "^4.1.2", | ||
"ua-parser-js": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "9.0.0", | ||
"@types/node": "16.11.7", | ||
"chai": "^4.3.4", | ||
"eslint": "8.2.0", | ||
"lighthouse": "^8.6.0", | ||
"mocha": "9.1.3", | ||
"playwright": "^1.16.1", | ||
"prettier": "^2.4.1" | ||
"@types/mocha": "9.1.0", | ||
"@types/node": "^16.11.26", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.12.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"lighthouse": "^9.5.0", | ||
"mocha": "9.2.2", | ||
"playwright-core": "1.19.2", | ||
"prettier": "^2.6.1" | ||
} | ||
} |
@@ -318,2 +318,55 @@ ## Lighthouse Playwright - NPM Package | ||
### Running lighthouse on authenticated routes with globalSetup | ||
In case you have a [`globalSetup`](https://playwright.dev/docs/test-auth) script in your test you might want to reuse saved state instead of running auth before every test. | ||
Additionally, you may pass `url` instead of `page` to speedup execution and save resources. | ||
```ts | ||
import os from 'os' | ||
import path from 'path' | ||
import { chromium, test as base } from '@playwright/test' | ||
import type { BrowserContext } from '@playwright/test' | ||
import getPort from 'get-port' // version ^5.1.1 due to issues with imports in playwright 1.20.1 | ||
export const lighthouseTest = base.extend<{ context: BrowserContext }, { port: number }>({ | ||
port: [ | ||
async ({}, use) => { | ||
// Assign a unique port for each playwright worker to support parallel tests | ||
const port = await getPort() | ||
await use(port) | ||
}, | ||
{ scope: 'worker' }, | ||
], | ||
context: [ | ||
async ({ port, launchOptions }, use) => { | ||
const context = await chromium.launchPersistentContext(path.join(os.tmpdir(), 'pw', `${Math.random()}`.replace('.', '')), { | ||
args: [...(launchOptions.args || []), `--remote-debugging-port=${port}`], | ||
}) | ||
// apply state previously saved in the the `globalSetup` | ||
await context.addCookies(require('../../state-chrome.json').cookies) | ||
await use(context) | ||
await context.close() | ||
}, | ||
{ scope: 'test' }, | ||
], | ||
}) | ||
lighthouseTest.describe('Authenticated route after globalSetup', () => { | ||
lighthouseTest( | ||
'should pass lighthouse tests', | ||
async ({ port }) => { | ||
// it's possible to pass url directly instead of a page | ||
// to avoid opening a page an extra time and keeping it opened | ||
await playAudit({ | ||
url: 'http://localhost:3000/my-profile', | ||
port, | ||
}); | ||
} | ||
); | ||
}); | ||
``` | ||
## Generating audit reports | ||
@@ -320,0 +373,0 @@ |
const { lighthouse } = require('./task'); | ||
const chalk = require('chalk'); | ||
const uaParser = require('ua-parser-js'); | ||
const log = console.log; | ||
const defaultThresholds = { | ||
@@ -28,13 +24,24 @@ performance: 100, | ||
let playAudit = async function (auditConfig = {}) { | ||
if (!auditConfig.port || !auditConfig.page) { | ||
if (!auditConfig.port || (!auditConfig.page && !auditConfig.url)) { | ||
throw new Error( | ||
`port/page 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 :). ` | ||
`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 ua = await auditConfig.page.evaluate(() => navigator.userAgent); | ||
const currentBrowserName = uaParser(ua).browser.name; | ||
const log = auditConfig.disableLogs ? () => {} : console.log; | ||
const chalk = auditConfig.disableLogs | ||
? new Proxy({}, { get: () => () => {} }) | ||
: require('chalk'); | ||
if (!checkBrowserIsValid(currentBrowserName)) { | ||
throw new Error(`${currentBrowserName} is not supported. Skipping...`); | ||
const url = auditConfig.url || auditConfig.page.url(); | ||
if (auditConfig.page) { | ||
const uaParser = require('ua-parser-js'); | ||
// eslint-disable-next-line no-undef | ||
const ua = await auditConfig.page.evaluate(() => navigator.userAgent); | ||
const currentBrowserName = uaParser(ua).browser.name; | ||
if (!checkBrowserIsValid(currentBrowserName)) { | ||
throw new Error(`${currentBrowserName} is not supported. Skipping...`); | ||
} | ||
} | ||
@@ -57,3 +64,3 @@ | ||
const { comparison, results } = await lighthouse({ | ||
url: auditConfig.page.url(), | ||
url, | ||
thresholds: auditConfig.thresholds || defaultThresholds, | ||
@@ -74,7 +81,7 @@ opts: auditConfig.opts, | ||
if (comparison.errors.length > 0) { | ||
if (auditConfig.ignoreError !== true && comparison.errors.length > 0) { | ||
const formateErrors = `\n\n${comparison.errors.join('\n')}`; | ||
const label = | ||
comparison.errors.length === 1 | ||
comparison.errors.length === 1 | ||
? `playwright lighthouse - A threshold is not matching the expectation.${formateErrors}` | ||
@@ -81,0 +88,0 @@ : `playwright lighthouse - Some thresholds are not matching the expectations.${formateErrors}`; |
@@ -1,2 +0,2 @@ | ||
const fs = require('fs'); | ||
const fs = require('fs/promises'); | ||
const lighthouseLib = require('lighthouse'); | ||
@@ -30,8 +30,4 @@ const ReportGenerator = require('lighthouse/report/generator/report-generator'); | ||
const reportBody = ReportGenerator.generateReport(lhr, type); | ||
try { | ||
await fs.mkdirSync(dir, { recursive: true }); | ||
await fs.writeFileSync(`${dir}/${name}.${type}`, reportBody); | ||
} catch (err) { | ||
throw err; | ||
} | ||
await fs.mkdir(dir, { recursive: true }); | ||
await fs.writeFile(`${dir}/${name}.${type}`, reportBody); | ||
} else { | ||
@@ -81,4 +77,4 @@ console.log(`Invalid report type specified: ${type} Skipping Reports...)`); | ||
const output = { comparison: compare(thresholds, newValues), results } | ||
const output = { comparison: compare(thresholds, newValues), results }; | ||
return output; | ||
}; |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
20979
12
216
412
10
+ Addedua-parser-js@1.0.39(transitive)
- Removedua-parser-js@0.7.39(transitive)
Updatedchalk@^4.1.2
Updatedua-parser-js@^1.0.2