@quickpickle/playwright 0.9.4 → 0.9.6

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.
@@ -0,0 +1,86 @@
1
+ import { expect } from '@playwright/test';
2
+ import { PNG } from 'pngjs';
3
+ import { promises } from 'node:fs';
4
+ import path from 'node:path';
5
+ import pixelmatch from 'pixelmatch';
6
+ import { defaultsDeep } from 'lodash-es';
7
+
8
+ const defaultOptions = {
9
+ // Options for the comparison
10
+ maxDiffPercentage: 0,
11
+ // Options for the screenshot
12
+ fullPage: true,
13
+ omitBackground: false,
14
+ mask: [],
15
+ maskColor: 'rgb(255,255,255)',
16
+ timeout: 5000,
17
+ // Options for pixelmatch
18
+ threshold: 0.1,
19
+ alpha: 0.6,
20
+ };
21
+ async function compareImages(actual, expected, opts) {
22
+ const actualImage = PNG.sync.read(actual);
23
+ const expectedImage = PNG.sync.read(expected);
24
+ const { width, height } = actualImage;
25
+ const diffImage = new PNG({ width, height });
26
+ const mismatchedPixels = pixelmatch(actualImage.data, expectedImage.data, diffImage.data, width, height, opts);
27
+ const totalPixels = width * height;
28
+ const diffPercentage = (mismatchedPixels / totalPixels) * 100;
29
+ const pass = diffPercentage <= opts.maxDiffPercentage;
30
+ return { pass, diffPercentage, image: diffImage };
31
+ }
32
+ async function customToHaveScreenshot(pageOrLocator, snapshotPath, opts) {
33
+ const options = defaultsDeep(opts, defaultOptions);
34
+ const pathparts = snapshotPath.split('/');
35
+ const name = pathparts.pop();
36
+ const screenshotDir = pathparts.join('/');
37
+ const filepath = path.join(screenshotDir, name || '');
38
+ const actualScreenshot = await pageOrLocator.screenshot(options);
39
+ try {
40
+ let expectedScreenshot;
41
+ try {
42
+ expectedScreenshot = await promises.readFile(snapshotPath);
43
+ }
44
+ catch (error) {
45
+ // If the snapshot doesn't exist, save the current screenshot and pass the test
46
+ await promises.mkdir(screenshotDir, { recursive: true });
47
+ await promises.writeFile(snapshotPath, actualScreenshot);
48
+ return {
49
+ pass: true,
50
+ message: () => `Screenshot saved as new snapshot: ${snapshotPath}`,
51
+ };
52
+ }
53
+ const { pass, diffPercentage, image } = await compareImages(actualScreenshot, expectedScreenshot, options);
54
+ if (!pass) {
55
+ const filepath = path.join(screenshotDir, name || '');
56
+ await promises.mkdir(screenshotDir, { recursive: true });
57
+ await promises.writeFile(`${filepath}.diff.png`, PNG.sync.write(image));
58
+ await promises.writeFile(`${filepath}.actual.png`, actualScreenshot);
59
+ return {
60
+ pass: false,
61
+ message: () => `Screenshot does not match the snapshot.\nDiff percentage: ${diffPercentage.toFixed(2)}%\nDiff image saved at: ${filepath}.(diff|actual).png`,
62
+ };
63
+ }
64
+ return {
65
+ pass: true,
66
+ message: () => `Screenshot matches the snapshot: ${snapshotPath}`,
67
+ };
68
+ }
69
+ catch (error) {
70
+ await promises.mkdir(screenshotDir, { recursive: true });
71
+ await promises.writeFile(`${filepath}.actual.png`, actualScreenshot);
72
+ return {
73
+ pass: false,
74
+ message: () => `Failed to compare screenshots: ${error.message}`,
75
+ };
76
+ }
77
+ }
78
+ // Override the default implementation
79
+ expect.extend({
80
+ async toMatchScreenshot(received, nameOrOptions, optOptions) {
81
+ const name = typeof nameOrOptions === 'string' ? nameOrOptions : 'screenshot';
82
+ const options = typeof nameOrOptions === 'object' ? nameOrOptions : optOptions;
83
+ return customToHaveScreenshot.call(this, received, name, options);
84
+ },
85
+ });
86
+ //# sourceMappingURL=snapshotMatcher.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshotMatcher.mjs","sources":["../src/snapshotMatcher.ts"],"sourcesContent":[null],"names":["fs","playwrightExpect"],"mappings":";;;;;;;AA4BA,MAAM,cAAc,GAAG;;AAErB,IAAA,iBAAiB,EAAE,CAAC;;AAGpB,IAAA,QAAQ,EAAE,IAAI;AACd,IAAA,cAAc,EAAE,KAAK;AACrB,IAAA,IAAI,EAAE,EAAE;AACR,IAAA,SAAS,EAAE,kBAAkB;AAC7B,IAAA,OAAO,EAAE,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AAO;AACL,gBAAA,IAAI,EAAE,KAAK;AACX,gBAAA,OAAO,EAAE,MAAM,CAA6D,0DAAA,EAAA,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA,wBAAA,EAA2B,QAAQ,CAAoB,kBAAA,CAAA;aAC7J,CAAC;SACH;QAED,OAAO;AACL,YAAA,IAAI,EAAE,IAAI;AACV,YAAA,OAAO,EAAE,MAAM,CAAA,iCAAA,EAAoC,YAAY,CAAE,CAAA;SAClE,CAAC;KACH;IAAC,OAAO,KAAS,EAAE;AAClB,QAAA,MAAMA,QAAE,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACnD,MAAMA,QAAE,CAAC,SAAS,CAAC,CAAA,EAAG,QAAQ,CAAa,WAAA,CAAA,EAAE,gBAAgB,CAAC,CAAA;QAC9D,OAAO;AACL,YAAA,IAAI,EAAE,KAAK;YACX,OAAO,EAAE,MAAM,kCAAkC,KAAK,CAAC,OAAO,CAAE,CAAA;SACjE,CAAC;KACH;AACH,CAAC;AAWD;AACAC,MAAgB,CAAC,MAAM,CAAC;AACtB,IAAA,MAAM,iBAAiB,CAAC,QAAwB,EAAE,aAAyD,EAAE,UAA6C,EAAA;AACxJ,QAAA,MAAM,IAAI,GAAG,OAAO,aAAa,KAAK,QAAQ,GAAG,aAAa,GAAG,YAAY,CAAC;AAC9E,QAAA,MAAM,OAAO,GAAG,OAAO,aAAa,KAAK,QAAQ,GAAG,aAAa,GAAG,UAAU,CAAC;AAC/E,QAAA,OAAO,sBAAsB,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;KACnE;AACF,CAAA,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quickpickle/playwright",
3
- "version": "0.9.4",
3
+ "version": "0.9.6",
4
4
  "description": "Support files for running tests with Playwright using QuickPickle (Gherkin in Vitest).",
