@midscene/web 0.3.2 → 0.3.3

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,559 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined")
11
+ return require.apply(this, arguments);
12
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
+ });
14
+ var __commonJS = (cb, mod) => function __require2() {
15
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
31
+ mod
32
+ ));
33
+
34
+ // ../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js
35
+ var require_dayjs_min = __commonJS({
36
+ "../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.js"(exports, module) {
37
+ "use strict";
38
+ !function(t, e) {
39
+ "object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
40
+ }(exports, function() {
41
+ "use strict";
42
+ var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(t2) {
43
+ var e2 = ["th", "st", "nd", "rd"], n2 = t2 % 100;
44
+ return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
45
+ } }, m = function(t2, e2, n2) {
46
+ var r2 = String(t2);
47
+ return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
48
+ }, v = { s: m, z: function(t2) {
49
+ var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
50
+ return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
51
+ }, m: function t2(e2, n2) {
52
+ if (e2.date() < n2.date())
53
+ return -t2(n2, e2);
54
+ var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
55
+ return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
56
+ }, a: function(t2) {
57
+ return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
58
+ }, p: function(t2) {
59
+ return { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
60
+ }, u: function(t2) {
61
+ return void 0 === t2;
62
+ } }, g = "en", D = {};
63
+ D[g] = M;
64
+ var p = "$isDayjsObject", S = function(t2) {
65
+ return t2 instanceof _ || !(!t2 || !t2[p]);
66
+ }, w = function t2(e2, n2, r2) {
67
+ var i2;
68
+ if (!e2)
69
+ return g;
70
+ if ("string" == typeof e2) {
71
+ var s2 = e2.toLowerCase();
72
+ D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
73
+ var u2 = e2.split("-");
74
+ if (!i2 && u2.length > 1)
75
+ return t2(u2[0]);
76
+ } else {
77
+ var a2 = e2.name;
78
+ D[a2] = e2, i2 = a2;
79
+ }
80
+ return !r2 && i2 && (g = i2), i2 || !r2 && g;
81
+ }, O = function(t2, e2) {
82
+ if (S(t2))
83
+ return t2.clone();
84
+ var n2 = "object" == typeof e2 ? e2 : {};
85
+ return n2.date = t2, n2.args = arguments, new _(n2);
86
+ }, b = v;
87
+ b.l = w, b.i = S, b.w = function(t2, e2) {
88
+ return O(t2, { locale: e2.$L, utc: e2.$u, x: e2.$x, $offset: e2.$offset });
89
+ };
90
+ var _ = function() {
91
+ function M2(t2) {
92
+ this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
93
+ }
94
+ var m2 = M2.prototype;
95
+ return m2.parse = function(t2) {
96
+ this.$d = function(t3) {
97
+ var e2 = t3.date, n2 = t3.utc;
98
+ if (null === e2)
99
+ return /* @__PURE__ */ new Date(NaN);
100
+ if (b.u(e2))
101
+ return /* @__PURE__ */ new Date();
102
+ if (e2 instanceof Date)
103
+ return new Date(e2);
104
+ if ("string" == typeof e2 && !/Z$/i.test(e2)) {
105
+ var r2 = e2.match($);
106
+ if (r2) {
107
+ var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
108
+ return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
109
+ }
110
+ }
111
+ return new Date(e2);
112
+ }(t2), this.init();
113
+ }, m2.init = function() {
114
+ var t2 = this.$d;
115
+ this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
116
+ }, m2.$utils = function() {
117
+ return b;
118
+ }, m2.isValid = function() {
119
+ return !(this.$d.toString() === l);
120
+ }, m2.isSame = function(t2, e2) {
121
+ var n2 = O(t2);
122
+ return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
123
+ }, m2.isAfter = function(t2, e2) {
124
+ return O(t2) < this.startOf(e2);
125
+ }, m2.isBefore = function(t2, e2) {
126
+ return this.endOf(e2) < O(t2);
127
+ }, m2.$g = function(t2, e2, n2) {
128
+ return b.u(t2) ? this[e2] : this.set(n2, t2);
129
+ }, m2.unix = function() {
130
+ return Math.floor(this.valueOf() / 1e3);
131
+ }, m2.valueOf = function() {
132
+ return this.$d.getTime();
133
+ }, m2.startOf = function(t2, e2) {
134
+ var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
135
+ var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
136
+ return r2 ? i2 : i2.endOf(a);
137
+ }, $2 = function(t3, e3) {
138
+ return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e3)), n2);
139
+ }, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
140
+ switch (f2) {
141
+ case h:
142
+ return r2 ? l2(1, 0) : l2(31, 11);
143
+ case c:
144
+ return r2 ? l2(1, M3) : l2(0, M3 + 1);
145
+ case o:
146
+ var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
147
+ return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
148
+ case a:
149
+ case d:
150
+ return $2(v2 + "Hours", 0);
151
+ case u:
152
+ return $2(v2 + "Minutes", 1);
153
+ case s:
154
+ return $2(v2 + "Seconds", 2);
155
+ case i:
156
+ return $2(v2 + "Milliseconds", 3);
157
+ default:
158
+ return this.clone();
159
+ }
160
+ }, m2.endOf = function(t2) {
161
+ return this.startOf(t2, false);
162
+ }, m2.$set = function(t2, e2) {
163
+ var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
164
+ if (o2 === c || o2 === h) {
165
+ var y2 = this.clone().set(d, 1);
166
+ y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
167
+ } else
168
+ l2 && this.$d[l2]($2);
169
+ return this.init(), this;
170
+ }, m2.set = function(t2, e2) {
171
+ return this.clone().$set(t2, e2);
172
+ }, m2.get = function(t2) {
173
+ return this[b.p(t2)]();
174
+ }, m2.add = function(r2, f2) {
175
+ var d2, l2 = this;
176
+ r2 = Number(r2);
177
+ var $2 = b.p(f2), y2 = function(t2) {
178
+ var e2 = O(l2);
179
+ return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
180
+ };
181
+ if ($2 === c)
182
+ return this.set(c, this.$M + r2);
183
+ if ($2 === h)
184
+ return this.set(h, this.$y + r2);
185
+ if ($2 === a)
186
+ return y2(1);
187
+ if ($2 === o)
188
+ return y2(7);
189
+ var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
190
+ return b.w(m3, this);
191
+ }, m2.subtract = function(t2, e2) {
192
+ return this.add(-1 * t2, e2);
193
+ }, m2.format = function(t2) {
194
+ var e2 = this, n2 = this.$locale();
195
+ if (!this.isValid())
196
+ return n2.invalidDate || l;
197
+ var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
198
+ return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
199
+ }, d2 = function(t3) {
200
+ return b.s(s2 % 12 || 12, t3, "0");
201
+ }, $2 = f2 || function(t3, e3, n3) {
202
+ var r3 = t3 < 12 ? "AM" : "PM";
203
+ return n3 ? r3.toLowerCase() : r3;
204
+ };
205
+ return r2.replace(y, function(t3, r3) {
206
+ return r3 || function(t4) {
207
+ switch (t4) {
208
+ case "YY":
209
+ return String(e2.$y).slice(-2);
210
+ case "YYYY":
211
+ return b.s(e2.$y, 4, "0");
212
+ case "M":
213
+ return a2 + 1;
214
+ case "MM":
215
+ return b.s(a2 + 1, 2, "0");
216
+ case "MMM":
217
+ return h2(n2.monthsShort, a2, c2, 3);
218
+ case "MMMM":
219
+ return h2(c2, a2);
220
+ case "D":
221
+ return e2.$D;
222
+ case "DD":
223
+ return b.s(e2.$D, 2, "0");
224
+ case "d":
225
+ return String(e2.$W);
226
+ case "dd":
227
+ return h2(n2.weekdaysMin, e2.$W, o2, 2);
228
+ case "ddd":
229
+ return h2(n2.weekdaysShort, e2.$W, o2, 3);
230
+ case "dddd":
231
+ return o2[e2.$W];
232
+ case "H":
233
+ return String(s2);
234
+ case "HH":
235
+ return b.s(s2, 2, "0");
236
+ case "h":
237
+ return d2(1);
238
+ case "hh":
239
+ return d2(2);
240
+ case "a":
241
+ return $2(s2, u2, true);
242
+ case "A":
243
+ return $2(s2, u2, false);
244
+ case "m":
245
+ return String(u2);
246
+ case "mm":
247
+ return b.s(u2, 2, "0");
248
+ case "s":
249
+ return String(e2.$s);
250
+ case "ss":
251
+ return b.s(e2.$s, 2, "0");
252
+ case "SSS":
253
+ return b.s(e2.$ms, 3, "0");
254
+ case "Z":
255
+ return i2;
256
+ }
257
+ return null;
258
+ }(t3) || i2.replace(":", "");
259
+ });
260
+ }, m2.utcOffset = function() {
261
+ return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
262
+ }, m2.diff = function(r2, d2, l2) {
263
+ var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
264
+ return b.m(y2, m3);
265
+ };
266
+ switch (M3) {
267
+ case h:
268
+ $2 = D2() / 12;
269
+ break;
270
+ case c:
271
+ $2 = D2();
272
+ break;
273
+ case f:
274
+ $2 = D2() / 3;
275
+ break;
276
+ case o:
277
+ $2 = (g2 - v2) / 6048e5;
278
+ break;
279
+ case a:
280
+ $2 = (g2 - v2) / 864e5;
281
+ break;
282
+ case u:
283
+ $2 = g2 / n;
284
+ break;
285
+ case s:
286
+ $2 = g2 / e;
287
+ break;
288
+ case i:
289
+ $2 = g2 / t;
290
+ break;
291
+ default:
292
+ $2 = g2;
293
+ }
294
+ return l2 ? $2 : b.a($2);
295
+ }, m2.daysInMonth = function() {
296
+ return this.endOf(c).$D;
297
+ }, m2.$locale = function() {
298
+ return D[this.$L];
299
+ }, m2.locale = function(t2, e2) {
300
+ if (!t2)
301
+ return this.$L;
302
+ var n2 = this.clone(), r2 = w(t2, e2, true);
303
+ return r2 && (n2.$L = r2), n2;
304
+ }, m2.clone = function() {
305
+ return b.w(this.$d, this);
306
+ }, m2.toDate = function() {
307
+ return new Date(this.valueOf());
308
+ }, m2.toJSON = function() {
309
+ return this.isValid() ? this.toISOString() : null;
310
+ }, m2.toISOString = function() {
311
+ return this.$d.toISOString();
312
+ }, m2.toString = function() {
313
+ return this.$d.toUTCString();
314
+ }, M2;
315
+ }(), k = _.prototype;
316
+ return O.prototype = k, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", c], ["$y", h], ["$D", d]].forEach(function(t2) {
317
+ k[t2[1]] = function(e2) {
318
+ return this.$g(e2, t2[0], t2[1]);
319
+ };
320
+ }), O.extend = function(t2, e2) {
321
+ return t2.$i || (t2(e2, _, O), t2.$i = true), O;
322
+ }, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
323
+ return O(1e3 * t2);
324
+ }, O.en = D[g], O.Ls = D, O.p = {}, O;
325
+ });
326
+ }
327
+ });
328
+
329
+ // src/debug/index.ts
330
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
331
+ import path2 from "path";
332
+
333
+ // src/img/img.ts
334
+ import assert from "assert";
335
+ import { Buffer as Buffer2 } from "buffer";
336
+ import sharp from "sharp";
337
+ var createSvgOverlay = (elements, imageWidth, imageHeight) => {
338
+ let svgContent = `<svg width="${imageWidth}" height="${imageHeight}" xmlns="http://www.w3.org/2000/svg">`;
339
+ const colors = [
340
+ { rect: "blue", text: "white" },
341
+ { rect: "green", text: "white" }
342
+ ];
343
+ svgContent += "<defs>";
344
+ elements.forEach((element, index) => {
345
+ svgContent += `
346
+ <clipPath id="clip${index}">
347
+ <rect x="${element.x}" y="${element.y}" width="${element.width}" height="${element.height}" />
348
+ </clipPath>
349
+ `;
350
+ });
351
+ svgContent += "</defs>";
352
+ elements.forEach((element, index) => {
353
+ const textWidth = element.label.length * 8;
354
+ const textHeight = 12;
355
+ const rectWidth = textWidth + 5;
356
+ const rectHeight = textHeight + 4;
357
+ let rectX = element.x - rectWidth;
358
+ let rectY = element.y + element.height / 2 - textHeight / 2 - 2;
359
+ let textX = rectX + rectWidth / 2;
360
+ let textY = rectY + rectHeight / 2 + 6;
361
+ if (rectX < 0) {
362
+ rectX = element.x;
363
+ rectY = element.y - rectHeight;
364
+ textX = rectX + rectWidth / 2;
365
+ textY = rectY + rectHeight / 2 + 6;
366
+ }
367
+ const color = colors[index % colors.length];
368
+ svgContent += `
369
+ <rect x="${element.x}" y="${element.y}" width="${element.width}" height="${element.height}"
370
+ style="fill:none;stroke:${color.rect};stroke-width:4" clip-path="url(#clip${index})" />
371
+ <rect x="${rectX}" y="${rectY}" width="${rectWidth}" height="${rectHeight}" style="fill:${color.rect};" />
372
+ <text x="${textX}" y="${textY}"
373
+ text-anchor="middle" dominant-baseline="middle" style="fill:${color.text};font-size:12px;font-weight:bold;">
374
+ ${element.label}
375
+ </text>
376
+ `;
377
+ });
378
+ svgContent += "</svg>";
379
+ return Buffer2.from(svgContent);
380
+ };
381
+ var processImageElementInfo = async (options) => {
382
+ const base64Image = options.inputImgBase64.split(";base64,").pop();
383
+ assert(base64Image, "base64Image is undefined");
384
+ const imageBuffer = Buffer2.from(base64Image, "base64");
385
+ const metadata = await sharp(imageBuffer).metadata();
386
+ const { width, height } = metadata;
387
+ if (width && height) {
388
+ const svgOverlay = createSvgOverlay(
389
+ options.elementsPositionInfo,
390
+ width,
391
+ height
392
+ );
393
+ const svgOverlayWithoutText = createSvgOverlay(
394
+ options.elementsPositionInfoWithoutText,
395
+ width,
396
+ height
397
+ );
398
+ const compositeElementInfoImgBase64 = await sharp(imageBuffer).composite([{ input: svgOverlay, blend: "over" }]).toBuffer().then((data) => {
399
+ return data.toString("base64");
400
+ }).catch((err) => {
401
+ throw err;
402
+ });
403
+ const compositeElementInfoImgWithoutTextBase64 = await sharp(imageBuffer).composite([{ input: svgOverlayWithoutText, blend: "over" }]).toBuffer().then((data) => {
404
+ return data.toString("base64");
405
+ }).catch((err) => {
406
+ throw err;
407
+ });
408
+ return {
409
+ compositeElementInfoImgBase64,
410
+ compositeElementInfoImgWithoutTextBase64
411
+ };
412
+ }
413
+ throw Error("Image processing failed because width or height is undefined");
414
+ };
415
+
416
+ // src/common/utils.ts
417
+ var import_dayjs = __toESM(require_dayjs_min());
418
+ import assert2 from "assert";
419
+ import fs, { readFileSync } from "fs";
420
+ import path from "path";
421
+ import {
422
+ base64Encoded,
423
+ imageInfoOfBase64
424
+ } from "@midscene/core/image";
425
+ import { getTmpFile } from "@midscene/core/utils";
426
+ async function getElementInfosFromPage(page) {
427
+ const pathDir = findNearestPackageJson(__dirname);
428
+ assert2(pathDir, `can't find pathDir, with ${__dirname}`);
429
+ const scriptPath = path.join(pathDir, "./dist/script/htmlElement.js");
430
+ const elementInfosScriptContent = readFileSync(scriptPath, "utf-8");
431
+ const extraReturnLogic = `${elementInfosScriptContent}midscene_element_inspector.extractTextWithPosition()`;
432
+ const captureElementSnapshot = await page.evaluate(extraReturnLogic);
433
+ return captureElementSnapshot;
434
+ }
435
+ function findNearestPackageJson(dir) {
436
+ const packageJsonPath = path.join(dir, "package.json");
437
+ if (fs.existsSync(packageJsonPath)) {
438
+ return dir;
439
+ }
440
+ const parentDir = path.dirname(dir);
441
+ if (parentDir === dir) {
442
+ return null;
443
+ }
444
+ return findNearestPackageJson(parentDir);
445
+ }
446
+
447
+ // src/img/util.ts
448
+ async function getElementInfos(page) {
449
+ const captureElementSnapshot = await getElementInfosFromPage(page);
450
+ const elementsPositionInfo = captureElementSnapshot.map((elementInfo) => {
451
+ return {
452
+ label: elementInfo.indexId.toString(),
453
+ x: elementInfo.rect.left,
454
+ y: elementInfo.rect.top,
455
+ width: elementInfo.rect.width,
456
+ height: elementInfo.rect.height,
457
+ attributes: elementInfo.attributes
458
+ };
459
+ });
460
+ const elementsPositionInfoWithoutText = elementsPositionInfo.filter(
461
+ (elementInfo) => {
462
+ if (elementInfo.attributes.nodeType === "TEXT Node" /* TEXT */) {
463
+ return false;
464
+ }
465
+ return true;
466
+ }
467
+ );
468
+ return {
469
+ elementsPositionInfo,
470
+ captureElementSnapshot,
471
+ elementsPositionInfoWithoutText
472
+ };
473
+ }
474
+
475
+ // src/debug/index.ts
476
+ import { resizeImg, saveBase64Image } from "@midscene/core/image";
477
+ async function generateExtractData(page, targetDir, saveImgType) {
478
+ const buffer = await page.screenshot({
479
+ encoding: "base64"
480
+ });
481
+ const inputImgBase64 = buffer.toString("base64");
482
+ const {
483
+ elementsPositionInfo,
484
+ captureElementSnapshot,
485
+ elementsPositionInfoWithoutText
486
+ } = await getElementInfos(page);
487
+ const inputImagePath = path2.join(targetDir, "input.png");
488
+ const outputImagePath = path2.join(targetDir, "output.png");
489
+ const outputWithoutTextImgPath = path2.join(
490
+ targetDir,
491
+ "output_without_text.png"
492
+ );
493
+ const resizeOutputImgPath = path2.join(targetDir, "resize-output.png");
494
+ const snapshotJsonPath = path2.join(targetDir, "element-snapshot.json");
495
+ const {
496
+ compositeElementInfoImgBase64,
497
+ compositeElementInfoImgWithoutTextBase64
498
+ } = await processImageElementInfo({
499
+ elementsPositionInfo,
500
+ elementsPositionInfoWithoutText,
501
+ inputImgBase64
502
+ });
503
+ const resizeImgBase64 = await resizeImg(inputImgBase64);
504
+ if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
505
+ writeFileSyncWithDir(
506
+ snapshotJsonPath,
507
+ JSON.stringify(captureElementSnapshot, null, 2)
508
+ );
509
+ }
510
+ if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
511
+ await saveBase64Image({
512
+ base64Data: inputImgBase64,
513
+ outputPath: inputImagePath
514
+ });
515
+ }
516
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
517
+ await saveBase64Image({
518
+ base64Data: compositeElementInfoImgBase64,
519
+ outputPath: outputImagePath
520
+ });
521
+ }
522
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
523
+ await saveBase64Image({
524
+ base64Data: compositeElementInfoImgWithoutTextBase64,
525
+ outputPath: outputWithoutTextImgPath
526
+ });
527
+ }
528
+ if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
529
+ await saveBase64Image({
530
+ base64Data: resizeImgBase64,
531
+ outputPath: resizeOutputImgPath
532
+ });
533
+ }
534
+ }
535
+ function generateTestDataPath(testDataName) {
536
+ const modulePath = __require.resolve("@midscene/core").replace("dist/lib/index.js", "");
537
+ const midsceneTestDataPath = path2.join(
538
+ modulePath,
539
+ `tests/ai/inspector/test-data/${testDataName}`
540
+ );
541
+ return midsceneTestDataPath;
542
+ }
543
+ function ensureDirectoryExistence(filePath) {
544
+ const dirname = path2.dirname(filePath);
545
+ if (existsSync(dirname)) {
546
+ return;
547
+ }
548
+ ensureDirectoryExistence(dirname);
549
+ mkdirSync(dirname);
550
+ }
551
+ function writeFileSyncWithDir(filePath, content, options = {}) {
552
+ ensureDirectoryExistence(filePath);
553
+ writeFileSync(filePath, content, options);
554
+ }
555
+ export {
556
+ generateExtractData,
557
+ generateTestDataPath,
558
+ writeFileSyncWithDir
559
+ };
package/dist/es/index.js CHANGED
@@ -336,7 +336,7 @@ var require_dayjs_min = __commonJS({
336
336
  }
337
337
  });
338
338
 
339
- // src/playwright/index.ts
339
+ // src/playwright/ai-fixture.ts
340
340
  import { randomUUID } from "crypto";
341
341
 
342
342
  // src/common/agent.ts
@@ -1048,7 +1048,7 @@ ${errorTask == null ? void 0 : errorTask.errorStack}`);
1048
1048
  }
1049
1049
  };
1050
1050
 
1051
- // src/playwright/index.ts
1051
+ // src/playwright/ai-fixture.ts
1052
1052
  import { test } from "@playwright/test";
1053
1053
 
1054
1054
  // src/playwright/cache.ts
@@ -1114,7 +1114,7 @@ function getPkgInfo() {
1114
1114
  };
1115
1115
  }
1116
1116
 
1117
- // src/playwright/index.ts
1117
+ // src/playwright/ai-fixture.ts
1118
1118
  var groupAndCaseForTest = (testInfo) => {
1119
1119
  let taskFile;
1120
1120
  let taskTitle;