appium-mcp 1.98.0 → 1.99.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/CHANGELOG.md +6 -0
- package/README.md +30 -0
- package/dist/locators/element-filter.d.ts +3 -1
- package/dist/locators/element-filter.d.ts.map +1 -1
- package/dist/locators/element-filter.js +44 -14
- package/dist/locators/element-filter.js.map +1 -1
- package/dist/locators/generate-all-locators.d.ts +6 -1
- package/dist/locators/generate-all-locators.d.ts.map +1 -1
- package/dist/locators/generate-all-locators.js +1 -1
- package/dist/locators/generate-all-locators.js.map +1 -1
- package/dist/locators/locator-generation.d.ts +4 -3
- package/dist/locators/locator-generation.d.ts.map +1 -1
- package/dist/locators/locator-generation.js +31 -10
- package/dist/locators/locator-generation.js.map +1 -1
- package/dist/locators/types.d.ts +3 -0
- package/dist/locators/types.d.ts.map +1 -0
- package/dist/locators/types.js +2 -0
- package/dist/locators/types.js.map +1 -0
- package/dist/tools/gestures/schema.d.ts.map +1 -1
- package/dist/tools/gestures/schema.js +7 -15
- package/dist/tools/gestures/schema.js.map +1 -1
- package/dist/tools/test-generation/locators.d.ts.map +1 -1
- package/dist/tools/test-generation/locators.js +19 -5
- package/dist/tools/test-generation/locators.js.map +1 -1
- package/dist/ui/inspector-screenshot-script.d.ts +3 -0
- package/dist/ui/inspector-screenshot-script.d.ts.map +1 -0
- package/dist/ui/inspector-screenshot-script.js +164 -0
- package/dist/ui/inspector-screenshot-script.js.map +1 -0
- package/dist/ui/mcp-ui-utils.d.ts.map +1 -1
- package/dist/ui/mcp-ui-utils.js +14 -73
- package/dist/ui/mcp-ui-utils.js.map +1 -1
- package/dist/ui/page-source-inspector-app.d.ts.map +1 -1
- package/dist/ui/page-source-inspector-app.js +79 -75
- package/dist/ui/page-source-inspector-app.js.map +1 -1
- package/dist/ui/xml-inspector-scripts.d.ts +4 -0
- package/dist/ui/xml-inspector-scripts.d.ts.map +1 -0
- package/dist/ui/xml-inspector-scripts.js +91 -0
- package/dist/ui/xml-inspector-scripts.js.map +1 -0
- package/package.json +1 -1
- package/server.json +2 -2
- package/src/locators/element-filter.ts +50 -16
- package/src/locators/generate-all-locators.ts +13 -7
- package/src/locators/locator-generation.ts +43 -10
- package/src/locators/types.ts +2 -0
- package/src/tools/gestures/schema.ts +7 -17
- package/src/tools/test-generation/locators.ts +31 -7
- package/src/ui/inspector-screenshot-script.ts +163 -0
- package/src/ui/mcp-ui-utils.ts +15 -73
- package/src/ui/page-source-inspector-app.ts +80 -75
- package/src/ui/xml-inspector-scripts.ts +91 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** Shared browser helpers embedded by both inspector transports. Keep these scripts data-free. */
|
|
2
|
+
export const XML_FORMAT_SCRIPT = String.raw `
|
|
3
|
+
function formatXml(source) {
|
|
4
|
+
const documentNode = new DOMParser().parseFromString(source, 'text/xml');
|
|
5
|
+
if (!documentNode.documentElement || documentNode.getElementsByTagName('parsererror').length) {
|
|
6
|
+
throw new Error('Invalid XML');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function indentElement(element, depth) {
|
|
10
|
+
// Whitespace can be meaningful in mixed content, CDATA and preserved subtrees.
|
|
11
|
+
if (element.getAttribute('xml:space') === 'preserve') return;
|
|
12
|
+
const children = Array.from(element.childNodes);
|
|
13
|
+
const hasText = children.some(child =>
|
|
14
|
+
child.nodeType === 4 || (child.nodeType === 3 && (child.nodeValue || '').trim())
|
|
15
|
+
);
|
|
16
|
+
if (hasText || !children.some(child => child.nodeType === 1)) return;
|
|
17
|
+
for (const child of children) {
|
|
18
|
+
if (child.nodeType === 3) {
|
|
19
|
+
element.removeChild(child);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (child.nodeType === 1) indentElement(child, depth + 1);
|
|
23
|
+
element.insertBefore(documentNode.createTextNode('\n' + ' '.repeat(depth + 1)), child);
|
|
24
|
+
}
|
|
25
|
+
element.appendChild(documentNode.createTextNode('\n' + ' '.repeat(depth)));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
indentElement(documentNode.documentElement, 0);
|
|
29
|
+
const serializer = new XMLSerializer();
|
|
30
|
+
let formatted = Array.from(documentNode.childNodes)
|
|
31
|
+
.filter(node => node.nodeType !== 3)
|
|
32
|
+
.map(node => serializer.serializeToString(node))
|
|
33
|
+
.join('\n');
|
|
34
|
+
const declaration = source.match(/^\s*(<\?xml\s[^?]*\?>)/);
|
|
35
|
+
if (declaration && !formatted.startsWith('<?xml ')) {
|
|
36
|
+
formatted = declaration[1] + '\n' + formatted;
|
|
37
|
+
}
|
|
38
|
+
return formatted;
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
export const XML_SEARCH_SCRIPT = String.raw `
|
|
42
|
+
function createXmlSearch(container, status) {
|
|
43
|
+
let matches = [];
|
|
44
|
+
let activeIndex = -1;
|
|
45
|
+
|
|
46
|
+
function render(text, query) {
|
|
47
|
+
container.textContent = '';
|
|
48
|
+
matches = [];
|
|
49
|
+
activeIndex = -1;
|
|
50
|
+
query = query.trim();
|
|
51
|
+
status.textContent = '';
|
|
52
|
+
if (!query) {
|
|
53
|
+
container.textContent = text;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const lowerText = text.toLowerCase();
|
|
57
|
+
const lowerQuery = query.toLowerCase();
|
|
58
|
+
let position = 0;
|
|
59
|
+
while (position < text.length) {
|
|
60
|
+
const index = lowerText.indexOf(lowerQuery, position);
|
|
61
|
+
if (index === -1) break;
|
|
62
|
+
container.appendChild(document.createTextNode(text.slice(position, index)));
|
|
63
|
+
const mark = document.createElement('mark');
|
|
64
|
+
mark.textContent = text.slice(index, index + query.length);
|
|
65
|
+
container.appendChild(mark);
|
|
66
|
+
matches.push(mark);
|
|
67
|
+
position = index + query.length;
|
|
68
|
+
}
|
|
69
|
+
container.appendChild(document.createTextNode(text.slice(position)));
|
|
70
|
+
status.textContent = matches.length ? matches.length + ' matches · Enter / Shift+Enter to navigate' : 'No matches';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function handleKeyDown(event) {
|
|
74
|
+
if (event.key !== 'Enter' || event.isComposing) return;
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
if (!matches.length) return;
|
|
77
|
+
if (activeIndex >= 0) matches[activeIndex].removeAttribute('data-active');
|
|
78
|
+
const step = event.shiftKey ? -1 : 1;
|
|
79
|
+
activeIndex = activeIndex < 0
|
|
80
|
+
? (event.shiftKey ? matches.length - 1 : 0)
|
|
81
|
+
: (activeIndex + step + matches.length) % matches.length;
|
|
82
|
+
const match = matches[activeIndex];
|
|
83
|
+
match.setAttribute('data-active', 'true');
|
|
84
|
+
match.scrollIntoView({block: 'nearest', inline: 'nearest'});
|
|
85
|
+
status.textContent = (activeIndex + 1) + ' / ' + matches.length + ' · Enter / Shift+Enter to navigate';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {render, handleKeyDown};
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
91
|
+
//# sourceMappingURL=xml-inspector-scripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml-inspector-scripts.js","sourceRoot":"","sources":["../../src/ui/xml-inspector-scripts.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsC1C,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiD1C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "appium-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.99.0",
|
|
4
4
|
"description": "Intelligent MCP server providing AI assistants with powerful tools and resources for Appium mobile automation",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/appium/appium-mcp/issues"
|
package/server.json
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
"name": "io.github.appium/appium-mcp",
|
|
4
4
|
"title": "MCP Appium - Mobile Development and Automation Server",
|
|
5
5
|
"description": "MCP server for Appium mobile automation on iOS and Android devices with test creation tools.",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.99.0",
|
|
7
7
|
"packages": [
|
|
8
8
|
{
|
|
9
9
|
"registryType": "npm",
|
|
10
10
|
"identifier": "appium-mcp",
|
|
11
|
-
"version": "1.
|
|
11
|
+
"version": "1.99.0",
|
|
12
12
|
"transport": {
|
|
13
13
|
"type": "stdio"
|
|
14
14
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type {JSONElement} from './source-parsing.js';
|
|
2
|
+
import type {DriverName} from './types.js';
|
|
2
3
|
|
|
3
4
|
export interface FilterOptions {
|
|
5
|
+
query?: string;
|
|
4
6
|
includeTagNames?: string[];
|
|
5
7
|
excludeTagNames?: string[];
|
|
6
8
|
requireAttributes?: string[];
|
|
@@ -16,9 +18,10 @@ export function shouldIncludeElement(
|
|
|
16
18
|
element: JSONElement,
|
|
17
19
|
filters: FilterOptions,
|
|
18
20
|
isNative: boolean,
|
|
19
|
-
automationName:
|
|
21
|
+
automationName: DriverName,
|
|
20
22
|
): boolean {
|
|
21
23
|
const {
|
|
24
|
+
query,
|
|
22
25
|
includeTagNames = [],
|
|
23
26
|
excludeTagNames = ['hierarchy'],
|
|
24
27
|
requireAttributes = [],
|
|
@@ -27,6 +30,10 @@ export function shouldIncludeElement(
|
|
|
27
30
|
clickableOnly = false,
|
|
28
31
|
} = filters;
|
|
29
32
|
|
|
33
|
+
if (!matchesSearchQuery(element, query)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
if (!matchesTagFilters(element, includeTagNames, excludeTagNames)) {
|
|
31
38
|
return false;
|
|
32
39
|
}
|
|
@@ -46,6 +53,15 @@ export function shouldIncludeElement(
|
|
|
46
53
|
return true;
|
|
47
54
|
}
|
|
48
55
|
|
|
56
|
+
/** Literal substring matching applies only to this element, never its descendants. */
|
|
57
|
+
function matchesSearchQuery(element: JSONElement, query?: string): boolean {
|
|
58
|
+
const search = query?.trim().toLowerCase();
|
|
59
|
+
if (!search) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return [element.tagName, ...Object.values(element.attributes)].some((value) => value.toLowerCase().includes(search));
|
|
63
|
+
}
|
|
64
|
+
|
|
49
65
|
/**
|
|
50
66
|
* Determines if an element matches the tag name filters
|
|
51
67
|
*/
|
|
@@ -84,7 +100,7 @@ function matchesAttributeFilters(
|
|
|
84
100
|
/**
|
|
85
101
|
* Determines if an element is interactable/fetchable based on platform
|
|
86
102
|
*/
|
|
87
|
-
function isInteractableElement(element: JSONElement, isNative: boolean, automationName:
|
|
103
|
+
function isInteractableElement(element: JSONElement, isNative: boolean, automationName: DriverName): boolean {
|
|
88
104
|
if (!isNative) {
|
|
89
105
|
const tag = element.tagName.toLowerCase();
|
|
90
106
|
const attrs = element.attributes;
|
|
@@ -99,20 +115,38 @@ function isInteractableElement(element: JSONElement, isNative: boolean, automati
|
|
|
99
115
|
(attrs.tabindex !== undefined && Number(attrs.tabindex) >= 0)
|
|
100
116
|
);
|
|
101
117
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
118
|
+
let interactableTags: string[];
|
|
119
|
+
switch (automationName) {
|
|
120
|
+
case 'uiautomator2':
|
|
121
|
+
interactableTags = [
|
|
122
|
+
'EditText',
|
|
123
|
+
'Button',
|
|
124
|
+
'ImageButton',
|
|
125
|
+
'CheckBox',
|
|
126
|
+
'RadioButton',
|
|
127
|
+
'Switch',
|
|
128
|
+
'ToggleButton',
|
|
129
|
+
'TextView',
|
|
130
|
+
];
|
|
131
|
+
break;
|
|
132
|
+
case 'xcuitest':
|
|
133
|
+
interactableTags = [
|
|
134
|
+
'XCUIElementTypeTextField',
|
|
135
|
+
'XCUIElementTypeSecureTextField',
|
|
136
|
+
'XCUIElementTypeButton',
|
|
137
|
+
'XCUIElementTypeImage',
|
|
138
|
+
'XCUIElementTypeSwitch',
|
|
139
|
+
'XCUIElementTypeStaticText',
|
|
140
|
+
'XCUIElementTypeTextView',
|
|
141
|
+
'XCUIElementTypeCell',
|
|
142
|
+
'XCUIElementTypeLink',
|
|
143
|
+
];
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
// Other native drivers use their own XML schemas. Do not require mobile
|
|
147
|
+
// element types or clickable/focusable attributes before generating XPath.
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
116
150
|
|
|
117
151
|
return (
|
|
118
152
|
interactableTags.some((tag) => element.tagName.includes(tag)) ||
|
|
@@ -7,6 +7,12 @@ import type {FilterOptions} from './element-filter.js';
|
|
|
7
7
|
import {getSuggestedLocators} from './locator-generation.js';
|
|
8
8
|
import {xmlToJSON} from './source-parsing.js';
|
|
9
9
|
import type {JSONElement} from './source-parsing.js';
|
|
10
|
+
import type {DriverName} from './types.js';
|
|
11
|
+
|
|
12
|
+
export interface LocatorGenerationOptions extends FilterOptions {
|
|
13
|
+
/** Additional XPath attributes, tried before the built-in attributes. */
|
|
14
|
+
xpathAttributes?: string[];
|
|
15
|
+
}
|
|
10
16
|
|
|
11
17
|
export interface ElementWithLocators {
|
|
12
18
|
tagName: string;
|
|
@@ -31,8 +37,8 @@ export interface ElementWithLocators {
|
|
|
31
37
|
export function generateAllElementLocators(
|
|
32
38
|
sourceXML: string,
|
|
33
39
|
isNative: boolean = true,
|
|
34
|
-
automationName:
|
|
35
|
-
filters:
|
|
40
|
+
automationName: DriverName,
|
|
41
|
+
filters: LocatorGenerationOptions = {},
|
|
36
42
|
): ElementWithLocators[] {
|
|
37
43
|
const sourceJSON = xmlToJSON(sourceXML);
|
|
38
44
|
const results: ElementWithLocators[] = [];
|
|
@@ -76,8 +82,8 @@ function processElement(
|
|
|
76
82
|
element: JSONElement,
|
|
77
83
|
sourceXML: string,
|
|
78
84
|
isNative: boolean,
|
|
79
|
-
automationName:
|
|
80
|
-
filters:
|
|
85
|
+
automationName: DriverName,
|
|
86
|
+
filters: LocatorGenerationOptions,
|
|
81
87
|
results: ElementWithLocators[],
|
|
82
88
|
): void {
|
|
83
89
|
if (!shouldIncludeElement(element, filters, isNative, automationName)) {
|
|
@@ -85,7 +91,7 @@ function processElement(
|
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
try {
|
|
88
|
-
const strategyMap = getSuggestedLocators(element, sourceXML, isNative, automationName);
|
|
94
|
+
const strategyMap = getSuggestedLocators(element, sourceXML, isNative, automationName, filters.xpathAttributes);
|
|
89
95
|
results.push(transformElementWithLocators(element, strategyMap));
|
|
90
96
|
} catch (error) {
|
|
91
97
|
log.error(`Error generating locators for element at path ${element.path}:`, error);
|
|
@@ -99,8 +105,8 @@ function traverseAndProcessElements(
|
|
|
99
105
|
element: JSONElement | null,
|
|
100
106
|
sourceXML: string,
|
|
101
107
|
isNative: boolean,
|
|
102
|
-
automationName:
|
|
103
|
-
filters:
|
|
108
|
+
automationName: DriverName,
|
|
109
|
+
filters: LocatorGenerationOptions,
|
|
104
110
|
results: ElementWithLocators[],
|
|
105
111
|
): void {
|
|
106
112
|
if (!element) {
|
|
@@ -7,6 +7,7 @@ import log from '../logger.js';
|
|
|
7
7
|
import {isEmpty, omitNilValues} from '../utils/collection.js';
|
|
8
8
|
import {childNodesOf, domToXML, findDOMNodeByPath, xmlToDOM} from './source-parsing.js';
|
|
9
9
|
import type {JSONElement, ElementAttributes} from './source-parsing.js';
|
|
10
|
+
import type {DriverName} from './types.js';
|
|
10
11
|
|
|
11
12
|
// Attributes on nodes that are likely to be unique to the node so we should consider first when
|
|
12
13
|
// suggesting xpath locators. These are considered IN ORDER.
|
|
@@ -84,7 +85,8 @@ export function getComplexSuggestedLocators(
|
|
|
84
85
|
path: string,
|
|
85
86
|
sourceDoc: XMLDocument,
|
|
86
87
|
isNative: boolean,
|
|
87
|
-
automationName:
|
|
88
|
+
automationName: DriverName,
|
|
89
|
+
xpathAttributes: string[] = [],
|
|
88
90
|
): Record<string, string> {
|
|
89
91
|
const complexLocators: Record<string, string | null> = {};
|
|
90
92
|
const domNode = findDOMNodeByPath(path, sourceDoc);
|
|
@@ -103,7 +105,7 @@ export function getComplexSuggestedLocators(
|
|
|
103
105
|
}
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
|
-
complexLocators.xpath = getOptimalXPath(sourceDoc, domNode);
|
|
108
|
+
complexLocators.xpath = getOptimalXPath(sourceDoc, domNode, xpathAttributes);
|
|
107
109
|
|
|
108
110
|
// Remove entries for locators where the optimal selector could not be found
|
|
109
111
|
return omitNilValues(complexLocators);
|
|
@@ -116,11 +118,18 @@ export function getSuggestedLocators(
|
|
|
116
118
|
selectedElement: JSONElement,
|
|
117
119
|
sourceXML: string,
|
|
118
120
|
isNative: boolean,
|
|
119
|
-
automationName:
|
|
121
|
+
automationName: DriverName,
|
|
122
|
+
xpathAttributes: string[] = [],
|
|
120
123
|
): [string, string][] {
|
|
121
124
|
const sourceDoc = xmlToDOM(sourceXML);
|
|
122
125
|
const simpleLocators = getSimpleSuggestedLocators(selectedElement.attributes, sourceDoc, isNative);
|
|
123
|
-
const complexLocators = getComplexSuggestedLocators(
|
|
126
|
+
const complexLocators = getComplexSuggestedLocators(
|
|
127
|
+
selectedElement.path,
|
|
128
|
+
sourceDoc,
|
|
129
|
+
isNative,
|
|
130
|
+
automationName,
|
|
131
|
+
xpathAttributes,
|
|
132
|
+
);
|
|
124
133
|
|
|
125
134
|
// Combine all locators
|
|
126
135
|
const allLocators = {...simpleLocators, ...complexLocators};
|
|
@@ -171,14 +180,20 @@ export function getSuggestedLocators(
|
|
|
171
180
|
/**
|
|
172
181
|
* Get an optimal XPath for a Node
|
|
173
182
|
*/
|
|
174
|
-
export function getOptimalXPath(doc: XMLDocument, domNode: XMLNode): string | null {
|
|
183
|
+
export function getOptimalXPath(doc: XMLDocument, domNode: XMLNode, xpathAttributes: string[] = []): string | null {
|
|
175
184
|
try {
|
|
176
185
|
// BASE CASE #1: If this isn't an element, we're above the root, return empty string
|
|
177
186
|
if (!domNode.nodeName || domNode.nodeType !== 1) {
|
|
178
187
|
return '';
|
|
179
188
|
}
|
|
180
189
|
|
|
181
|
-
|
|
190
|
+
// Attribute names are data, not XPath expressions. Namespaced attributes need
|
|
191
|
+
// namespace resolution and are intentionally not accepted here.
|
|
192
|
+
if (xpathAttributes.some((name) => !/^[A-Za-z_][A-Za-z0-9_.-]*$/.test(name))) {
|
|
193
|
+
throw new Error('Invalid XPath attribute name');
|
|
194
|
+
}
|
|
195
|
+
const preferredAttributes = [...new Set([...xpathAttributes, ...UNIQUE_XPATH_ATTRIBUTES])];
|
|
196
|
+
const attrsForPairs = [...new Set([...preferredAttributes, ...MAYBE_UNIQUE_XPATH_ATTRIBUTES])];
|
|
182
197
|
const attrPairsPermutations: [string, string][] = attrsForPairs.flatMap((v1, i) =>
|
|
183
198
|
attrsForPairs.slice(i + 1).map((v2) => [v1, v2] as [string, string]),
|
|
184
199
|
);
|
|
@@ -186,7 +201,7 @@ export function getOptimalXPath(doc: XMLDocument, domNode: XMLNode): string | nu
|
|
|
186
201
|
const cases = [
|
|
187
202
|
// BASE CASE #2: If this node has a unique attribute or content attribute, return an absolute
|
|
188
203
|
// XPath with that attribute
|
|
189
|
-
|
|
204
|
+
preferredAttributes,
|
|
190
205
|
|
|
191
206
|
// BASE CASE #3: If this node has a unique pair of attributes including 'maybe' attributes,
|
|
192
207
|
// return an xpath based on that pair
|
|
@@ -246,7 +261,7 @@ export function getOptimalXPath(doc: XMLDocument, domNode: XMLNode): string | nu
|
|
|
246
261
|
}
|
|
247
262
|
|
|
248
263
|
// Make a recursive call to this nodes parents and prepend it to this xpath
|
|
249
|
-
return (domNode.parentNode ? getOptimalXPath(doc, domNode.parentNode) : '') + xpath;
|
|
264
|
+
return (domNode.parentNode ? getOptimalXPath(doc, domNode.parentNode, xpathAttributes) : '') + xpath;
|
|
250
265
|
} catch (error) {
|
|
251
266
|
// If there's an unexpected exception, abort
|
|
252
267
|
logLocatorError('XPath', error);
|
|
@@ -514,13 +529,13 @@ function getUniqueXPath(
|
|
|
514
529
|
if (!attr1Value || !attr2Value) {
|
|
515
530
|
continue;
|
|
516
531
|
}
|
|
517
|
-
xpath = `//${tagForXpath}[@${attr1Name}
|
|
532
|
+
xpath = `//${tagForXpath}[@${attr1Name}=${xpathLiteral(attr1Value)} and @${attr2Name}=${xpathLiteral(attr2Value)}]`;
|
|
518
533
|
} else {
|
|
519
534
|
const attrValue = (domNode as XMLElement).getAttribute?.(attrName as string);
|
|
520
535
|
if (!attrValue) {
|
|
521
536
|
continue;
|
|
522
537
|
}
|
|
523
|
-
xpath = `//${tagForXpath}[@${attrName}
|
|
538
|
+
xpath = `//${tagForXpath}[@${attrName}=${xpathLiteral(attrValue)}]`;
|
|
524
539
|
}
|
|
525
540
|
const [isUnique, indexIfNotUnique] = determineXpathUniqueness(xpath, doc, domNode);
|
|
526
541
|
if (isUnique) {
|
|
@@ -547,3 +562,21 @@ function getUniqueXPath(
|
|
|
547
562
|
function logLocatorError(strategy: string, error: any): void {
|
|
548
563
|
log.error(`The most optimal ${strategy} could not be determined because an error was thrown: '${error}'`);
|
|
549
564
|
}
|
|
565
|
+
|
|
566
|
+
/** XPath 1.0 has no backslash escaping; mixed quotes require concat(). */
|
|
567
|
+
function xpathLiteral(value: string): string {
|
|
568
|
+
if (!value.includes('"')) {
|
|
569
|
+
return `"${value}"`;
|
|
570
|
+
}
|
|
571
|
+
if (!value.includes("'")) {
|
|
572
|
+
return `'${value}'`;
|
|
573
|
+
}
|
|
574
|
+
return (
|
|
575
|
+
'concat(' +
|
|
576
|
+
value
|
|
577
|
+
.split('"')
|
|
578
|
+
.map((part) => `"${part}"`)
|
|
579
|
+
.join(`, '"', `) +
|
|
580
|
+
')'
|
|
581
|
+
);
|
|
582
|
+
}
|
|
@@ -43,19 +43,12 @@ export const gestureSchema = z.object({
|
|
|
43
43
|
.enum(GESTURE_ACTIONS)
|
|
44
44
|
.describe(
|
|
45
45
|
[
|
|
46
|
-
'
|
|
47
|
-
'
|
|
48
|
-
'
|
|
49
|
-
'
|
|
50
|
-
'
|
|
51
|
-
'
|
|
52
|
-
'- swipe: dismiss a card, switch screens or tabs, navigate a carousel, or pull-to-refresh (use speed=fast).',
|
|
53
|
-
'For scroll/swipe, provide direction or all four custom coordinates: x, y, endX, endY.',
|
|
54
|
-
'- pinch_zoom: zoom in (scale > 1) or out (scale < 1) on maps, images, or other zoomable views. Requires scale.',
|
|
55
|
-
'- scroll_to_element: scroll to find a target using strategy and selector. Direction is up/down, default down.',
|
|
56
|
-
'Stops when the element is found, page source is unchanged after a scroll (likely end of content), or maxScrollAttempts is reached.',
|
|
57
|
-
'Adjust distance with scrollDistance (0.05–1) or scrollDistancePreset (small/medium/large).',
|
|
58
|
-
'- back: trigger system back navigation.',
|
|
46
|
+
'tap/double_tap/long_press: requires elementUUID or both x/y.',
|
|
47
|
+
'scroll/swipe: requires direction or x/y/endX/endY.',
|
|
48
|
+
'pinch_zoom: requires scale (<1 out, >1 in).',
|
|
49
|
+
'scroll_to_element: requires strategy/selector; direction up/down (default down).',
|
|
50
|
+
'Stops on a match, unchanged page source, or maxScrollAttempts. Adjust distance with scrollDistance or scrollDistancePreset.',
|
|
51
|
+
'back: system back navigation.',
|
|
59
52
|
].join('\n'),
|
|
60
53
|
),
|
|
61
54
|
|
|
@@ -130,10 +123,7 @@ export const gestureSchema = z.object({
|
|
|
130
123
|
.min(0.01)
|
|
131
124
|
.max(10)
|
|
132
125
|
.optional()
|
|
133
|
-
.describe(
|
|
134
|
-
'Required for pinch_zoom. Scale < 1 zooms out (fingers close); scale > 1 zooms in (fingers spread). ' +
|
|
135
|
-
'Examples: 0.5 zooms out; 2.0 zooms in 2x.',
|
|
136
|
-
),
|
|
126
|
+
.describe('Required for pinch_zoom: <1 zooms out, >1 zooms in (e.g. 0.5 or 2 for 2x).'),
|
|
137
127
|
velocity: z
|
|
138
128
|
.number()
|
|
139
129
|
.min(0.1)
|
|
@@ -14,6 +14,7 @@ import {z} from 'zod';
|
|
|
14
14
|
|
|
15
15
|
import {getCurrentContext, getPageSource} from '../../command.js';
|
|
16
16
|
import {generateAllElementLocators} from '../../locators/generate-all-locators.js';
|
|
17
|
+
import type {DriverName} from '../../locators/types.js';
|
|
17
18
|
import {LOCATOR_GENERATOR_URI} from '../../resources/locator-generator.js';
|
|
18
19
|
import {isAndroidUiautomator2DriverSession, isXCUITestDriverSession} from '../../session-store.js';
|
|
19
20
|
import {clientSupportsMcpApps, isMcpAppsEnabled} from '../../ui/mcp-apps.js';
|
|
@@ -24,16 +25,37 @@ export default function generateLocators(server: FastMCP): void {
|
|
|
24
25
|
const mcpAppsEnabled = isMcpAppsEnabled();
|
|
25
26
|
server.addTool({
|
|
26
27
|
name: 'generate_locators',
|
|
27
|
-
description: `Generate
|
|
28
|
+
description: `Generate current-page locator candidates for inspection. Defaults: controls for UiAutomator2/XCUITest/Web; all elements except hierarchy for other native drivers.`,
|
|
28
29
|
_meta: mcpAppsEnabled ? {ui: {resourceUri: LOCATOR_GENERATOR_URI}} : undefined,
|
|
29
30
|
parameters: z.object({
|
|
31
|
+
query: z
|
|
32
|
+
.string()
|
|
33
|
+
.optional()
|
|
34
|
+
.describe(
|
|
35
|
+
'Case-insensitive literal substring of an element tag or attribute value (name, label, text, ID, etc.). Searches all element types, including hidden elements. Blank or omitted keeps the default driver-specific filter.',
|
|
36
|
+
),
|
|
37
|
+
xpathAttributes: z
|
|
38
|
+
.array(
|
|
39
|
+
z
|
|
40
|
+
.string()
|
|
41
|
+
.trim()
|
|
42
|
+
.regex(/^[A-Za-z_][A-Za-z0-9_.-]*$/),
|
|
43
|
+
)
|
|
44
|
+
.max(20)
|
|
45
|
+
.optional()
|
|
46
|
+
.describe(
|
|
47
|
+
'Additional XPath attribute names in priority order, e.g. AutomationId or data-testid. Tried before built-in attributes; defaults remain as fallback. No namespace prefixes or XPath expressions.',
|
|
48
|
+
),
|
|
30
49
|
sessionId: z.string().optional().describe('Session ID; defaults to the active session.'),
|
|
31
50
|
}),
|
|
32
51
|
annotations: {
|
|
33
52
|
readOnlyHint: true,
|
|
34
53
|
openWorldHint: false,
|
|
35
54
|
},
|
|
36
|
-
execute: async (
|
|
55
|
+
execute: async (
|
|
56
|
+
args: {sessionId?: string; query?: string; xpathAttributes?: string[]},
|
|
57
|
+
context: any,
|
|
58
|
+
): Promise<any> => {
|
|
37
59
|
const {log} = context;
|
|
38
60
|
log.info('Getting page source');
|
|
39
61
|
const resolved = await resolveDriver(args.sessionId);
|
|
@@ -54,20 +76,22 @@ export default function generateLocators(server: FastMCP): void {
|
|
|
54
76
|
: driver.capabilities;
|
|
55
77
|
const driverName = String(
|
|
56
78
|
capabilities['appium:automationName'] ?? capabilities.automationName ?? '',
|
|
57
|
-
).toLowerCase();
|
|
79
|
+
).toLowerCase() as DriverName;
|
|
58
80
|
// Read the driver's current context: a cached NATIVE_APP value can be stale
|
|
59
81
|
// after attaching to a session that already has an active WebView.
|
|
60
82
|
const isNative = ['uiautomator2', 'xcuitest'].includes(driverName)
|
|
61
83
|
? (await getCurrentContext(driver)) === 'NATIVE_APP'
|
|
62
84
|
: !capabilities.browserName;
|
|
63
85
|
|
|
64
|
-
const
|
|
65
|
-
|
|
86
|
+
const query = args.query?.trim();
|
|
87
|
+
const elementsWithLocators = generateAllElementLocators(pageSource, isNative, driverName, {
|
|
88
|
+
...(query ? {query, fetchableOnly: false} : {fetchableOnly: true}),
|
|
89
|
+
...(args.xpathAttributes?.length ? {xpathAttributes: [...new Set(args.xpathAttributes)]} : {}),
|
|
66
90
|
});
|
|
67
91
|
|
|
68
92
|
const textResponse = textResult(
|
|
69
93
|
JSON.stringify({
|
|
70
|
-
interactableElements,
|
|
94
|
+
interactableElements: elementsWithLocators,
|
|
71
95
|
message: 'Page source retrieved successfully',
|
|
72
96
|
instruction: `These are the locators for the current page. Use this to generate code for the current page.
|
|
73
97
|
Using the template provided by generate://code-with-locators resource.`,
|
|
@@ -83,7 +107,7 @@ export default function generateLocators(server: FastMCP): void {
|
|
|
83
107
|
return addUIResourceToResponse(textResponse, () =>
|
|
84
108
|
createUIResource(
|
|
85
109
|
`ui://appium-mcp/locator-generator/${Date.now()}`,
|
|
86
|
-
createLocatorGeneratorUI(
|
|
110
|
+
createLocatorGeneratorUI(elementsWithLocators),
|
|
87
111
|
),
|
|
88
112
|
);
|
|
89
113
|
} catch (err: unknown) {
|