@midscene/shared 0.17.4-beta-20250528162713.0 → 0.17.4

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.
@@ -261,9 +261,15 @@ export function elementRect(
261
261
  currentWindow: typeof globalThis.window,
262
262
  currentDocument: typeof globalThis.document,
263
263
  baseZoom = 1,
264
- visibleOnly = true,
265
264
  ):
266
- | { left: number; top: number; width: number; height: number; zoom: number }
265
+ | {
266
+ left: number;
267
+ top: number;
268
+ width: number;
269
+ height: number;
270
+ zoom: number;
271
+ isVisible: boolean;
272
+ }
267
273
  | false {
268
274
  if (!el) {
269
275
  logger(el, 'Element is not in the DOM hierarchy');
@@ -304,29 +310,11 @@ export function elementRect(
304
310
  return false;
305
311
  }
306
312
 
307
- const scrollLeft =
308
- currentWindow.pageXOffset || currentDocument.documentElement.scrollLeft;
309
- const scrollTop =
310
- currentWindow.pageYOffset || currentDocument.documentElement.scrollTop;
311
- const viewportWidth =
312
- currentWindow.innerWidth || currentDocument.documentElement.clientWidth;
313
- const viewportHeight =
314
- currentWindow.innerHeight || currentDocument.documentElement.clientHeight;
315
-
316
- const isPartiallyInViewport = visibleOnly
317
- ? isElementPartiallyInViewport(rect, currentWindow, currentDocument)
318
- : true;
319
-
320
- if (!isPartiallyInViewport) {
321
- logger(el, 'Element is completely outside the viewport', {
322
- rect,
323
- viewportHeight,
324
- viewportWidth,
325
- scrollTop,
326
- scrollLeft,
327
- });
328
- return false;
329
- }
313
+ const isVisible = isElementPartiallyInViewport(
314
+ rect,
315
+ currentWindow,
316
+ currentDocument,
317
+ );
330
318
 
331
319
  // check if the element is hidden by an ancestor
332
320
  let parent: HTMLElement | Node | null = el;
@@ -384,6 +372,7 @@ export function elementRect(
384
372
  width: Math.round(rect.width),
385
373
  height: Math.round(rect.height),
386
374
  zoom: rect.zoom,
375
+ isVisible,
387
376
  };
388
377
  }
389
378
 
@@ -32,11 +32,11 @@ function tagNameOfNode(node: globalThis.Node): string {
32
32
  let tagName = '';
33
33
  if (node instanceof HTMLElement) {
34
34
  tagName = node.tagName.toLowerCase();
35
- }
36
-
37
- const parentElement = node.parentElement;
38
- if (parentElement && parentElement instanceof HTMLElement) {
39
- tagName = parentElement.tagName.toLowerCase();
35
+ } else {
36
+ const parentElement = node.parentElement;
37
+ if (parentElement && parentElement instanceof HTMLElement) {
38
+ tagName = parentElement.tagName.toLowerCase();
39
+ }
40
40
  }
41
41
 
42
42
  return tagName ? `<${tagName}>` : '';
@@ -48,23 +48,16 @@ export function collectElementInfo(
48
48
  currentDocument: typeof document,
49
49
  baseZoom = 1,
50
50
  basePoint: Point = { left: 0, top: 0 },
51
- visibleOnly = true,
52
51
  ): WebElementInfo | null | any {
53
- const rect = elementRect(
54
- node,
55
- currentWindow,
56
- currentDocument,
57
- baseZoom,
58
- visibleOnly,
59
- );
52
+ const rect = elementRect(node, currentWindow, currentDocument, baseZoom);
60
53
 
61
54
  if (!rect) {
62
55
  return null;
63
56
  }
64
57
 
65
58
  if (
66
- visibleOnly &&
67
- (rect.width < CONTAINER_MINI_WIDTH || rect.height < CONTAINER_MINI_HEIGHT)
59
+ rect.width < CONTAINER_MINI_WIDTH ||
60
+ rect.height < CONTAINER_MINI_HEIGHT
68
61
  ) {
69
62
  return null;
70
63
  }
@@ -122,6 +115,7 @@ export function collectElementInfo(
122
115
  Math.round(rect.top + rect.height / 2),
123
116
  ],
124
117
  zoom: rect.zoom,
118
+ isVisible: rect.isVisible,
125
119
  };
126
120
  return elementInfo;
127
121
  }
@@ -132,7 +126,6 @@ export function collectElementInfo(
132
126
  currentWindow,
133
127
  currentDocument,
134
128
  baseZoom,
135
- visibleOnly,
136
129
  );
137
130
  if (!rect) {
138
131
  return null;
@@ -160,6 +153,7 @@ export function collectElementInfo(
160
153
  Math.round(rect.top + rect.height / 2),
161
154
  ],
162
155
  zoom: rect.zoom,
156
+ isVisible: rect.isVisible,
163
157
  };
164
158
  return elementInfo;
165
159
  }
