ftmocks-utils 1.4.6 → 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 +48 -11
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);
|
|
@@ -88,7 +120,8 @@ const healSelector = async (page, event, selector, position) => {
|
|
|
88
120
|
const getSelectorPosition = async (page, selector) => {
|
|
89
121
|
const element = await page.locator(selector).elementHandle();
|
|
90
122
|
const position = await element.boundingBox();
|
|
91
|
-
|
|
123
|
+
position.windowWidth = page.viewportSize().width;
|
|
124
|
+
position.windowHeight = page.viewportSize().height;
|
|
92
125
|
return position;
|
|
93
126
|
};
|
|
94
127
|
|
|
@@ -105,16 +138,20 @@ const runEvent = async ({
|
|
|
105
138
|
const beforeEvent = async () => {
|
|
106
139
|
await page.waitForTimeout(delay);
|
|
107
140
|
if (screenshots) {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
}
|
|
118
155
|
}
|
|
119
156
|
if (healSelectors) {
|
|
120
157
|
const locator = await getLocator(page, event);
|