ftmocks-utils 1.4.7 → 1.4.8
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/package.json +4 -2
- package/src/event-run-utils.js +46 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ftmocks-utils",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
4
4
|
"description": "Util functions for FtMocks",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
"homepage": "https://github.com/SodhanaLibrary/ftmocks-utils#readme",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"uui": "^1.0.7",
|
|
22
|
-
"uuid": "^11.1.0"
|
|
22
|
+
"uuid": "^11.1.0",
|
|
23
|
+
"pixelmatch": "^5.3.1",
|
|
24
|
+
"pngjs": "^7.0.1"
|
|
23
25
|
}
|
|
24
26
|
}
|
package/src/event-run-utils.js
CHANGED
|
@@ -2,6 +2,38 @@ const path = require("path");
|
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const { getMockDir, nameToFolder } = require("./common-utils");
|
|
4
4
|
|
|
5
|
+
const matchAndReplaceScreenshot = async (page, event, screenshotsDir) => {
|
|
6
|
+
const pixelmatch = (await import("pixelmatch")).default;
|
|
7
|
+
const { PNG } = (await import("pngjs")).default;
|
|
8
|
+
|
|
9
|
+
const file = path.join(screenshotsDir, `${event.id}.png`);
|
|
10
|
+
const newFile = path.join(screenshotsDir, `__temp_${event.id}.png`);
|
|
11
|
+
|
|
12
|
+
await page.screenshot({ path: newFile, fullPage: false });
|
|
13
|
+
|
|
14
|
+
if (!fs.existsSync(file)) {
|
|
15
|
+
fs.renameSync(newFile, file);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const img1 = PNG.sync.read(fs.readFileSync(file));
|
|
20
|
+
const img2 = PNG.sync.read(fs.readFileSync(newFile));
|
|
21
|
+
|
|
22
|
+
const diff = pixelmatch(img1.data, img2.data, null, img1.width, img1.height, {
|
|
23
|
+
threshold: 0.1,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (diff > 0) {
|
|
27
|
+
console.log(`Screenshot changed → replacing: ${file}`);
|
|
28
|
+
fs.renameSync(newFile, file); // overwrite only when mismatch
|
|
29
|
+
return true;
|
|
30
|
+
} else {
|
|
31
|
+
console.log(`Screenshot did not change → removing temp file: ${newFile}`);
|
|
32
|
+
fs.unlinkSync(newFile); // no change → remove temp file
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
5
37
|
const getLocator = async (page, event) => {
|
|
6
38
|
// Check if the event.target exists on the page before returning it.
|
|
7
39
|
console.log("➡ Getting locator for event", event);
|
|
@@ -106,16 +138,20 @@ const runEvent = async ({
|
|
|
106
138
|
const beforeEvent = async () => {
|
|
107
139
|
await page.waitForTimeout(delay);
|
|
108
140
|
if (screenshots) {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
141
|
+
const replaced = await matchAndReplaceScreenshot(
|
|
142
|
+
page,
|
|
143
|
+
event,
|
|
144
|
+
screenshotsDir
|
|
145
|
+
);
|
|
146
|
+
if (replaced) {
|
|
147
|
+
const locator = await getLocator(page, event);
|
|
148
|
+
const position = await getSelectorPosition(page, locator);
|
|
149
|
+
event.screenshotInfo = {
|
|
150
|
+
name: `${event.id}.png`,
|
|
151
|
+
position,
|
|
152
|
+
time: new Date().toISOString(),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
119
155
|
}
|
|
120
156
|
if (healSelectors) {
|
|
121
157
|
const locator = await getLocator(page, event);
|