@midscene/web 0.7.1 → 0.7.2-beta-20241024094141.0

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/dist/es/debug.js CHANGED
@@ -5,6 +5,26 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
5
5
  return require.apply(this, arguments);
6
6
  throw Error('Dynamic require of "' + x + '" is not supported');
7
7
  });
8
+ var __async = (__this, __arguments, generator) => {
9
+ return new Promise((resolve, reject) => {
10
+ var fulfilled = (value) => {
11
+ try {
12
+ step(generator.next(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var rejected = (value) => {
18
+ try {
19
+ step(generator.throw(value));
20
+ } catch (e) {
21
+ reject(e);
22
+ }
23
+ };
24
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
25
+ step((generator = generator.apply(__this, __arguments)).next());
26
+ });
27
+ };
8
28
 
9
29
  // src/debug/index.ts
10
30
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
@@ -23,67 +43,67 @@ import {
23
43
  resizeImg,
24
44
  saveBase64Image
25
45
  } from "@midscene/shared/img";
26
- async function generateExtractData(page, targetDir, saveImgType) {
27
- const file = await page.screenshot();
28
- const screenshotBuffer = readFileSync(file);
29
- const inputImgBase64 = screenshotBuffer.toString("base64");
30
- const {
31
- elementsPositionInfo,
32
- captureElementSnapshot,
33
- elementsPositionInfoWithoutText
34
- } = await getElementInfos(page);
35
- const inputImagePath = path.join(targetDir, "input.png");
36
- const outputImagePath = path.join(targetDir, "output.png");
37
- const outputWithoutTextImgPath = path.join(
38
- targetDir,
39
- "output_without_text.png"
40
- );
41
- const resizeOutputImgPath = path.join(targetDir, "resize-output.png");
42
- const snapshotJsonPath = path.join(targetDir, "element-snapshot.json");
43
- const {
44
- compositeElementInfoImgBase64,
45
- compositeElementInfoImgWithoutTextBase64
46
- } = await processImageElementInfo({
47
- elementsPositionInfo,
48
- elementsPositionInfoWithoutText,
49
- inputImgBase64
50
- });
51
- const resizeImgBase64 = await resizeImg(inputImgBase64);
52
- const existingSnapshot = existsSync(snapshotJsonPath) ? JSON.parse(readFileSync(snapshotJsonPath, "utf-8")) : null;
53
- if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
54
- console.log("skip save snapshot for ", targetDir);
55
- return;
56
- }
57
- if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
58
- writeFileSyncWithDir(
59
- snapshotJsonPath,
60
- JSON.stringify(captureElementSnapshot, null, 2)
46
+ function generateExtractData(page, targetDir, saveImgType) {
47
+ return __async(this, null, function* () {
48
+ const inputImgBase64 = yield page.screenshotBase64();
49
+ const {
50
+ elementsPositionInfo,
51
+ captureElementSnapshot,
52
+ elementsPositionInfoWithoutText
53
+ } = yield getElementInfos(page);
54
+ const inputImagePath = path.join(targetDir, "input.png");
55
+ const outputImagePath = path.join(targetDir, "output.png");
56
+ const outputWithoutTextImgPath = path.join(
57
+ targetDir,
58
+ "output_without_text.png"
61
59
  );
62
- }
63
- if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
64
- await saveBase64Image({
65
- base64Data: inputImgBase64,
66
- outputPath: inputImagePath
67
- });
68
- }
69
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
70
- await saveBase64Image({
71
- base64Data: compositeElementInfoImgBase64,
72
- outputPath: outputImagePath
60
+ const resizeOutputImgPath = path.join(targetDir, "resize-output.png");
61
+ const snapshotJsonPath = path.join(targetDir, "element-snapshot.json");
62
+ const {
63
+ compositeElementInfoImgBase64,
64
+ compositeElementInfoImgWithoutTextBase64
65
+ } = yield processImageElementInfo({
66
+ elementsPositionInfo,
67
+ elementsPositionInfoWithoutText,
68
+ inputImgBase64
73
69
  });
74
- }
75
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
76
- await saveBase64Image({
77
- base64Data: compositeElementInfoImgWithoutTextBase64,
78
- outputPath: outputWithoutTextImgPath
79
- });
80
- }
81
- if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
82
- await saveBase64Image({
83
- base64Data: resizeImgBase64,
84
- outputPath: resizeOutputImgPath
85
- });
86
- }
70
+ const resizeImgBase64 = yield resizeImg(inputImgBase64);
71
+ const existingSnapshot = existsSync(snapshotJsonPath) ? JSON.parse(readFileSync(snapshotJsonPath, "utf-8")) : null;
72
+ if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
73
+ console.log("skip save snapshot for ", targetDir);
74
+ return;
75
+ }
76
+ if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
77
+ writeFileSyncWithDir(
78
+ snapshotJsonPath,
79
+ JSON.stringify(captureElementSnapshot, null, 2)
80
+ );
81
+ }
82
+ if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
83
+ yield saveBase64Image({
84
+ base64Data: inputImgBase64,
85
+ outputPath: inputImagePath
86
+ });
87
+ }
88
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
89
+ yield saveBase64Image({
90
+ base64Data: compositeElementInfoImgBase64,
91
+ outputPath: outputImagePath
92
+ });
93
+ }
94
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
95
+ yield saveBase64Image({
96
+ base64Data: compositeElementInfoImgWithoutTextBase64,
97
+ outputPath: outputWithoutTextImgPath
98
+ });
99
+ }
100
+ if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
101
+ yield saveBase64Image({
102
+ base64Data: resizeImgBase64,
103
+ outputPath: resizeOutputImgPath
104
+ });
105
+ }
106
+ });
87
107
  }
88
108
  function generateTestDataPath(testDataName) {
89
109
  const modulePath = __require.resolve("@midscene/core").replace("dist/lib/index.js", "");
@@ -105,21 +125,23 @@ function writeFileSyncWithDir(filePath, content, options = {}) {
105
125
  ensureDirectoryExistence(filePath);
106
126
  writeFileSync(filePath, content, options);
107
127
  }
108
- async function getElementInfos(page) {
109
- const captureElementSnapshot = await page.getElementInfos();
110
- const elementsPositionInfoWithoutText = captureElementSnapshot.filter(
111
- (elementInfo) => {
112
- if (elementInfo.attributes.nodeType === NodeType.TEXT) {
113
- return false;
128
+ function getElementInfos(page) {
129
+ return __async(this, null, function* () {
130
+ const captureElementSnapshot = yield page.getElementInfos();
131
+ const elementsPositionInfoWithoutText = captureElementSnapshot.filter(
132
+ (elementInfo) => {
133
+ if (elementInfo.attributes.nodeType === NodeType.TEXT) {
134
+ return false;
135
+ }
136
+ return true;
114
137
  }
115
- return true;
116
- }
117
- );
118
- return {
119
- elementsPositionInfo: captureElementSnapshot,
120
- captureElementSnapshot,
121
- elementsPositionInfoWithoutText
122
- };
138
+ );
139
+ return {
140
+ elementsPositionInfo: captureElementSnapshot,
141
+ captureElementSnapshot,
142
+ elementsPositionInfoWithoutText
143
+ };
144
+ });
123
145
  }
124
146
  export {
125
147
  generateExtractData,