lighthouse-reporting 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,10 +4,322 @@
4
4
 
5
5
  The reports include trend history support, allowing you to track performance improvements over time.
6
6
 
7
+ <p align="center">
8
+ <img alt="HTML" src="./docs/html_report.png" width="45%">
9
+ &nbsp; &nbsp; &nbsp; &nbsp;
10
+ <img alt="Plot" src="./docs/plot_report.png" width="45%">
11
+ </p>
12
+
7
13
  ## Usage
8
14
 
9
- ### Jenkins
15
+ Examples of usage with Playwright + Lighthouse (and Storybook).
16
+
17
+ ### In your frontend or testing framework
18
+ The example of usage [playwright](https://github.com/microsoft/playwright) and [playwright-lighthouse](https://github.com/abhinaba-ghosh/playwright-lighthouse) together.
19
+
20
+ <details>
21
+ <summary>lighthouse_pages.spec</summary>
22
+
23
+ ```ts
24
+ import path from 'path'
25
+ import { playAudit } from 'playwright-lighthouse'
26
+ import { playwrightLighthouseTest, getScores, writeCsvResult, writeHtmlListEntryWithRetry, LighthouseResult } from 'lighthouse-reporting'
27
+ import { MyPage1 } from '../../pages/my-page-1.page.js'
28
+ import { MyPage2 } from '../../pages/my-page-1.page.js'
29
+
30
+ playwrightLighthouseTest.setTimeout(60000)
31
+ const reportDir = path.join(process.cwd(), 'lighthouse')
32
+ const htmlFilePath = path.join(reportDir, 'index.html')
33
+
34
+ const swimlanes = ['performance']
35
+ const lighthousePages = [
36
+ { name: 'MyPage1', po: MyPage1, thresholds: { performance: 80, accessibility: 88, seo: 92 }, swimlanes },
37
+ { name: 'MyPage2', po: MyPage2, thresholds: { performance: 90, accessibility: 100, seo: 90 }, swimlanes },
38
+ ]
39
+
40
+ lighthousePages.forEach(({ name, po, thresholds, swimlanes }) => {
41
+ playwrightLighthouseTest(name, async ({ port, baseURL }) => {
42
+ const onlyCategories = ['accessibility', 'seo', 'performance']
43
+
44
+ const result: LighthouseResult = await playAudit({
45
+ url: baseURL + po.getPath('123'),
46
+ port,
47
+ thresholds,
48
+ reports: {
49
+ formats: {
50
+ html: true,
51
+ },
52
+ name,
53
+ directory: reportDir,
54
+ },
55
+ opts: {
56
+ onlyCategories,
57
+ screenEmulation: { disabled: true },
58
+ },
59
+ disableLogs: true,
60
+ ignoreError: true,
61
+ })
62
+
63
+ const scores = getScores(result)
64
+ await writeCsvResult(reportDir, name, scores, thresholds, swimlanes)
65
+ await writeHtmlListEntryWithRetry(htmlFilePath, name, scores, thresholds, result.comparisonError)
66
+
67
+ if (result.comparisonError) {
68
+ throw new Error(result.comparisonError)
69
+ }
70
+ })
71
+ })
72
+ ```
73
+
74
+ </details>
75
+
76
+ ### Or in Storybook
77
+
78
+ An example for storybook with lighthouse and screenshot testing
79
+
80
+ <details>
81
+ <summary>storybook.spec.ts</summary>
82
+
83
+ ```ts
84
+ import path from 'path'
85
+ import { BrowserContext } from '@playwright/test'
86
+ import { playAudit } from 'playwright-lighthouse'
87
+ import {
88
+ playwrightLighthouseTest,
89
+ getScores,
90
+ writeCsvResult,
91
+ writeHtmlListEntryWithRetry,
92
+ LighthouseResult,
93
+ StorybookIndexStory,
94
+ storybookPlaywright,
95
+ } from 'lighthouse-reporting'
96
+
97
+ playwrightLighthouseTest.setTimeout(60000)
98
+ const reportDir = path.join(process.cwd(), process.env.LH_REPORT_DIR as string)
99
+ const htmlFilePath = path.join(reportDir, 'index.html')
100
+
101
+ const stories = storybookPlaywright.getStories('./storybook-static/index.json', (story) => {
102
+ // skip docs, etc
103
+ if (story.type !== 'story') {
104
+ return false
105
+ }
106
+ // only include stories with test tag
107
+ if (!story.tags.includes('test')) {
108
+ return false
109
+ }
110
+ return true
111
+ })
112
+
113
+ stories.forEach((story) => {
114
+ playwrightLighthouseTest(`${story.title} - ${story.name}`, async ({ context, port, baseURL }) => {
115
+ await runLighthouse(story, context, port, baseURL)
116
+ await storybookPlaywright.captureScreenshot(story, context)
117
+ })
118
+ })
119
+
120
+ const runLighthouse = async (story: StorybookIndexStory, context: BrowserContext, port: number, baseURL?: string) => {
121
+ const onlyCategories = ['accessibility']
122
+ const thresholds = { accessibility: 100 }
123
+ const name = story.id
124
+
125
+ const page = context.pages()[0]
126
+ await page.goto(`/iframe.html?id=${story.id}`)
127
+
128
+ const result: LighthouseResult = await playAudit({
129
+ url: baseURL + `/iframe.html?id=${story.id}`,
130
+ port,
131
+ thresholds,
132
+ reports: {
133
+ formats: {
134
+ html: true,
135
+ },
136
+ name,
137
+ directory: reportDir,
138
+ },
139
+ opts: {
140
+ onlyCategories,
141
+ screenEmulation: { disabled: true },
142
+ },
143
+ disableLogs: true,
144
+ ignoreError: true,
145
+ })
146
+
147
+ const scores = getScores(result)
148
+ await writeCsvResult(reportDir, name, scores, thresholds)
149
+ await writeHtmlListEntryWithRetry(htmlFilePath, name, scores, thresholds, result.comparisonError)
150
+ }
151
+ ```
152
+
153
+ </details>
154
+
155
+ ### Configs examples
156
+
157
+ <details>
158
+ <summary>playwright.storybook.config.ts</summary>
159
+
160
+ ```ts
161
+ import { PlaywrightTestConfig } from '@playwright/test'
162
+
163
+ const baseURL = 'http://127.0.0.1:6009'
164
+ // process.env.LH_REPORT_DIR = 'lighthouse-storybook' // adjust lighthouse output folder if required
165
+
166
+ const config: PlaywrightTestConfig = {
167
+ use: {
168
+ viewport: { width: 1280, height: 820 },
169
+ ignoreHTTPSErrors: true,
170
+ acceptDownloads: false,
171
+ trace: 'off',
172
+ baseURL,
173
+ screenshot: { mode: 'off' },
174
+ },
175
+ projects: [
176
+ {
177
+ name: 'chromium',
178
+ use: {
179
+ browserName: 'chromium',
180
+ launchOptions: { args: ['--disable-gpu'] },
181
+ },
182
+ retries: 0,
183
+ },
184
+ ],
185
+ expect: { toMatchSnapshot: { threshold: 0.2 } },
186
+ reporter: 'line',
187
+ testDir: 'test/storybook',
188
+ testMatch: '*.spec.ts',
189
+ fullyParallel: true,
190
+ globalSetup: './src/global-setup.ts',
191
+ globalTeardown: './src/global-teardown.ts',
192
+ forbidOnly: true,
193
+ webServer: [
194
+ {
195
+ command: 'npx http-server ./storybook-static --port 6009 --silent',
196
+ url: `${baseURL}/index.json`,
197
+ timeout: 15 * 1000,
198
+ reuseExistingServer: false,
199
+ ignoreHTTPSErrors: true,
200
+ },
201
+ ],
202
+ }
203
+ export default config
204
+ ```
205
+
206
+ </details>
207
+
208
+ <details>
209
+ <summary>global-setup.ts</summary>
210
+
211
+ ```ts
212
+ import { PlaywrightTestConfig } from '@playwright/test'
213
+
214
+ const baseURL = 'http://127.0.0.1:6009'
215
+ // process.env.LH_REPORT_DIR = 'lighthouse-storybook' // adjust lighthouse output folder if required
216
+
217
+ const config: PlaywrightTestConfig = {
218
+ use: {
219
+ viewport: { width: 1280, height: 820 },
220
+ ignoreHTTPSErrors: true,
221
+ acceptDownloads: false,
222
+ trace: 'off',
223
+ baseURL,
224
+ screenshot: { mode: 'off' },
225
+ },
226
+ projects: [
227
+ {
228
+ name: 'chromium',
229
+ use: {
230
+ browserName: 'chromium',
231
+ launchOptions: { args: ['--disable-gpu'] },
232
+ },
233
+ retries: 0,
234
+ },
235
+ ],
236
+ expect: { toMatchSnapshot: { threshold: 0.2 } },
237
+ reporter: 'line',
238
+ testDir: 'test/storybook',
239
+ testMatch: '*.spec.ts',
240
+ fullyParallel: true,
241
+ globalSetup: './src/global-setup.ts',
242
+ globalTeardown: './src/global-teardown.ts',
243
+ forbidOnly: true,
244
+ webServer: [
245
+ {
246
+ command: 'npx http-server ./storybook-static --port 6009 --silent',
247
+ url: `${baseURL}/index.json`,
248
+ timeout: 15 * 1000,
249
+ reuseExistingServer: false,
250
+ ignoreHTTPSErrors: true,
251
+ },
252
+ ],
253
+ }
254
+ export default config
255
+ ```
256
+
257
+ </details>
258
+
259
+ <details>
260
+ <summary>global-teardown.ts</summary>
261
+
262
+ ```ts
263
+ import { lighthousePlaywrightTeardown } from 'lighthouse-reporting'
264
+
265
+ async function globalTeardown() {
266
+ await lighthousePlaywrightTeardown()
267
+ }
268
+
269
+ export default globalTeardown
270
+ ```
271
+
272
+ </details>
273
+
274
+ ## Jenkins
275
+
276
+ Plugins used [HTML Publisher](https://plugins.jenkins.io/htmlpublisher/) and [Plot](https://plugins.jenkins.io/plot/).
277
+
278
+ <details>
279
+ <summary>Jenkinsfile</summary>
280
+
281
+ ```
282
+ stage('Lighthouse') {
283
+ steps {
284
+ # run playwright-lighthouse tests
285
+ }
286
+
287
+ post {
288
+ always {
289
+ # html report per build
290
+ publishHTML(target: [
291
+ reportName : 'Lighthouse',
292
+ reportDir : "$WORKSPACE/lighthouse",
293
+ reportFiles : 'index.html',
294
+ keepAll : true,
295
+ alwaysLinkToLastBuild: true,
296
+ allowMissing : false
297
+ ])
298
+
299
+ # csv trend history report
300
+ script {
301
+ csvFiles = findFiles(glob: 'lighthouse/*.csv')
302
+ for (csvFile in csvFiles) {
303
+ filePath = "${csvFile}"
304
+ plot(csvFileName: filePath.substring(filePath.lastIndexOf("/") + 1),
305
+ csvSeries: [[displayTableFlag: false, exclusionValues: '', file: filePath, inclusionFlag: 'OFF', url: '']],
306
+ exclZero: true,
307
+ group: 'my app',
308
+ numBuilds: '100',
309
+ style: 'line',
310
+ title: filePath.substring(filePath.lastIndexOf("/") + 1, filePath.indexOf(".")),
311
+ yaxis: 'score',
312
+ yaxisMaximum: '101',
313
+ yaxisMinimum: '15') # adjust scale
314
+ }
315
+ }
316
+ }
317
+ }
318
+ }
319
+ ```
320
+
321
+ </details>
10
322
 
11
- ### GitHub Actions
323
+ ## GitHub Actions
12
324
 
13
325
  TODO
@@ -202,12 +202,15 @@
202
202
  const stories = Object.values(storybookIndexJson.entries).filter(storyFilterFn);
203
203
  return stories;
204
204
  },
205
- captureScreenshot: async (story, context, screenshotOptions) => {
205
+ captureScreenshot: async (story, context, screenshotOptions, actionBeforeScreenshot) => {
206
206
  const page = context.pages()[0];
207
207
  await page.goto(`/iframe.html?id=${story.id}`);
208
208
  await test.expect(page.locator(".sb-show-main")).toBeVisible();
209
209
  await test.expect(page.locator(".sb-show-errordisplay")).not.toBeVisible();
210
210
  await page.waitForLoadState("networkidle");
211
+ if (actionBeforeScreenshot) {
212
+ await actionBeforeScreenshot(page);
213
+ }
211
214
  await test.expect(page).toHaveScreenshot(`${story.id}.png`, screenshotOptions);
212
215
  }
213
216
  };
@@ -12,12 +12,15 @@ const storybookPlaywright = {
12
12
  const stories = Object.values(storybookIndexJson.entries).filter(storyFilterFn);
13
13
  return stories;
14
14
  },
15
- captureScreenshot: async (story, context, screenshotOptions) => {
15
+ captureScreenshot: async (story, context, screenshotOptions, actionBeforeScreenshot) => {
16
16
  const page = context.pages()[0];
17
17
  await page.goto(`/iframe.html?id=${story.id}`);
18
18
  await test.expect(page.locator(".sb-show-main")).toBeVisible();
19
19
  await test.expect(page.locator(".sb-show-errordisplay")).not.toBeVisible();
20
20
  await page.waitForLoadState("networkidle");
21
+ if (actionBeforeScreenshot) {
22
+ await actionBeforeScreenshot(page);
23
+ }
21
24
  await test.expect(page).toHaveScreenshot(`${story.id}.png`, screenshotOptions);
22
25
  }
23
26
  };
@@ -1,4 +1,4 @@
1
- import { BrowserContext } from '@playwright/test';
1
+ import { BrowserContext, Locator, Page } from '@playwright/test';
2
2
  export interface StorybookIndexStory {
3
3
  id: string;
4
4
  title: string;
@@ -46,6 +46,49 @@ export declare const storybookPlaywright: {
46
46
  */
47
47
  height: number;
48
48
  };
49
- }) => Promise<void>;
49
+ /**
50
+ * When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Defaults to
51
+ * `false`.
52
+ */
53
+ fullPage?: boolean;
54
+ /**
55
+ * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
56
+ * box `#FF00FF` that completely covers its bounding box.
57
+ */
58
+ mask?: Array<Locator>;
59
+ /**
60
+ * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is
61
+ * configurable with `TestConfig.expect`. Unset by default.
62
+ */
63
+ maxDiffPixelRatio?: number;
64
+ /**
65
+ * An acceptable amount of pixels that could be different. Default is configurable with `TestConfig.expect`. Unset by
66
+ * default.
67
+ */
68
+ maxDiffPixels?: number;
69
+ /**
70
+ * Hides default white background and allows capturing screenshots with transparency. Not applicable to `jpeg` images.
71
+ * Defaults to `false`.
72
+ */
73
+ omitBackground?: boolean;
74
+ /**
75
+ * When set to `"css"`, screenshot will have a single pixel per each css pixel on the page. For high-dpi devices, this
76
+ * will keep screenshots small. Using `"device"` option will produce a single pixel per each device pixel, so
77
+ * screenshots of high-dpi devices will be twice as large or even larger.
78
+ *
79
+ * Defaults to `"css"`.
80
+ */
81
+ scale?: 'css' | 'device';
82
+ /**
83
+ * An acceptable perceived color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between the
84
+ * same pixel in compared images, between zero (strict) and one (lax), default is configurable with
85
+ * `TestConfig.expect`. Defaults to `0.2`.
86
+ */
87
+ threshold?: number;
88
+ /**
89
+ * Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
90
+ */
91
+ timeout?: number;
92
+ }, actionBeforeScreenshot?: ((page: Page) => Promise<void>) | undefined) => Promise<void>;
50
93
  };
