android-midscene-automation 0.1.6 → 0.1.9

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.
@@ -1,5 +1,11 @@
1
1
  import type { AppiumBounds, AppiumNode, AppiumSelector } from './types';
2
2
 
3
+ type SelectorCounts = {
4
+ contentDesc: Map<string, number>;
5
+ resourceId: Map<string, number>;
6
+ textClass: Map<string, number>;
7
+ };
8
+
3
9
  function attr(element: Element, name: string) {
4
10
  return element.getAttribute(name) || '';
5
11
  }
@@ -25,19 +31,35 @@ function escapeUiAutomatorText(value: string) {
25
31
  return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
26
32
  }
27
33
 
28
- function buildSelector(element: Element, bounds?: AppiumBounds): AppiumSelector {
34
+ function textClassKey(element: Element) {
35
+ return `${attr(element, 'text')}|${attr(element, 'class')}`;
36
+ }
37
+
38
+ function withCount(selector: AppiumSelector, count: number): AppiumSelector {
39
+ return {
40
+ ...selector,
41
+ unique: count === 1,
42
+ matchCount: count,
43
+ source: selector.source || 'node',
44
+ };
45
+ }
46
+
47
+ function buildSelector(element: Element, counts: SelectorCounts, bounds?: AppiumBounds): AppiumSelector {
29
48
  const contentDesc = attr(element, 'content-desc');
30
49
  const resourceId = attr(element, 'resource-id');
31
50
  const text = attr(element, 'text');
32
51
  const className = attr(element, 'class');
33
- if (contentDesc) return { strategy: 'accessibilityId', value: contentDesc };
34
- if (resourceId) return { strategy: 'id', value: resourceId };
52
+ if (contentDesc) return withCount({ strategy: 'accessibilityId', value: contentDesc }, counts.contentDesc.get(contentDesc) || 1);
53
+ if (resourceId) return withCount({ strategy: 'id', value: resourceId }, counts.resourceId.get(resourceId) || 1);
35
54
  if (text) {
36
55
  const classClause = className ? `.className("${escapeUiAutomatorText(className)}")` : '';
37
- return { strategy: 'androidUiAutomator', value: `new UiSelector().text("${escapeUiAutomatorText(text)}")${classClause}` };
56
+ return withCount(
57
+ { strategy: 'androidUiAutomator', value: `new UiSelector().text("${escapeUiAutomatorText(text)}")${classClause}` },
58
+ counts.textClass.get(textClassKey(element)) || 1,
59
+ );
38
60
  }
39
- if (bounds) return { strategy: 'bounds', centerX: bounds.centerX, centerY: bounds.centerY };
40
- return { strategy: 'xpath', value: '' };
61
+ if (bounds) return { strategy: 'bounds', centerX: bounds.centerX, centerY: bounds.centerY, unique: false, matchCount: 0, source: 'fallback' };
62
+ return { strategy: 'xpath', value: '', unique: false, matchCount: 0 };
41
63
  }
42
64
 
43
65
  function nodeLabel(element: Element) {
@@ -61,6 +83,8 @@ function transformNode(
61
83
  parentPath: string,
62
84
  index: number,
63
85
  resourceIdCounts: Map<string, number>,
86
+ counts: SelectorCounts,
87
+ nearestContextSelector?: AppiumSelector,
64
88
  ): AppiumNode {
65
89
  const bounds = parseBounds(attr(element, 'bounds'));
66
90
  const className = attr(element, 'class') || element.tagName;
@@ -72,8 +96,13 @@ function transformNode(
72
96
  const xpath = resourceId && resourceIdCounts.get(resourceId) === 1
73
97
  ? `//*[@resource-id='${escapeXpathValue(resourceId)}']`
74
98
  : `${parentPath}/${className}[${sameClassIndex}]`;
99
+ const selector = buildSelector(element, counts, bounds);
100
+ const selfContextSelector: AppiumSelector = selector.unique && selector.strategy !== 'bounds'
101
+ ? selector
102
+ : { strategy: 'xpath', value: xpath, unique: true, matchCount: 1, source: 'ancestor' };
103
+ const contextSelector = selector.unique ? undefined : nearestContextSelector;
75
104
  const children = elementChildren(element).map((child, childIndex) => (
76
- transformNode(child, xpath, childIndex, resourceIdCounts)
105
+ transformNode(child, xpath, childIndex, resourceIdCounts, counts, selfContextSelector)
77
106
  ));
78
107
  return {
79
108
  id: `${xpath}-${attr(element, 'bounds') || index}`,
@@ -87,7 +116,8 @@ function transformNode(
87
116
  enabled: attr(element, 'enabled') !== 'false',
88
117
  xpath,
89
118
  bounds,
90
- selector: buildSelector(element, bounds),
119
+ selector,
120
+ contextSelector,
91
121
  children,
92
122
  };
93
123
  }
@@ -97,11 +127,27 @@ export function parseWindowHierarchy(xml: string) {
97
127
  const root = doc.querySelector('hierarchy > node') || doc.querySelector('node');
98
128
  if (!root) return null;
99
129
  const resourceIdCounts = new Map<string, number>();
130
+ const counts: SelectorCounts = {
131
+ contentDesc: new Map(),
132
+ resourceId: resourceIdCounts,
133
+ textClass: new Map(),
134
+ };
100
135
  doc.querySelectorAll('node[resource-id]').forEach((element) => {
101
136
  const resourceId = attr(element, 'resource-id');
102
137
  if (resourceId) resourceIdCounts.set(resourceId, (resourceIdCounts.get(resourceId) || 0) + 1);
103
138
  });
104
- return transformNode(root, '', 0, resourceIdCounts);
139
+ doc.querySelectorAll('node[content-desc]').forEach((element) => {
140
+ const contentDesc = attr(element, 'content-desc');
141
+ if (contentDesc) counts.contentDesc.set(contentDesc, (counts.contentDesc.get(contentDesc) || 0) + 1);
142
+ });
143
+ doc.querySelectorAll('node[text]').forEach((element) => {
144
+ const text = attr(element, 'text');
145
+ if (text) {
146
+ const key = textClassKey(element);
147
+ counts.textClass.set(key, (counts.textClass.get(key) || 0) + 1);
148
+ }
149
+ });
150
+ return transformNode(root, '', 0, resourceIdCounts, counts);
105
151
  }
106
152
 
107
153
  export function flattenNodes(node: AppiumNode | null): AppiumNode[] {
@@ -12,6 +12,16 @@ export type AppiumSelector = {
12
12
  value?: string;
13
13
  centerX?: number;
14
14
  centerY?: number;
15
+ unique?: boolean;
16
+ matchCount?: number;
17
+ source?: 'node' | 'ancestor' | 'fallback';
18
+ };
19
+
20
+ export type AppiumPageSnapshot = {
21
+ activity?: string;
22
+ packageName?: string;
23
+ treeSignature?: string;
24
+ selectedNodePath?: string;
15
25
  };
16
26
 
17
27
  export type AppiumNode = {
@@ -27,6 +37,7 @@ export type AppiumNode = {
27
37
  xpath: string;
28
38
  bounds?: AppiumBounds;
29
39
  selector: AppiumSelector;
40
+ contextSelector?: AppiumSelector;
30
41
  children: AppiumNode[];
31
42
  };
32
43
 
@@ -35,6 +46,10 @@ export type AppiumRecordedStep = {
35
46
  type:
36
47
  | 'tap'
37
48
  | 'input'
49
+ | 'tapIfExists'
50
+ | 'inputIfExists'
51
+ | 'clearIfExists'
52
+ | 'backIfExists'
38
53
  | 'waitFor'
39
54
  | 'assertExists'
40
55
  | 'key'
@@ -48,13 +63,30 @@ export type AppiumRecordedStep = {
48
63
  | 'waitDisappear'
49
64
  | 'assertText'
50
65
  | 'longPress'
51
- | 'pinch';
66
+ | 'pinch'
67
+ | 'runScript';
52
68
  label: string;
53
69
  selector?: AppiumSelector;
70
+ contextSelector?: AppiumSelector;
71
+ selectorChain?: AppiumSelector[];
54
72
  fallback?: AppiumSelector;
55
73
  value?: string;
74
+ optional?: boolean;
56
75
  keyCode?: number;
57
76
  timeoutMs?: number;
77
+ flow?: {
78
+ scope?: 'pre' | 'main' | 'post';
79
+ nodeKind?: 'action' | 'condition' | 'assertion';
80
+ yesTargetId?: string;
81
+ noTargetId?: string;
82
+ parentConditionId?: string;
83
+ parentBranch?: 'yes' | 'no';
84
+ successTargetId?: string;
85
+ failureTargetId?: string;
86
+ collapsed?: boolean;
87
+ };
88
+ pageBefore?: AppiumPageSnapshot;
89
+ pageAfter?: AppiumPageSnapshot;
58
90
  swipe?: {
59
91
  startX: number;
60
92
  startY: number;