@midscene/web 0.7.2 → 0.7.3-beta-20241104100519.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/lib/debug.js CHANGED
@@ -1,151 +1,150 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/debug/index.ts
31
- var debug_exports = {};
32
- __export(debug_exports, {
33
- generateExtractData: () => generateExtractData,
34
- generateTestDataPath: () => generateTestDataPath,
35
- getElementInfos: () => getElementInfos,
36
- writeFileSyncWithDir: () => writeFileSyncWithDir
37
- });
38
- module.exports = __toCommonJS(debug_exports);
39
- var import_node_fs = require("fs");
40
- var import_node_path = __toESM(require("path"));
41
-
42
- // src/extractor/constants.ts
43
- var import_constants = require("@midscene/shared/constants");
44
-
45
- // src/debug/index.ts
46
- var import_img = require("@midscene/shared/img");
47
- async function generateExtractData(page, targetDir, saveImgType) {
48
- const file = await page.screenshot();
49
- const screenshotBuffer = (0, import_node_fs.readFileSync)(file);
50
- const inputImgBase64 = screenshotBuffer.toString("base64");
51
- const {
52
- elementsPositionInfo,
53
- captureElementSnapshot,
54
- elementsPositionInfoWithoutText
55
- } = await getElementInfos(page);
56
- const inputImagePath = import_node_path.default.join(targetDir, "input.png");
57
- const outputImagePath = import_node_path.default.join(targetDir, "output.png");
58
- const outputWithoutTextImgPath = import_node_path.default.join(
59
- targetDir,
60
- "output_without_text.png"
61
- );
62
- const resizeOutputImgPath = import_node_path.default.join(targetDir, "resize-output.png");
63
- const snapshotJsonPath = import_node_path.default.join(targetDir, "element-snapshot.json");
64
- const {
65
- compositeElementInfoImgBase64,
66
- compositeElementInfoImgWithoutTextBase64
67
- } = await (0, import_img.processImageElementInfo)({
68
- elementsPositionInfo,
69
- elementsPositionInfoWithoutText,
70
- inputImgBase64
71
- });
72
- const resizeImgBase64 = await (0, import_img.resizeImg)(inputImgBase64);
73
- const existingSnapshot = (0, import_node_fs.existsSync)(snapshotJsonPath) ? JSON.parse((0, import_node_fs.readFileSync)(snapshotJsonPath, "utf-8")) : null;
74
- if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
75
- console.log("skip save snapshot for ", targetDir);
76
- return;
77
- }
78
- if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
79
- writeFileSyncWithDir(
80
- snapshotJsonPath,
81
- JSON.stringify(captureElementSnapshot, null, 2)
82
- );
83
- }
84
- if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
85
- await (0, import_img.saveBase64Image)({
86
- base64Data: inputImgBase64,
87
- outputPath: inputImagePath
88
- });
89
- }
90
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
91
- await (0, import_img.saveBase64Image)({
92
- base64Data: compositeElementInfoImgBase64,
93
- outputPath: outputImagePath
1
+ (function(global, factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("fs"), require("path"), require("@midscene/shared/constants"), require("@midscene/shared/img"));
3
+ else if (typeof define === "function" && define.amd) define([
4
+ "exports",
5
+ "fs",
6
+ "path",
7
+ "@midscene/shared/constants",
8
+ "@midscene/shared/img"
9
+ ], factory);
10
+ else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.debug = {}, global.fs, global.path, global.constants, global.img);
11
+ })(this, function(exports, _fs, _path, _constants, _img) {
12
+ "use strict";
13
+ Object.defineProperty(exports, "__esModule", {
14
+ value: true
94
15
  });
95
- }
96
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
97
- await (0, import_img.saveBase64Image)({
98
- base64Data: compositeElementInfoImgWithoutTextBase64,
99
- outputPath: outputWithoutTextImgPath
16
+ function _export(target, all) {
17
+ for(var name in all)Object.defineProperty(target, name, {
18
+ enumerable: true,
19
+ get: all[name]
20
+ });
21
+ }
22
+ _export(exports, {
23
+ generateExtractData: function() {
24
+ return generateExtractData;
25
+ },
26
+ generateTestDataPath: function() {
27
+ return generateTestDataPath;
28
+ },
29
+ getElementInfos: function() {
30
+ return getElementInfos;
31
+ },
32
+ writeFileSyncWithDir: function() {
33
+ return writeFileSyncWithDir;
34
+ }
100
35
  });
101
- }
102
- if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
103
- await (0, import_img.saveBase64Image)({
104
- base64Data: resizeImgBase64,
105
- outputPath: resizeOutputImgPath
36
+ _path = /*#__PURE__*/ _interop_require_default(_path);
37
+ function _interop_require_default(obj) {
38
+ return obj && obj.__esModule ? obj : {
39
+ default: obj
40
+ };
41
+ }
42
+ var __require = /* @__PURE__ */ ((x)=>typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
43
+ get: (a, b)=>(typeof require !== "undefined" ? require : a)[b]
44
+ }) : x)(function(x) {
45
+ if (typeof require !== "undefined") return require.apply(this, arguments);
46
+ throw Error('Dynamic require of "' + x + '" is not supported');
106
47
  });
107
- }
108
- }
109
- function generateTestDataPath(testDataName) {
110
- const modulePath = require.resolve("@midscene/core").replace("dist/lib/index.js", "");
111
- const midsceneTestDataPath = import_node_path.default.join(
112
- modulePath,
113
- `tests/ai/evaluate/test-data/${testDataName}`
114
- );
115
- return midsceneTestDataPath;
116
- }
117
- function ensureDirectoryExistence(filePath) {
118
- const dirname = import_node_path.default.dirname(filePath);
119
- if ((0, import_node_fs.existsSync)(dirname)) {
120
- return;
121
- }
122
- ensureDirectoryExistence(dirname);
123
- (0, import_node_fs.mkdirSync)(dirname);
124
- }
125
- function writeFileSyncWithDir(filePath, content, options = {}) {
126
- ensureDirectoryExistence(filePath);
127
- (0, import_node_fs.writeFileSync)(filePath, content, options);
128
- }
129
- async function getElementInfos(page) {
130
- const captureElementSnapshot = await page.getElementInfos();
131
- const elementsPositionInfoWithoutText = captureElementSnapshot.filter(
132
- (elementInfo) => {
133
- if (elementInfo.attributes.nodeType === import_constants.NodeType.TEXT) {
134
- return false;
135
- }
136
- return true;
48
+ var __async = (__this, __arguments, generator)=>{
49
+ return new Promise((resolve, reject)=>{
50
+ var fulfilled = (value)=>{
51
+ try {
52
+ step(generator.next(value));
53
+ } catch (e) {
54
+ reject(e);
55
+ }
56
+ };
57
+ var rejected = (value)=>{
58
+ try {
59
+ step(generator.throw(value));
60
+ } catch (e) {
61
+ reject(e);
62
+ }
63
+ };
64
+ var step = (x)=>x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
65
+ step((generator = generator.apply(__this, __arguments)).next());
66
+ });
67
+ };
68
+ function generateExtractData(page, targetDir, saveImgType) {
69
+ return __async(this, null, function*() {
70
+ const inputImgBase64 = yield page.screenshotBase64();
71
+ const { elementsPositionInfo, captureElementSnapshot, elementsPositionInfoWithoutText } = yield getElementInfos(page);
72
+ const inputImagePath = _path.default.join(targetDir, "input.png");
73
+ const outputImagePath = _path.default.join(targetDir, "output.png");
74
+ const outputWithoutTextImgPath = _path.default.join(targetDir, "output_without_text.png");
75
+ const resizeOutputImgPath = _path.default.join(targetDir, "resize-output.png");
76
+ const snapshotJsonPath = _path.default.join(targetDir, "element-snapshot.json");
77
+ const { compositeElementInfoImgBase64, compositeElementInfoImgWithoutTextBase64 } = yield (0, _img.processImageElementInfo)({
78
+ elementsPositionInfo,
79
+ elementsPositionInfoWithoutText,
80
+ inputImgBase64
81
+ });
82
+ const resizedImg = yield (0, _img.resizeImgBase64)(inputImgBase64, void 0);
83
+ const existingSnapshot = (0, _fs.existsSync)(snapshotJsonPath) ? JSON.parse((0, _fs.readFileSync)(snapshotJsonPath, "utf-8")) : null;
84
+ if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
85
+ console.log("skip save snapshot for ", targetDir);
86
+ return;
87
+ }
88
+ if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
89
+ writeFileSyncWithDir(snapshotJsonPath, JSON.stringify(captureElementSnapshot, null, 2));
90
+ }
91
+ if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
92
+ yield (0, _img.saveBase64Image)({
93
+ base64Data: inputImgBase64,
94
+ outputPath: inputImagePath
95
+ });
96
+ }
97
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
98
+ yield (0, _img.saveBase64Image)({
99
+ base64Data: compositeElementInfoImgBase64,
100
+ outputPath: outputImagePath
101
+ });
102
+ }
103
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
104
+ yield (0, _img.saveBase64Image)({
105
+ base64Data: compositeElementInfoImgWithoutTextBase64,
106
+ outputPath: outputWithoutTextImgPath
107
+ });
108
+ }
109
+ if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
110
+ yield (0, _img.saveBase64Image)({
111
+ base64Data: resizedImg,
112
+ outputPath: resizeOutputImgPath
113
+ });
114
+ }
115
+ });
116
+ }
117
+ function generateTestDataPath(testDataName) {
118
+ const modulePath = __require.resolve("@midscene/core").replace("dist/lib/index.js", "");
119
+ const midsceneTestDataPath = _path.default.join(modulePath, `tests/ai/evaluate/test-data/${testDataName}`);
120
+ return midsceneTestDataPath;
121
+ }
122
+ function ensureDirectoryExistence(filePath) {
123
+ const dirname = _path.default.dirname(filePath);
124
+ if ((0, _fs.existsSync)(dirname)) {
125
+ return;
126
+ }
127
+ ensureDirectoryExistence(dirname);
128
+ (0, _fs.mkdirSync)(dirname);
129
+ }
130
+ function writeFileSyncWithDir(filePath, content, options = {}) {
131
+ ensureDirectoryExistence(filePath);
132
+ (0, _fs.writeFileSync)(filePath, content, options);
133
+ }
134
+ function getElementInfos(page) {
135
+ return __async(this, null, function*() {
136
+ const captureElementSnapshot = yield page.getElementInfos();
137
+ const elementsPositionInfoWithoutText = captureElementSnapshot.filter((elementInfo)=>{
138
+ if (elementInfo.attributes.nodeType === _constants.NodeType.TEXT) {
139
+ return false;
140
+ }
141
+ return true;
142
+ });
143
+ return {
144
+ elementsPositionInfo: captureElementSnapshot,
145
+ captureElementSnapshot,
146
+ elementsPositionInfoWithoutText
147
+ };
148
+ });
137
149
  }
138
- );
139
- return {
140
- elementsPositionInfo: captureElementSnapshot,
141
- captureElementSnapshot,
142
- elementsPositionInfoWithoutText
143
- };
144
- }
145
- // Annotate the CommonJS export names for ESM import in node:
146
- 0 && (module.exports = {
147
- generateExtractData,
148
- generateTestDataPath,
149
- getElementInfos,
150
- writeFileSyncWithDir
151
150
  });