MCPcopy Create free account
hub / github.com/appium/appium / transformQuery

Function transformQuery

packages/universal-xml-plugin/lib/xpath.ts:41–72  ·  view source on GitHub ↗
(query: string, xmlStr: string, multiple: boolean)

Source from the content-addressed store, hash-verified

39 * @returns The transformed query string or null if no matches found
40 */
41export function transformQuery(query: string, xmlStr: string, multiple: boolean): string | null {
42 const nodes = runQuery(query, xmlStr);
43 if (!Array.isArray(nodes)) {
44 return null;
45 }
46
47 const newQueries = nodes
48 // nodes with no indexPath cannot be located on the device, so skip them deliberately.
49 // the only such node is the ios AppiumAUT root (see transformSourceXml)
50 .filter((node) => node.hasAttribute('indexPath'))
51 .map((node) => {
52 const indexPath = getNodeAttrVal(node, 'indexPath');
53 // at this point indexPath will look like /0/0/1/1/0/1/0/2
54 const newQuery = indexPath
55 .substring(1) // remove leading / so we can split
56 .split('/') // split into indexes
57 .map((indexStr) => {
58 // map to xpath node indexes (1-based)
59 const xpathIndex = parseInt(indexStr, 10) + 1;
60 return `*[${xpathIndex}]`;
61 })
62 .join('/'); // reapply /
63
64 // now to make this a valid xpath from the root, prepend the / we removed earlier
65 return `/${newQuery}`;
66 });
67
68 if (newQueries.length === 0) {
69 return null;
70 }
71 return multiple ? newQueries.join(' | ') : newQueries[0];
72}
73
74/**
75 * Gets the value of a node attribute

Callers 2

_findMethod · 0.85
xpath.spec.tsFile · 0.85

Calls 2

runQueryFunction · 0.85
getNodeAttrValFunction · 0.85

Tested by

no test coverage detected