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,163 @@
|
|
|
1
|
+
/** Browser controller for the optional screenshot and XML bounds overlay. */
|
|
2
|
+
export const INSPECTOR_SCREENSHOT_SCRIPT = String.raw`
|
|
3
|
+
function createScreenshotInspector(callTool, setSource, readSourceResult) {
|
|
4
|
+
const button = document.getElementById('takeScreenshotButton');
|
|
5
|
+
const panel = document.getElementById('screenshotPanel');
|
|
6
|
+
const image = document.getElementById('inspectorScreenshot');
|
|
7
|
+
const overlay = document.getElementById('elementOverlay');
|
|
8
|
+
const select = document.getElementById('screenshotElement');
|
|
9
|
+
const status = document.getElementById('screenshotStatus');
|
|
10
|
+
const details = document.getElementById('selectedXmlElement');
|
|
11
|
+
let elements = [];
|
|
12
|
+
let screen;
|
|
13
|
+
let query = '';
|
|
14
|
+
|
|
15
|
+
function readBounds(element) {
|
|
16
|
+
// Drivers differ in attribute casing. Prefer the exact lowercase spelling if both exist.
|
|
17
|
+
function attribute(name) {
|
|
18
|
+
if (element.hasAttribute(name)) return element.getAttribute(name);
|
|
19
|
+
return Array.from(element.attributes).find(attr => attr.name.toLowerCase() === name)?.value;
|
|
20
|
+
}
|
|
21
|
+
const android = (attribute('bounds') || '').match(/^\[(-?\d+),(-?\d+)\]\[(-?\d+),(-?\d+)\]$/);
|
|
22
|
+
if (android) {
|
|
23
|
+
const [, x, y, right, bottom] = android.map(Number);
|
|
24
|
+
return {x, y, width: right - x, height: bottom - y};
|
|
25
|
+
}
|
|
26
|
+
const coordinates = ['x', 'y', 'width', 'height'].map(attribute);
|
|
27
|
+
if (!coordinates.every(value => value !== undefined && value.trim() !== '')) return;
|
|
28
|
+
const values = coordinates.map(Number);
|
|
29
|
+
if (!values.every(Number.isFinite)) return;
|
|
30
|
+
const [x, y, width, height] = values;
|
|
31
|
+
return {x, y, width, height};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function showSelection() {
|
|
35
|
+
overlay.hidden = true;
|
|
36
|
+
details.textContent = '';
|
|
37
|
+
if (!select.value) return;
|
|
38
|
+
const element = elements[Number(select.value) - 1];
|
|
39
|
+
if (!element) return;
|
|
40
|
+
// Display only the selected opening tag, not its entire subtree.
|
|
41
|
+
const clone = element.cloneNode(false);
|
|
42
|
+
details.textContent = new XMLSerializer().serializeToString(clone);
|
|
43
|
+
if (!screen) {
|
|
44
|
+
status.textContent = 'Take a screenshot to highlight this element.';
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const rect = readBounds(element);
|
|
48
|
+
if (!rect || rect.width <= 0 || rect.height <= 0) {
|
|
49
|
+
status.textContent = 'This element has no usable screen bounds.';
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (element.getAttribute('visible') === 'false' || element.getAttribute('displayed') === 'false') {
|
|
53
|
+
status.textContent = 'This element is marked hidden in the XML.';
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const left = Math.max(0, rect.x);
|
|
57
|
+
const top = Math.max(0, rect.y);
|
|
58
|
+
const right = Math.min(screen.width, rect.x + rect.width);
|
|
59
|
+
const bottom = Math.min(screen.height, rect.y + rect.height);
|
|
60
|
+
if (right <= left || bottom <= top) {
|
|
61
|
+
status.textContent = 'This element is outside the captured screen.';
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
overlay.style.left = (left / screen.width * 100) + '%';
|
|
65
|
+
overlay.style.top = (top / screen.height * 100) + '%';
|
|
66
|
+
overlay.style.width = ((right - left) / screen.width * 100) + '%';
|
|
67
|
+
overlay.style.height = ((bottom - top) / screen.height * 100) + '%';
|
|
68
|
+
overlay.hidden = false;
|
|
69
|
+
status.textContent = 'XML bounds: x=' + rect.x + ', y=' + rect.y + ', width=' + rect.width + ', height=' + rect.height;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function filter(nextQuery) {
|
|
73
|
+
query = nextQuery.trim().toLowerCase();
|
|
74
|
+
const previous = select.value;
|
|
75
|
+
select.textContent = '';
|
|
76
|
+
const placeholder = document.createElement('option');
|
|
77
|
+
placeholder.value = '';
|
|
78
|
+
placeholder.textContent = 'Select an XML element to highlight';
|
|
79
|
+
select.appendChild(placeholder);
|
|
80
|
+
let retained = false;
|
|
81
|
+
elements.forEach((element, index) => {
|
|
82
|
+
const attributes = Array.from(element.attributes).map(attr => attr.value);
|
|
83
|
+
if (query && ![element.tagName, ...attributes].some(value => value.toLowerCase().includes(query))) return;
|
|
84
|
+
const option = document.createElement('option');
|
|
85
|
+
option.value = String(index + 1);
|
|
86
|
+
const name = element.getAttribute('name') || element.getAttribute('label') || element.getAttribute('text') || element.getAttribute('resource-id') || element.getAttribute('id');
|
|
87
|
+
option.textContent = (index + 1) + '. ' + element.tagName + (name ? ' — ' + name : '');
|
|
88
|
+
select.appendChild(option);
|
|
89
|
+
if (option.value === previous) retained = true;
|
|
90
|
+
});
|
|
91
|
+
select.value = retained ? previous : '';
|
|
92
|
+
showSelection();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function updateSource(source) {
|
|
96
|
+
screen = undefined;
|
|
97
|
+
image.hidden = true;
|
|
98
|
+
overlay.hidden = true;
|
|
99
|
+
elements = [];
|
|
100
|
+
select.value = '';
|
|
101
|
+
if (source.trim().startsWith('<')) {
|
|
102
|
+
const doc = new DOMParser().parseFromString(source, 'text/xml');
|
|
103
|
+
if (!doc.getElementsByTagName('parsererror').length) elements = Array.from(doc.getElementsByTagName('*'));
|
|
104
|
+
}
|
|
105
|
+
filter(query);
|
|
106
|
+
status.textContent = 'XML updated. Take a screenshot to refresh the preview.';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertSuccess(result) {
|
|
110
|
+
if (result?.isError) throw new Error((result.content || []).filter(item => item.type === 'text').map(item => item.text).join('\n') || 'Tool failed');
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function capture() {
|
|
115
|
+
if (button.disabled) return;
|
|
116
|
+
button.disabled = true;
|
|
117
|
+
panel.hidden = false;
|
|
118
|
+
screen = undefined;
|
|
119
|
+
image.hidden = true;
|
|
120
|
+
overlay.hidden = true;
|
|
121
|
+
status.textContent = 'Capturing screenshot and refreshing XML…';
|
|
122
|
+
try {
|
|
123
|
+
const sourceResult = assertSuccess(await callTool('appium_get_page_source', {saveToFile: false}));
|
|
124
|
+
const sizeResult = assertSuccess(await callTool('appium_get_window_size', {}));
|
|
125
|
+
const result = assertSuccess(await callTool('appium_screenshot', {returnRawBase64: true}));
|
|
126
|
+
const sizeText = (sizeResult.content || []).filter(item => item.type === 'text').map(item => item.text).join(' ');
|
|
127
|
+
const size = sizeText.match(/Width:\s*([\d.]+),\s*Height:\s*([\d.]+)/);
|
|
128
|
+
if (!size || !(Number(size[1]) > 0 && Number(size[2]) > 0)) throw new Error('Screen size is unavailable');
|
|
129
|
+
const picture = result.structuredContent?.screenshot || (result.content || []).find(item => item.type === 'image');
|
|
130
|
+
if (!picture?.data || !['image/png', 'image/jpeg'].includes(picture.mimeType)) throw new Error('No supported screenshot returned');
|
|
131
|
+
setSource(readSourceResult(sourceResult));
|
|
132
|
+
screen = {width: Number(size[1]), height: Number(size[2])};
|
|
133
|
+
image.src = 'data:' + picture.mimeType + ';base64,' + picture.data;
|
|
134
|
+
image.hidden = false;
|
|
135
|
+
status.textContent = 'Select an XML element below. Search filters the list.';
|
|
136
|
+
} catch (error) {
|
|
137
|
+
status.textContent = 'Screenshot failed: ' + error.message;
|
|
138
|
+
} finally {
|
|
139
|
+
button.disabled = false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
image.addEventListener('load', () => {
|
|
144
|
+
if (!screen) return;
|
|
145
|
+
// Pixel density is handled by percentages; orientation changes are not.
|
|
146
|
+
const ratio = image.naturalWidth / image.naturalHeight;
|
|
147
|
+
if (Math.abs(ratio / (screen.width / screen.height) - 1) > 0.03) {
|
|
148
|
+
screen = undefined;
|
|
149
|
+
overlay.hidden = true;
|
|
150
|
+
status.textContent = 'Screenshot and screen dimensions differ. Capture again before highlighting.';
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
image.addEventListener('error', () => {
|
|
154
|
+
screen = undefined;
|
|
155
|
+
image.hidden = true;
|
|
156
|
+
overlay.hidden = true;
|
|
157
|
+
status.textContent = 'Unable to display screenshot.';
|
|
158
|
+
});
|
|
159
|
+
button.addEventListener('click', capture);
|
|
160
|
+
select.addEventListener('change', showSelection);
|
|
161
|
+
return {updateSource, filter};
|
|
162
|
+
}
|
|
163
|
+
`;
|
package/src/ui/mcp-ui-utils.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {XML_FORMAT_SCRIPT, XML_SEARCH_SCRIPT} from './xml-inspector-scripts.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* MCP-UI Utility Functions
|
|
3
5
|
*
|
|
@@ -1022,6 +1024,10 @@ export function createPageSourceInspectorUI(pageSource: string): string {
|
|
|
1022
1024
|
outline: none;
|
|
1023
1025
|
border-color: #007AFF;
|
|
1024
1026
|
}
|
|
1027
|
+
mark { background: #ffd700; color: #000; }
|
|
1028
|
+
mark[data-active] { outline: 2px solid #91baff; background: #ff963c; }
|
|
1029
|
+
.search-status { padding: 6px 16px; font-size: 12px; color: #bbb; }
|
|
1030
|
+
.search-status:empty { display: none; }
|
|
1025
1031
|
</style>
|
|
1026
1032
|
</head>
|
|
1027
1033
|
<body>
|
|
@@ -1038,11 +1044,14 @@ export function createPageSourceInspectorUI(pageSource: string): string {
|
|
|
1038
1044
|
<button class="btn" onclick="generateLocators()">Generate Locators</button>
|
|
1039
1045
|
</div>
|
|
1040
1046
|
</div>
|
|
1047
|
+
<div id="searchStatus" class="search-status" role="status" aria-live="polite"></div>
|
|
1041
1048
|
<div class="viewer" title="Drag the bottom-right corner to resize vertically">
|
|
1042
1049
|
<pre class="xml-content" id="xmlContent">${escapedSource}</pre>
|
|
1043
1050
|
</div>
|
|
1044
1051
|
</details>
|
|
1045
1052
|
<script>
|
|
1053
|
+
${XML_FORMAT_SCRIPT}
|
|
1054
|
+
${XML_SEARCH_SCRIPT}
|
|
1046
1055
|
function copyToClipboard() {
|
|
1047
1056
|
const text = document.getElementById('xmlContent').textContent;
|
|
1048
1057
|
navigator.clipboard.writeText(text).then(() => {
|
|
@@ -1052,44 +1061,7 @@ export function createPageSourceInspectorUI(pageSource: string): string {
|
|
|
1052
1061
|
|
|
1053
1062
|
function formatXML() {
|
|
1054
1063
|
try {
|
|
1055
|
-
|
|
1056
|
-
const xmlDoc = parser.parseFromString(originalSource, 'text/xml');
|
|
1057
|
-
if (!xmlDoc.documentElement || xmlDoc.getElementsByTagName('parsererror').length) {
|
|
1058
|
-
throw new Error('Invalid XML');
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
function indentElement(element, depth) {
|
|
1062
|
-
// Do not introduce whitespace into mixed content or preserved subtrees.
|
|
1063
|
-
if (element.getAttribute('xml:space') === 'preserve') return;
|
|
1064
|
-
const children = Array.from(element.childNodes);
|
|
1065
|
-
if (children.some(child =>
|
|
1066
|
-
child.nodeType === 4 || (child.nodeType === 3 && child.nodeValue.trim())
|
|
1067
|
-
)) return;
|
|
1068
|
-
if (!children.some(child => child.nodeType === 1)) return;
|
|
1069
|
-
|
|
1070
|
-
for (const child of children) {
|
|
1071
|
-
if (child.nodeType === 3) {
|
|
1072
|
-
element.removeChild(child);
|
|
1073
|
-
continue;
|
|
1074
|
-
}
|
|
1075
|
-
if (child.nodeType === 1) indentElement(child, depth + 1);
|
|
1076
|
-
element.insertBefore(xmlDoc.createTextNode('\\n' + ' '.repeat(depth + 1)), child);
|
|
1077
|
-
}
|
|
1078
|
-
element.appendChild(xmlDoc.createTextNode('\\n' + ' '.repeat(depth)));
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
indentElement(xmlDoc.documentElement, 0);
|
|
1082
|
-
const serializer = new XMLSerializer();
|
|
1083
|
-
let formatted = Array.from(xmlDoc.childNodes)
|
|
1084
|
-
.filter(node => node.nodeType !== 3)
|
|
1085
|
-
.map(node => serializer.serializeToString(node))
|
|
1086
|
-
.join('\\n');
|
|
1087
|
-
// Browser DOMParser does not retain the XML declaration.
|
|
1088
|
-
const declaration = originalSource.match(/^\\s*(<\\?xml\\s[^?]*\\?>)/);
|
|
1089
|
-
if (declaration && !formatted.startsWith('<?xml ')) {
|
|
1090
|
-
formatted = declaration[1] + '\\n' + formatted;
|
|
1091
|
-
}
|
|
1092
|
-
displayedSource = formatted;
|
|
1064
|
+
displayedSource = formatXml(originalSource);
|
|
1093
1065
|
renderSource(document.getElementById('searchBox').value);
|
|
1094
1066
|
} catch (e) {
|
|
1095
1067
|
alert('Failed to format XML');
|
|
@@ -1101,7 +1073,7 @@ export function createPageSourceInspectorUI(pageSource: string): string {
|
|
|
1101
1073
|
type: 'tool',
|
|
1102
1074
|
payload: {
|
|
1103
1075
|
toolName: 'generate_locators',
|
|
1104
|
-
params: {}
|
|
1076
|
+
params: {query: document.getElementById('searchBox').value.trim()}
|
|
1105
1077
|
}
|
|
1106
1078
|
}, '*');
|
|
1107
1079
|
}
|
|
@@ -1110,42 +1082,12 @@ export function createPageSourceInspectorUI(pageSource: string): string {
|
|
|
1110
1082
|
const originalSource = xmlContent.textContent;
|
|
1111
1083
|
let displayedSource = originalSource;
|
|
1112
1084
|
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
const textLower = text.toLowerCase();
|
|
1116
|
-
let position = 0;
|
|
1117
|
-
|
|
1118
|
-
while (position < text.length) {
|
|
1119
|
-
const matchIndex = textLower.indexOf(searchLower, position);
|
|
1120
|
-
if (matchIndex === -1) {
|
|
1121
|
-
container.appendChild(document.createTextNode(text.slice(position)));
|
|
1122
|
-
return;
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
if (matchIndex > position) {
|
|
1126
|
-
container.appendChild(
|
|
1127
|
-
document.createTextNode(text.slice(position, matchIndex))
|
|
1128
|
-
);
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
const mark = document.createElement('mark');
|
|
1132
|
-
mark.style.background = '#ffd700';
|
|
1133
|
-
mark.style.color = '#000';
|
|
1134
|
-
mark.textContent = text.slice(matchIndex, matchIndex + searchTerm.length);
|
|
1135
|
-
container.appendChild(mark);
|
|
1136
|
-
position = matchIndex + searchTerm.length;
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1085
|
+
const searchBox = document.getElementById('searchBox');
|
|
1086
|
+
const xmlSearch = createXmlSearch(xmlContent, document.getElementById('searchStatus'));
|
|
1140
1087
|
function renderSource(searchTerm = '') {
|
|
1141
|
-
|
|
1142
|
-
if (!searchTerm) {
|
|
1143
|
-
xmlContent.textContent = displayedSource;
|
|
1144
|
-
return;
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
appendTextWithHighlights(xmlContent, displayedSource, searchTerm);
|
|
1088
|
+
xmlSearch.render(displayedSource, searchTerm);
|
|
1148
1089
|
}
|
|
1090
|
+
searchBox.addEventListener('keydown', xmlSearch.handleKeyDown);
|
|
1149
1091
|
|
|
1150
1092
|
// Search functionality
|
|
1151
1093
|
document.getElementById('searchBox').addEventListener('input', (e) => {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {INSPECTOR_SCREENSHOT_SCRIPT} from './inspector-screenshot-script.js';
|
|
2
|
+
import {XML_FORMAT_SCRIPT, XML_SEARCH_SCRIPT} from './xml-inspector-scripts.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Static MCP App used by appium_get_page_source.
|
|
3
6
|
*
|
|
@@ -35,10 +38,19 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
35
38
|
.viewer { height: 320px; min-height: 160px; resize: vertical; overflow: auto; padding: 20px; }
|
|
36
39
|
.xml-content, .selector { margin: 0; font: 12px/1.75 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
37
40
|
.xml-content { white-space: pre; color: #c5d3e7; }
|
|
41
|
+
mark[data-active] { outline: 2px solid #91baff; background: #ff963c; }
|
|
42
|
+
.search-status { padding: 6px 20px; color: var(--muted); font-size: 12px; }
|
|
43
|
+
.search-status:empty { display: none; }
|
|
38
44
|
mark { border-radius: 3px; background: #eac77a; color: #252019; }
|
|
39
45
|
.empty, .hint { color: var(--muted); }
|
|
40
46
|
.empty { font-family: inherit; }
|
|
41
47
|
.hint { margin: 6px 0 0; font-size: 12px; }
|
|
48
|
+
.screenshot-panel { border-top: 1px solid var(--line); padding: 20px; }
|
|
49
|
+
.screenshot-frame { position: relative; width: fit-content; max-width: 100%; margin: 12px auto; }
|
|
50
|
+
.screenshot-frame img { display: block; max-width: 100%; max-height: 480px; }
|
|
51
|
+
.element-overlay { position: absolute; border: 2px solid #ff963c; background: #ff963c33; pointer-events: none; }
|
|
52
|
+
#screenshotElement { width: 100%; margin: 10px 0; padding: 8px; background: var(--panel); color: var(--text); border: 1px solid var(--line); }
|
|
53
|
+
#selectedXmlElement { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; }
|
|
42
54
|
.locators { border-top: 1px solid var(--line); padding: 20px; }
|
|
43
55
|
h2 { margin: 0; font-size: 14px; font-weight: 600; }
|
|
44
56
|
#locatorStatus { margin: 16px 0 10px; color: var(--accent); font-size: 12px; }
|
|
@@ -59,21 +71,38 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
59
71
|
<span class="info" id="sourceInfo">Waiting for page source…</span>
|
|
60
72
|
</summary>
|
|
61
73
|
<div class="toolbar">
|
|
62
|
-
<input type="search" class="search-box" id="searchBox" placeholder="
|
|
74
|
+
<input type="search" class="search-box" id="searchBox" placeholder="Search elements…" aria-label="Search elements" title="Highlight XML text and find locator candidates by element tag or attribute value">
|
|
63
75
|
<div class="toolbar-right">
|
|
76
|
+
<button class="btn btn-secondary" id="takeScreenshotButton">Take screenshot</button>
|
|
64
77
|
<button class="btn btn-secondary" id="formatButton">Format XML</button>
|
|
65
78
|
<button class="btn btn-secondary" id="copyButton">Copy XML</button>
|
|
66
79
|
</div>
|
|
67
80
|
</div>
|
|
81
|
+
<div id="searchStatus" class="search-status" role="status" aria-live="polite"></div>
|
|
68
82
|
<div class="viewer" title="Drag the bottom-right corner to resize vertically">
|
|
69
83
|
<pre class="xml-content empty" id="xmlContent">Waiting for page source…</pre>
|
|
70
84
|
</div>
|
|
85
|
+
<section id="screenshotPanel" class="screenshot-panel" hidden aria-label="Screenshot preview">
|
|
86
|
+
<h2>Screenshot & XML elements</h2>
|
|
87
|
+
<p class="hint">Capture refreshes XML too. Select an element to overlay its bounds. Images and XML are captured sequentially; keep the screen still. Supports x/y/width/height and Android bounds.</p>
|
|
88
|
+
<p id="screenshotStatus" role="status" aria-live="polite"></p>
|
|
89
|
+
<div class="screenshot-frame">
|
|
90
|
+
<img id="inspectorScreenshot" alt="Device screenshot" hidden>
|
|
91
|
+
<div id="elementOverlay" class="element-overlay" hidden></div>
|
|
92
|
+
</div>
|
|
93
|
+
<label for="screenshotElement">XML element (filtered by Search)</label>
|
|
94
|
+
<select id="screenshotElement"><option value="">Select an XML element to highlight</option></select>
|
|
95
|
+
<pre id="selectedXmlElement"></pre>
|
|
96
|
+
</section>
|
|
71
97
|
<section class="locators" aria-labelledby="locatorsHeading">
|
|
72
98
|
<div class="section-header">
|
|
73
99
|
<h2 id="locatorsHeading">Locator candidates</h2>
|
|
74
|
-
<button class="btn" id="generateButton">
|
|
100
|
+
<button class="btn" id="generateButton">Find locators</button>
|
|
75
101
|
</div>
|
|
76
|
-
<p class="hint">
|
|
102
|
+
<p class="hint">Find locators for elements matching the search text in their type or attributes (name, label, text, ID, etc.). Matches ignore case and may include hidden elements. Fetches fresh XML; leave search blank for the default candidate list.</p>
|
|
103
|
+
<label class="hint" for="xpathAttributes">Preferred XPath attributes (optional)</label>
|
|
104
|
+
<input type="text" class="search-box" id="xpathAttributes" placeholder="AutomationId, data-testid" aria-describedby="xpathAttributesHelp" style="display:block;width:100%;margin-top:6px">
|
|
105
|
+
<p class="hint" id="xpathAttributesHelp">Comma-separated attribute names, in priority order. Existing attributes remain as fallback.</p>
|
|
77
106
|
<div id="locatorResults" hidden>
|
|
78
107
|
<p id="locatorStatus" role="status" aria-live="polite"></p>
|
|
79
108
|
<div class="locator-results" id="locatorOutput"></div>
|
|
@@ -82,6 +111,9 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
82
111
|
</details>
|
|
83
112
|
<script>
|
|
84
113
|
(() => {
|
|
114
|
+
${XML_FORMAT_SCRIPT}
|
|
115
|
+
${XML_SEARCH_SCRIPT}
|
|
116
|
+
${INSPECTOR_SCREENSHOT_SCRIPT}
|
|
85
117
|
const PAGE_SOURCE_PREFIX = 'Page source retrieved successfully: \\n\\\`\\\`\\\`xml ';
|
|
86
118
|
const xmlContent = document.getElementById('xmlContent');
|
|
87
119
|
const sourceInfo = document.getElementById('sourceInfo');
|
|
@@ -90,11 +122,16 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
90
122
|
const locatorResults = document.getElementById('locatorResults');
|
|
91
123
|
const locatorStatus = document.getElementById('locatorStatus');
|
|
92
124
|
const locatorOutput = document.getElementById('locatorOutput');
|
|
125
|
+
const xmlSearch = createXmlSearch(xmlContent, document.getElementById('searchStatus'));
|
|
93
126
|
let source = '';
|
|
94
127
|
let savedFile;
|
|
95
128
|
let nextRequestId = 1;
|
|
96
129
|
const pendingRequests = new Map();
|
|
97
130
|
|
|
131
|
+
const screenshotInspector = createScreenshotInspector(
|
|
132
|
+
(name, args) => sendRequest('tools/call', {name, arguments: args}), setSource, sourceFromToolResult,
|
|
133
|
+
);
|
|
134
|
+
|
|
98
135
|
function sendRequest(method, params) {
|
|
99
136
|
const id = nextRequestId++;
|
|
100
137
|
window.parent.postMessage({jsonrpc: '2.0', id, method, params}, '*');
|
|
@@ -128,30 +165,9 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
128
165
|
return text;
|
|
129
166
|
}
|
|
130
167
|
|
|
131
|
-
function appendHighlightedText(container, text, query) {
|
|
132
|
-
const lowerText = text.toLowerCase();
|
|
133
|
-
const lowerQuery = query.toLowerCase();
|
|
134
|
-
let position = 0;
|
|
135
|
-
|
|
136
|
-
while (position < text.length) {
|
|
137
|
-
const matchIndex = lowerText.indexOf(lowerQuery, position);
|
|
138
|
-
if (matchIndex === -1) {
|
|
139
|
-
container.appendChild(document.createTextNode(text.slice(position)));
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (matchIndex > position) {
|
|
143
|
-
container.appendChild(document.createTextNode(text.slice(position, matchIndex)));
|
|
144
|
-
}
|
|
145
|
-
const mark = document.createElement('mark');
|
|
146
|
-
mark.textContent = text.slice(matchIndex, matchIndex + query.length);
|
|
147
|
-
container.appendChild(mark);
|
|
148
|
-
position = matchIndex + query.length;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
168
|
function renderSource() {
|
|
153
169
|
const query = searchBox.value.trim();
|
|
154
|
-
|
|
170
|
+
xmlSearch.render('', '');
|
|
155
171
|
xmlContent.classList.toggle('empty', source.length === 0);
|
|
156
172
|
if (savedFile) {
|
|
157
173
|
sourceInfo.textContent = savedFile.bytes + ' bytes saved';
|
|
@@ -160,59 +176,22 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
160
176
|
}
|
|
161
177
|
if (!source) {
|
|
162
178
|
xmlContent.textContent = 'No page source was returned.';
|
|
163
|
-
} else if (!query) {
|
|
164
|
-
xmlContent.textContent = source;
|
|
165
179
|
} else {
|
|
166
|
-
|
|
180
|
+
xmlSearch.render(source, query);
|
|
167
181
|
}
|
|
168
182
|
sourceInfo.textContent = source.length + ' characters';
|
|
169
183
|
}
|
|
170
184
|
|
|
171
185
|
function setSource(nextSource) {
|
|
172
186
|
source = nextSource;
|
|
187
|
+
screenshotInspector.updateSource(source);
|
|
173
188
|
renderSource();
|
|
174
189
|
}
|
|
175
190
|
|
|
176
191
|
function formatXML() {
|
|
177
192
|
if (!source) return;
|
|
178
193
|
try {
|
|
179
|
-
|
|
180
|
-
const documentNode = parser.parseFromString(source, 'text/xml');
|
|
181
|
-
if (!documentNode.documentElement || documentNode.getElementsByTagName('parsererror').length) {
|
|
182
|
-
throw new Error('Invalid XML');
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function indentElement(element, depth) {
|
|
186
|
-
if (element.getAttribute('xml:space') === 'preserve') return;
|
|
187
|
-
const children = Array.from(element.childNodes);
|
|
188
|
-
const hasMixedContent = children.some((child) =>
|
|
189
|
-
child.nodeType === 4 || (child.nodeType === 3 && (child.nodeValue || '').trim()),
|
|
190
|
-
);
|
|
191
|
-
if (hasMixedContent || !children.some((child) => child.nodeType === 1)) return;
|
|
192
|
-
|
|
193
|
-
for (const child of children) {
|
|
194
|
-
if (child.nodeType === 3) {
|
|
195
|
-
element.removeChild(child);
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
if (child.nodeType === 1) indentElement(child, depth + 1);
|
|
199
|
-
element.insertBefore(documentNode.createTextNode('\\n' + ' '.repeat(depth + 1)), child);
|
|
200
|
-
}
|
|
201
|
-
element.appendChild(documentNode.createTextNode('\\n' + ' '.repeat(depth)));
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
indentElement(documentNode.documentElement, 0);
|
|
205
|
-
const serializer = new XMLSerializer();
|
|
206
|
-
let formattedSource = Array.from(documentNode.childNodes)
|
|
207
|
-
.filter((node) => node.nodeType !== 3)
|
|
208
|
-
.map((node) => serializer.serializeToString(node))
|
|
209
|
-
.join('\\n');
|
|
210
|
-
const declaration = source.match(/^\\s*(<\\?xml\\s[^?]*\\?>)/);
|
|
211
|
-
if (declaration && !formattedSource.startsWith('<?xml ')) {
|
|
212
|
-
formattedSource = declaration[1] + '\\n' + formattedSource;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
source = formattedSource;
|
|
194
|
+
source = formatXml(source);
|
|
216
195
|
renderSource();
|
|
217
196
|
} catch {
|
|
218
197
|
window.alert('Failed to format XML');
|
|
@@ -268,22 +247,44 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
268
247
|
});
|
|
269
248
|
}
|
|
270
249
|
|
|
250
|
+
function parseLocatorResult(result) {
|
|
251
|
+
const text = (result?.content || [])
|
|
252
|
+
.filter(item => item.type === 'text')
|
|
253
|
+
.map(item => item.text)
|
|
254
|
+
.join('\\n');
|
|
255
|
+
if (result?.isError) throw new Error(text || 'Tool returned an error');
|
|
256
|
+
const data = JSON.parse(text);
|
|
257
|
+
if (!Array.isArray(data.interactableElements)) throw new Error('No locator data returned');
|
|
258
|
+
return data.interactableElements;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function describeLocatorResults(count, query) {
|
|
262
|
+
if (query) {
|
|
263
|
+
const matches = count ? count + ' matching elements' : 'No matching elements';
|
|
264
|
+
return matches + ' for “' + query + '” in freshly fetched XML.';
|
|
265
|
+
}
|
|
266
|
+
if (!count) return 'No supported elements found in the fetched XML.';
|
|
267
|
+
return 'Candidates generated for ' + count + ' elements from freshly fetched XML.';
|
|
268
|
+
}
|
|
269
|
+
|
|
271
270
|
async function generateLocators() {
|
|
272
271
|
if (generateButton.disabled) return;
|
|
272
|
+
const query = searchBox.value.trim();
|
|
273
273
|
generateButton.disabled = true;
|
|
274
274
|
locatorResults.hidden = false;
|
|
275
275
|
locatorStatus.textContent = 'Fetching XML and generating candidates…';
|
|
276
276
|
locatorOutput.textContent = '';
|
|
277
277
|
try {
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
if (
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
278
|
+
const xpathAttributes = [...new Set((document.getElementById('xpathAttributes').value || '')
|
|
279
|
+
.split(',').map(name => name.trim()).filter(Boolean))];
|
|
280
|
+
if (xpathAttributes.length > 20 || xpathAttributes.some(name => !/^[A-Za-z_][A-Za-z0-9_.-]*$/.test(name))) {
|
|
281
|
+
throw new Error('Enter up to 20 attribute names, separated by commas (no XPath expressions or namespace prefixes).');
|
|
282
|
+
}
|
|
283
|
+
const args = query ? {query} : {};
|
|
284
|
+
if (xpathAttributes.length) args.xpathAttributes = xpathAttributes;
|
|
285
|
+
const result = await sendRequest('tools/call', {name: 'generate_locators', arguments: args});
|
|
286
|
+
const elements = parseLocatorResult(result);
|
|
287
|
+
locatorStatus.textContent = describeLocatorResults(elements.length, query);
|
|
287
288
|
renderLocators(elements);
|
|
288
289
|
} catch (error) {
|
|
289
290
|
locatorStatus.textContent = 'Failed to generate locators: ' + error.message;
|
|
@@ -313,7 +314,11 @@ export function createPageSourceInspectorAppUI(): string {
|
|
|
313
314
|
}
|
|
314
315
|
});
|
|
315
316
|
|
|
316
|
-
searchBox.addEventListener('input',
|
|
317
|
+
searchBox.addEventListener('input', () => {
|
|
318
|
+
renderSource();
|
|
319
|
+
screenshotInspector.filter(searchBox.value);
|
|
320
|
+
});
|
|
321
|
+
searchBox.addEventListener('keydown', xmlSearch.handleKeyDown);
|
|
317
322
|
document.getElementById('copyButton').addEventListener('click', copyToClipboard);
|
|
318
323
|
document.getElementById('formatButton').addEventListener('click', formatXML);
|
|
319
324
|
document.getElementById('generateButton').addEventListener('click', generateLocators);
|
|
@@ -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
|
+
|
|
42
|
+
export const XML_SEARCH_SCRIPT = String.raw`
|
|
43
|
+
function createXmlSearch(container, status) {
|
|
44
|
+
let matches = [];
|
|
45
|
+
let activeIndex = -1;
|
|
46
|
+
|
|
47
|
+
function render(text, query) {
|
|
48
|
+
container.textContent = '';
|
|
49
|
+
matches = [];
|
|
50
|
+
activeIndex = -1;
|
|
51
|
+
query = query.trim();
|
|
52
|
+
status.textContent = '';
|
|
53
|
+
if (!query) {
|
|
54
|
+
container.textContent = text;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const lowerText = text.toLowerCase();
|
|
58
|
+
const lowerQuery = query.toLowerCase();
|
|
59
|
+
let position = 0;
|
|
60
|
+
while (position < text.length) {
|
|
61
|
+
const index = lowerText.indexOf(lowerQuery, position);
|
|
62
|
+
if (index === -1) break;
|
|
63
|
+
container.appendChild(document.createTextNode(text.slice(position, index)));
|
|
64
|
+
const mark = document.createElement('mark');
|
|
65
|
+
mark.textContent = text.slice(index, index + query.length);
|
|
66
|
+
container.appendChild(mark);
|
|
67
|
+
matches.push(mark);
|
|
68
|
+
position = index + query.length;
|
|
69
|
+
}
|
|
70
|
+
container.appendChild(document.createTextNode(text.slice(position)));
|
|
71
|
+
status.textContent = matches.length ? matches.length + ' matches · Enter / Shift+Enter to navigate' : 'No matches';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function handleKeyDown(event) {
|
|
75
|
+
if (event.key !== 'Enter' || event.isComposing) return;
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
if (!matches.length) return;
|
|
78
|
+
if (activeIndex >= 0) matches[activeIndex].removeAttribute('data-active');
|
|
79
|
+
const step = event.shiftKey ? -1 : 1;
|
|
80
|
+
activeIndex = activeIndex < 0
|
|
81
|
+
? (event.shiftKey ? matches.length - 1 : 0)
|
|
82
|
+
: (activeIndex + step + matches.length) % matches.length;
|
|
83
|
+
const match = matches[activeIndex];
|
|
84
|
+
match.setAttribute('data-active', 'true');
|
|
85
|
+
match.scrollIntoView({block: 'nearest', inline: 'nearest'});
|
|
86
|
+
status.textContent = (activeIndex + 1) + ' / ' + matches.length + ' · Enter / Shift+Enter to navigate';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {render, handleKeyDown};
|
|
90
|
+
}
|
|
91
|
+
`;
|