51
94
  export {};
@@ -10,12 +10,15 @@ const storybookPlaywright = {
10
10
  const stories = Object.values(storybookIndexJson.entries).filter(storyFilterFn);
11
11
  return stories;
12
12
  },
13
- captureScreenshot: async (story, context, screenshotOptions) => {
13
+ captureScreenshot: async (story, context, screenshotOptions, actionBeforeScreenshot) => {
14
14
  const page = context.pages()[0];
15
15
  await page.goto(`/iframe.html?id=${story.id}`);
16
16
  await expect(page.locator(".sb-show-main")).toBeVisible();
17
17
  await expect(page.locator(".sb-show-errordisplay")).not.toBeVisible();
18
18
  await page.waitForLoadState("networkidle");
19
+ if (actionBeforeScreenshot) {
20
+ await actionBeforeScreenshot(page);
21
+ }
19
22
  await expect(page).toHaveScreenshot(`${story.id}.png`, screenshotOptions);
20
23
  }
21
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lighthouse-reporting",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "vite build && vite build -c vite.umd.config.ts && tsc -p ./tsconfig.build.json",
@@ -29,7 +29,7 @@
29
29
  "devDependencies": {
30
30
  "@playwright/test": "^1.34.3",
31
31
  "@types/fs-extra": "^11.0.1",
32
- "@types/node": "^20.2.3",
32
+ "@types/node": "^20.2.4",
33
33
  "@typescript-eslint/eslint-plugin": "^5.59.7",
34
34
  "@typescript-eslint/parser": "^5.59.7",
35
35
  "eslint": "^8.41.0",
@@ -41,7 +41,7 @@
41
41
  "prettier": "^2.8.8",
42
42
  "semantic-release": "^21.0.2",
43
43
  "typescript": "^5.0.4",
44
- "vite": "^4.3.8",
44
+ "vite": "^4.3.9",
45
45
  "vite-plugin-static-copy": "^0.15.0"
46
46
  },
47
47
  "dependencies": {