(query: string, xmlStr: string, multiple: boolean)
| 39 | * @returns The transformed query string or null if no matches found |
| 40 | */ |
| 41 | export 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 |
no test coverage detected