5
5
  "keywords": [
6
6
  "BDD",
@@ -61,16 +61,23 @@
61
61
  "author": "David Hunt",
62
62
  "dependencies": {
63
63
  "@playwright/test": "^1.48.0",
64
+ "jest-image-snapshot": "^6.4.0",
64
65
  "lodash-es": "^4.17.21",
66
+ "pixelmatch": "^6.0.0",
65
67
  "playwright": "^1.48.0",
66
68
  "vite": "^5.0.11",
67
- "quickpickle": "^1.2.2"
69
+ "quickpickle": "^1.2.3"
68
70
  },
69
71
  "devDependencies": {
70
72
  "@rollup/plugin-replace": "^6.0.1",
71
73
  "@rollup/plugin-typescript": "^12.1.0",
74
+ "@types/jest-image-snapshot": "^6.4.0",
72
75
  "@types/lodash-es": "^4.17.12",
76
+ "@types/pixelmatch": "^5.2.6",
77
+ "@types/pngjs": "^6.0.5",
73
78
  "fast-glob": "^3.3.2",
79
+ "playwright-core": "^1.48.0",
80
+ "pngjs": "^7.0.0",
74
81
  "rollup": "^3.20.7",
75
82
  "typescript": "^5.6.2",
76
83
  "vitest": "^2.1.2"