@@ -191,6 +185,7 @@ export function collectElementInfo(
191
185
  Math.round(rect.top + rect.height / 2),
192
186
  ],
193
187
  zoom: rect.zoom,
188
+ isVisible: rect.isVisible,
194
189
  };
195
190
  return elementInfo;
196
191
  }
@@ -225,6 +220,7 @@ export function collectElementInfo(
225
220
  content: text,
226
221
  rect,
227
222
  zoom: rect.zoom,
223
+ isVisible: rect.isVisible,
228
224
  };
229
225
  return elementInfo;
230
226
  }
@@ -253,6 +249,7 @@ export function collectElementInfo(
253
249
  Math.round(rect.top + rect.height / 2),
254
250
  ],
255
251
  zoom: rect.zoom,
252
+ isVisible: rect.isVisible,
256
253
  };
257
254
  return elementInfo;
258
255
  }
@@ -280,6 +277,7 @@ export function collectElementInfo(
280
277
  Math.round(rect.top + rect.height / 2),
281
278
  ],
282
279
  zoom: rect.zoom,
280
+ isVisible: rect.isVisible,
283
281
  };
284
282
  return elementInfo;
285
283
  }
@@ -314,10 +312,11 @@ export function extractTextWithPosition(
314
312
 
315
313
  export function extractTreeNodeAsString(
316
314
  initNode: globalThis.Node,
315
+ visibleOnly = false,
317
316
  debugMode = false,
318
317
  ): string {
319
318
  const elementNode = extractTreeNode(initNode, debugMode);
320
- return descriptionOfTree(elementNode);
319
+ return descriptionOfTree(elementNode, undefined, false, visibleOnly);
321
320
  }
322
321
 
323
322
  export function extractTreeNode(
@@ -368,14 +367,13 @@ export function extractTreeNode(
368
367
  node: elementInfo,
369
368
  children: [],
370
369
  };
371
- // stop collecting if the node is a Button/Image/Text/A/FormItem/Container
370
+ // stop collecting if the node is a Button/Image/Text/FormItem/Container
372
371
  if (
373
372
  elementInfo?.nodeType === NodeType.BUTTON ||
374
373
  elementInfo?.nodeType === NodeType.IMG ||
375
374
  elementInfo?.nodeType === NodeType.TEXT ||
376
- elementInfo?.nodeType === NodeType.A ||
377
375
  elementInfo?.nodeType === NodeType.FORM_ITEM ||
378
- elementInfo?.nodeType === NodeType.CONTAINER
376
+ elementInfo?.nodeType === NodeType.CONTAINER // TODO: need return the container node?
379
377
  ) {
380
378
  return nodeInfo;
381
379
  }
@@ -443,15 +441,8 @@ export function mergeElementAndChildrenRects(
443
441
  currentWindow: typeof window,
444
442
  currentDocument: typeof document,
445
443
  baseZoom = 1,
446
- visibleOnly = true,
447
444
  ) {
448
- const selfRect = elementRect(
449
- node,
450
- currentWindow,
451
- currentDocument,
452
- baseZoom,
453
- visibleOnly,
454
- );
445
+ const selfRect = elementRect(node, currentWindow, currentDocument, baseZoom);
455
446
  if (!selfRect) return null;
456
447
 
457
448
  let minLeft = selfRect.left;
@@ -463,13 +454,7 @@ export function mergeElementAndChildrenRects(
463
454
  for (let i = 0; i < child.childNodes.length; i++) {
464
455
  const sub = child.childNodes[i];
465
456
  if (sub.nodeType === 1) {
466
- const rect = elementRect(
467
- sub,
468
- currentWindow,
469
- currentDocument,
470
- baseZoom,
471
- visibleOnly,
472
- );
457
+ const rect = elementRect(sub, currentWindow, currentDocument, baseZoom);
473
458
  if (rect) {
474
459
  minLeft = Math.min(minLeft, rect.left);
475
460
  minTop = Math.min(minTop, rect.top);
@@ -31,6 +31,10 @@ export abstract class BaseElement {
31
31
  abstract center: [number, number];
32
32
 
33
33
  abstract locator?: string;
34
+
35
+ abstract xpaths?: string[];
36
+
37
+ abstract isVisible: boolean;
34
38
  }
35
39
 
36
40
  export interface ElementTreeNode<