ff-dom 2.0.9 → 3.0.0-beta.1
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/dist/index.browser.cjs +1 -1
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +1 -1
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cdn.js +1 -1
- package/dist/types/browser/browser/xpath.d.ts +35 -3
- package/dist/types/browser/utils/iosSelector.d.ts +7 -0
- package/dist/types/browser/utils/referenceXpath.d.ts +14 -2
- package/dist/types/browser/utils/xpath.d.ts +10 -2
- package/dist/types/browser/utils/xpathHelpers.d.ts +43 -3
- package/dist/types/node/utils/xpath.d.ts +10 -2
- package/dist/types/node/utils/xpathHelpers.d.ts +43 -3
- package/dist/xpath.mjs +1 -1
- package/dist/xpath.mjs.map +1 -1
- package/package.json +71 -71
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
export declare const reWhiteSpace: RegExp;
|
|
2
2
|
export declare let cspEnabled: boolean;
|
|
3
|
+
export declare const timeLog: (label: string, start: number) => void;
|
|
4
|
+
type EvalResult = {
|
|
5
|
+
first: Node | null;
|
|
6
|
+
second: Node | null;
|
|
7
|
+
};
|
|
8
|
+
export declare const clearXPathEvalCache: (node?: Node | null) => void;
|
|
9
|
+
export declare const evaluateXPathOnce: (xpath: string, docmt: Node) => EvalResult;
|
|
3
10
|
export declare let modifiedElementAttributes: {
|
|
4
11
|
url: string | null;
|
|
5
12
|
attributeName: string | null;
|
|
6
13
|
element: HTMLElement | Element;
|
|
7
14
|
doc: Document;
|
|
8
15
|
}[];
|
|
16
|
+
export declare const sanitizeAttributeValue: (attributeName: string, attributeValue: string | null | undefined) => string;
|
|
9
17
|
export declare const createObserver: (addedNodeCallBack: Function) => void;
|
|
10
18
|
export declare const startObserver: (target: Node, options: MutationObserverInit | undefined) => void;
|
|
11
19
|
export declare const stopObserver: () => void;
|
|
@@ -19,6 +27,12 @@ export declare const removeParenthesis: (xpath: string) => string;
|
|
|
19
27
|
export declare const findXpathWithIndex: (val: string, node: HTMLElement | Element, docmt: Node, count: number) => string | undefined;
|
|
20
28
|
export declare const replaceWhiteSpaces: (str: string) => string;
|
|
21
29
|
export declare const getShadowRoot: (el: HTMLElement | Element) => Element | null;
|
|
30
|
+
export declare const createShadowEvaluationContext: (root: ShadowRoot, element?: HTMLElement | Element | (HTMLElement | Element)[]) => {
|
|
31
|
+
owner: Document;
|
|
32
|
+
contextNode: HTMLDivElement;
|
|
33
|
+
cloneElement: Element | null;
|
|
34
|
+
cloneElements: (Element | null)[];
|
|
35
|
+
};
|
|
22
36
|
export declare const checkBlockedAttributes: (attribute: {
|
|
23
37
|
name: string;
|
|
24
38
|
value: string;
|
|
@@ -30,12 +44,24 @@ export declare const getPropertyXPath: (element: HTMLElement | Element, docmt: D
|
|
|
30
44
|
export declare const getAbsoluteXPath: (domNode: HTMLElement | Element | null, docmt: Document) => string;
|
|
31
45
|
export declare const getRelativeXPath: (domNode: HTMLElement | Element, docmt: Document, isIndex: boolean, isTarget: boolean | undefined, attributesArray: Attr[]) => any;
|
|
32
46
|
export declare const getCombinationXpath: (attribute: Attr, domNode: HTMLElement | Element) => string | undefined;
|
|
33
|
-
export declare const getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
|
|
47
|
+
export declare const getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document | ShadowRoot, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
|
|
34
48
|
export declare const intermediateXpathStep: (targetElemt: HTMLElement | Element, attr: {
|
|
35
49
|
name: string;
|
|
36
50
|
value: string;
|
|
37
51
|
}, isTarget: boolean) => string;
|
|
38
|
-
export declare const getFilteredTextXPath: (node: HTMLElement | Element, docmt: Document) => string;
|
|
52
|
+
export declare const getFilteredTextXPath: (node: HTMLElement | Element, docmt: Document | ShadowRoot) => string;
|
|
53
|
+
export declare const getNormalizedPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
54
|
+
export declare const getStartsWithPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
55
|
+
export declare const getContainsPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
56
|
+
export declare const getOrAttributesXPath: (element: HTMLElement | Element, attributes: Attr[]) => string;
|
|
57
|
+
export declare const getTagOnlyXPath: (element: HTMLElement | Element, docmt?: Document | ShadowRoot) => string;
|
|
58
|
+
export declare const getFirstMatchedNode: (xpath: string, docmt: Document | ShadowRoot) => HTMLElement | Element | null;
|
|
59
|
+
export declare const isExactUniqueXpath: (xpath: string, element: HTMLElement | Element, docmt: Document | ShadowRoot) => boolean;
|
|
60
|
+
export declare const getAxisNodeTest: (element: HTMLElement | Element) => string;
|
|
61
|
+
export declare const getUniqueNodeAnchorXpaths: (node: HTMLElement | Element, docmt: Document | ShadowRoot, isTarget: boolean) => {
|
|
62
|
+
key: string;
|
|
63
|
+
value: string;
|
|
64
|
+
}[];
|
|
39
65
|
export declare const getTextXpathFunction: (domNode: HTMLElement | Element) => string | undefined;
|
|
40
66
|
export declare const getXpathString: (node: HTMLElement | Element, attrName: string, attrValue: string) => string;
|
|
41
67
|
export declare const replaceActualAttributes: (str: string, element: {
|
|
@@ -67,7 +93,7 @@ export declare const xpathUtils: {
|
|
|
67
93
|
getAbsoluteXPath: (domNode: HTMLElement | Element | null, docmt: Document) => string;
|
|
68
94
|
getRelativeXPath: (domNode: HTMLElement | Element, docmt: Document, isIndex: boolean, isTarget: boolean | undefined, attributesArray: Attr[]) => any;
|
|
69
95
|
getCombinationXpath: (attribute: Attr, domNode: HTMLElement | Element) => string | undefined;
|
|
70
|
-
getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
|
|
96
|
+
getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document | ShadowRoot, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
|
|
71
97
|
getElementFromXpath: (xpath: string, docmt: Node, multi?: boolean) => Node | XPathResult | null;
|
|
72
98
|
isSvg: (element: HTMLElement | Element) => element is SVGElement;
|
|
73
99
|
findXpathWithIndex: (val: string, node: HTMLElement | Element, docmt: Node, count: number) => string | undefined;
|
|
@@ -97,6 +123,20 @@ export declare const xpathUtils: {
|
|
|
97
123
|
}[];
|
|
98
124
|
cspEnabled: boolean;
|
|
99
125
|
getXPathPattern: typeof getXPathPattern;
|
|
126
|
+
getNormalizedPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
127
|
+
getStartsWithPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
128
|
+
getContainsPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
|
|
129
|
+
getOrAttributesXPath: (element: HTMLElement | Element, attributes: Attr[]) => string;
|
|
130
|
+
getTagOnlyXPath: (element: HTMLElement | Element, docmt?: Document | ShadowRoot) => string;
|
|
131
|
+
getFirstMatchedNode: (xpath: string, docmt: Document | ShadowRoot) => HTMLElement | Element | null;
|
|
132
|
+
isExactUniqueXpath: (xpath: string, element: HTMLElement | Element, docmt: Document | ShadowRoot) => boolean;
|
|
133
|
+
getAxisNodeTest: (element: HTMLElement | Element) => string;
|
|
134
|
+
getUniqueNodeAnchorXpaths: (node: HTMLElement | Element, docmt: Document | ShadowRoot, isTarget: boolean) => {
|
|
135
|
+
key: string;
|
|
136
|
+
value: string;
|
|
137
|
+
}[];
|
|
100
138
|
escapeAttrValue: typeof escapeAttrValue;
|
|
101
139
|
isUniqueInDOM: typeof isUniqueInDOM;
|
|
140
|
+
sanitizeAttributeValue: (attributeName: string, attributeValue: string | null | undefined) => string;
|
|
102
141
|
};
|
|
142
|
+
export {};
|
package/dist/xpath.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{VirtualConsole as e,JSDOM as t}from"jsdom";const n=/^[\S]+( [\S]+)*$/i,a={};let l=[];const r=e=>/\d/.test(e),o=e=>e?.childNodes[0]?.nodeValue||"",s=(e,t,n,l=!1)=>{try{let r;if(void 0!==a[e])r=a[e];else{r=(9===n.nodeType?n:n.ownerDocument).evaluate(`count(${e})`,n,null,XPathResult.NUMBER_TYPE,null).numberValue,a[e]=r}if(l&&Array.isArray(t)){if(r>1){const a=d(e,n,!0);let l,r=0;if(a instanceof XPathResult)for(;(l=a?.iterateNext())&&(!t.includes(l)||(r+=1,2!==r)););if(2!==r)return 0}}else if(1===r){return d(e,n)===t?r:0}return r}catch(t){return console.error(`Error evaluating XPath: ${e}`,t),0}},i=e=>{if(e){if(-1!==e.indexOf('"'))return`'${e}'`;if(-1!==e.indexOf("'"))return`"${e}"`}return`'${e}'`},c=(e,t,n,a)=>{try{const l=9===n.nodeType?n:n.ownerDocument;let r=0;if(a){if(1===s(`${e}[${a}]`,t,n))return`${e}[${a}]`;if(1===s(`(${e})[${a}]`,t,n))return`(${e})[${a}]`}const o=l.evaluate(e,n,null,XPathResult.ANY_TYPE,null);let i;for(;i=o.iterateNext();)if(r++,i.isSameNode(t))return 1===s(`${e}[${r}]`,t,n)?`${e}[${r}]`:1===s(`(${e})[${r}]`,t,n)?`(${e})[${r}]`:void 0}catch(e){console.log(e)}},u=e=>e?e.replace(/\s\s+/g," ").trim():e,f=(e,t,n)=>{if(!e?.value||"boolean"==typeof e?.value)return!1;if(["true","false","on","off","flntooltip","flutter-highlight-overlay"].some(t=>t===e.value))return!1;if(["type","style","locator-data-tooltip","value"].some(t=>t===e.name))return!1;const a=l?.find(n=>n.doc===t.ownerDocument&&n.element===t&&n.attributeName===e.name);return!a&&(!(0===e?.name?.indexOf("on")&&e?.name?.length>3)&&("function"!=typeof e.value&&(!n||!r(e.value))))},m=e=>e instanceof SVGElement,d=(e,t,n=!1)=>{let a=t;t.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&(a=t.host??t);const l=a.nodeType===Node.DOCUMENT_NODE?a:a.ownerDocument;return n?l.evaluate(e,a,null,XPathResult.ANY_TYPE,null):l.evaluate(e,a,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue},h=(e,t,a,l,o,c)=>{if(l){const{tagName:f}=e;let d,h="";const p=[];let g;if(l&&(!c||!r(l))&&(g=n.test(l)?`//${f}[${a}=${i(l)}]`:((e,t,n)=>t?`//*[local-name()='${n}' and ${e}]`:`//${n}[${e}]`)(`normalize-space(${a})=${i(u(l)).trim()}`,m(e),e.tagName.toLowerCase()),d=s(g,e,t),1===d&&!o))return g;if(l&&c){const r=l.split(" ");if(r?.length)if(1===r.length){const c=[...new Set(r[0].match(/([^0-9]+)/g))];if(c?.length>=1&&c[0]&&u(c[0].trim())?.length>1&&l.startsWith(c[0])&&(h=n.test(c[0])?`starts-with(${a},${i(c[0]).trim()})`:`starts-with(${a},${i(u(c[0])).trim()})`),c?.length>1&&c[c.length-1]&&u(c[c.length-1].trim())?.length>1&&l.endsWith(c[c.length-1])&&(h=n.test(c[c.length-1])?`ends-with(${a},${i(c[c.length-1]).trim()})`:`ends-with(${a},${i(u(c[c.length-1])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g}else{const c=r.length%2==0?r.length/2:r.length%2,p=r.slice(0,c).join(" ");let $=[...new Set(p.match(/([^0-9]+)/g))];if($?.length&&$[0]&&u($[0].trim())?.length&&l.startsWith($[0])&&(h=n.test($[0])?`starts-with(${a},${i($[0]).trim()})`:`starts-with(${a},${i(u($[0])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g;const y=r.slice(c,r.length-1).join(" ");if($=[...new Set(y.match(/([^0-9]+)/g))],$?.length&&$[0]&&u($[0].trim())?.length>3&&l.endsWith($[0])&&(h=n.test($[0])?`ends-with(${a},${i($[0]).trim()})`:`ends-with(${a},${i(u($[0])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g}}if(l&&c&&r(l)){const r=[...new Set(l.match(/([^0-9]+)/g))];if(r?.length)for(let e=0;e<r?.length;e++)r[e]&&u(r[e].trim())?.length>1&&(n.test(r[e])?p.push(`contains(${a},${i(r[e].trim()).trim()})`):p.push(`contains(${a},${i(u(r[e])).trim()})`));if(p?.length&&(g=m(e)?`//*[local-name()='${f}' and ${p.join(" and ")}]`:`//${f}[${p.join(" and ")}]`,d=s(g,e,t),1===d&&!o))return g}if(g=m(e)?`//*[local-name()='${f}' and text()]`:`//${f}[text()]`,d=s(g,e,t),1===d&&!o)return g}},p=(e,t,a)=>{let l=m(e),r="";if(f(t,e,a)){let a=t.value;a=a.replace("removePointers","");const o=t.name;r=n.test(a)?l?`*[local-name()='${e.tagName}' and @${o}=${i(a)}]`:`${e.tagName||"*"}[@${o}=${i(a)}]`:l?`*[local-name()='${e.tagName}' and normalize-space(@${o})=${i(a)}]`:`${e.tagName||"*"}[normalize-space(@${o})=${i(a)}]`}return r},g=(e,t)=>{if(!e.textContent)return"";const a=o(e);let l;return l=n.test(a)?m(e)?`//*[local-name()='${e.tagName}' and .=${i(a)}]`:`//${e.tagName||"*"}[.=${i(a)}]`:m(e)?`//*[local-name()='${e.tagName}' and normalize-space(.)=${i(a)}]`:`//${e.tagName||"*"}[normalize-space(.)=${i(a)}]`,l},$=(e,t,n)=>{const a=new RegExp(/^[\S]+( [\S]+)*$/gi);let l="";return n&&(l=a.test(n)?"class"===t?m(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${i(n)})]`:`//${e.tagName||"*"}[contains(@${t},${i(n)})]`:m(e)?`//*[local-name()='${e.tagName}' and @${t}=${i(n)}]`:`//${e.tagName||"*"}[@${t}=${i(n)}]`:m(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${i(n)})]`:`//${e.tagName||"*"}[contains(@${t},${i(n)})]`),l},y=e=>e=(e=e.replace(/text\(\)\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1")).replace(/\.\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1");function v(e){const t=function(e){return e.toLowerCase().replace(/'[^']*'/g,"?").replace(/"[^"]*"/g,"?").replace(/\[\d+\]/g,"[?]").replace(/\/\/[a-z0-9_-]+/g,"//node").replace(/\/[a-z0-9_-]+/g,"/node")}(e),n=function(e){const t=e.toLowerCase(),n=t.match(/(ancestor-or-self|ancestor|descendant-or-self|descendant|following-sibling|preceding-sibling|following|preceding|parent|child|self)::/),a=n?.[1]??"none",l=t.match(/@([a-z0-9:-]+)/);return{axis:a,attribute:l?.[1]??"none",usesNormalize:t.includes("normalize-space")}}(e);return["XPATH",`axis:${n.axis}`,`attr:${n.attribute}`,`normalize:${n.usesNormalize}`,`shape:${t}`].join("|")}function N(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/ /g,"\\ ")}function E(e,t,n,a){const l=a?.getRootNode?.()??e,r=e=>{try{return Array.from(l.querySelectorAll(e))}catch{return[]}};try{switch(t){case"id":return 1===r(`#${N(n)}`).length;case"name":return 1===r(`[name="${N(n)}"]`).length;case"className":return 1===r(`.${n}`).length;case"tagName":case"cssSelector":return 1===r(n).length;case"linkText":return 1===r("a").filter(e=>e.textContent?.trim()===n).length;case"partialLinkText":return 1===r("a").filter(e=>e.textContent?.includes(n)).length;default:return!1}}catch{return!1}}let x=[],b=[],S=new Map,w=new Map;const R=new Map,k=(e,t,n,a,l,r)=>{if(e){let o;o="parent"===r?[`${e}/descendant-or-self::${t}`,`${e}/following::${t}`]:[`${e}/ancestor-or-self::${t}`,`${e}/preceding::${t}`];for(const e of o){S?.get(e)||S.set(e,s(e,n,a));const t=S?.get(e);if(1===t)return e;if(t>1&&(b.length?t<b[0].count&&(b.pop(),b.push({key:"relative xpath by unique parent "+(l?"index":""),value:e,count:t})):b.push({key:"relative xpath by unique parent "+(l?"index":""),value:e,count:t})),t>1&&l&&!x.length){const r=c(e,n,a,t);r&&1===s(r,n,a)&&x.push({key:"relative xpath by unique parent "+(l?"index":""),value:r})}}}return null},C=(e,t,a,l,c,d)=>{try{if(R.has(e))return R.get(e);const h=[];let p=e;for(;p&&1===p.nodeType;){const e=!1;for(const e of Array.from(p.attributes))if(f(e,p,l)){let n=e.nodeValue;n=n?.replace("removePointers","")??"";const l=e.name,r=$(p,l,n);let o;try{o=k(r,c,a,t,d,"parent")}catch(e){continue}if(o)return o}if(p.textContent&&!p.textContent)if(!l||l&&!r(p.textContent)){let e;e=n.test(p.textContent)?m(p)?`//*[local-name()='${p.tagName}' and .=${i(o(p))}]`:`//${p.tagName||"*"}[.=${i(o(p))}]`:m(p)?`//*[local-name()='${p.tagName}' and normalize-space(.)=${i(o(p))}]`:`//${p.tagName||"*"}[normalize-space(.)=${i(o(p))}]`;const l=k(e,c,a,t,d,"parent");if(l)return l}else{const e=[],n=[...new Set(o(p).match(/([^0-9]+)/g))],l=new RegExp(/^[\S]+( [\S]+)*$/gi);if(n?.length)for(let t=0;t<n?.length;t++)n[t]&&u(n[t].trim())&&(l.test(n[t])?e.push(`contains(.,${i(n[t].trim()).trim()})`):e.push(`contains(.,${i(u(n[t])).trim()})`));if(e?.length){const n=m(p)?`//*[local-name()='${p.tagName}' and ${e.join(" and ")}]`:`//${p.tagName||"*"}[${e.join(" and ")}]`,l=k(n,c,a,t,d,"parent");if(l)return l}}if(!e){const e=m(p)?`/*[local-name()='${p.tagName}']`:`/${p.tagName}`;h.unshift(e)}p=p.parentElement}const g=h.join("")+c;if(1===s(g,e,t))return R.set(e,g),g}catch(e){return console.error(e),null}},T=(e,t,n,a)=>{const l=new Map;if(l.has(e))return l.get(e);const r=[];let o=e;try{for(;o&&1===o.nodeType;){if(!o.tagName)return"";let e=!1;for(const n of Array.from(o.attributes))if(f(n,o,a)){let a=n.nodeValue;a=a?.replace("removePointers","")??"";const l=n.name,i=$(o,l,a);let c;try{c=s(i,o,t)}catch(e){continue}if(1===c){r.unshift(i),e=!0;break}}if(!e&&o.textContent&&!n.textContent){const n=g(o);if(n){if(1===s(n,o,t)){e=!0,r.unshift(n);break}}}if(e)break;{const e=m(o)?`/*[local-name()='${o.tagName}']`:`/${o.tagName}`;r.unshift(e)}o=o.parentElement}const i=r.join("");return l.set(e,i),i}catch(e){return console.log(e),null}},P=(e,t,n)=>{const a=[];let l;const r=[];null!=e.firstElementChild&&e.firstElementChild.classList.contains("flntooltip")?r.unshift(e.firstElementChild):null!=e.nextElementSibling&&r.unshift(e.nextElementSibling);for(let t=e.parentElement;null!=t&&1===t.nodeType;t=t.parentElement)t.nextElementSibling&&r.unshift(t.nextElementSibling);try{do{let e=!1;for(l=r.pop();null!==l;){for(const n of Array.from(l.attributes))if(f(n,l,!0)){let r=n.nodeValue;r=r?.replace("removePointers","")??"";const o=n.name,i=$(l,o,r);let c;try{c=s(i,l,t)}catch(e){continue}if(1===c){e=!0,a.push(i);break}}if(!e&&l.textContent&&!n.textContent){const n=g(l);if(n){if(1===s(n,l,t)){e=!0,a.push(n);break}}}if(e)break;{const e=m(l)?`/*[local-name()='${l.tagName}']`:`/${l.tagName}`;a.push(e),null!=l.firstElementChild?(r.push(l.nextElementSibling),l=l.firstElementChild):l=l.nextElementSibling}}}while(r.length>0);const o=a.join("");return w.set(e,o),o}catch(e){return console.log(e),null}},A=(e,t,n,a)=>{try{const l=document.querySelector(".flntooltip");for(let r=e.nextElementSibling;null!==r&&r!==l;r=r.nextElementSibling)D(r,e,t,a,"preceding-sibling",n);for(let r=e.previousElementSibling;null!==r&&r!==l;r=r.previousElementSibling)D(r,e,t,a,"following-sibling",n)}catch(e){return console.error("sibling error",e),null}},D=(e,t,n,a,l,r)=>{try{if(e.hasAttributes())for(const t of Array.from(e.attributes)){let o=p(e,{name:t.name,value:t.value},r);if(o){o+=`/${l}::${a}`;const t=s(o,e,n);if(1===t)return void x.push({key:`xpath by ${l}`,value:o});t>1&&(b.length?t<b[0].count&&(b.pop(),b.push({key:`relative xpath by ${l}`,value:o,count:t})):b.push({key:`relative xpath by ${l}`,value:o,count:t}))}}if(!r){let t;if(t=p(e,{name:"text",value:e.textContent},r),t){const a=s(t,e,n);if(1===a)return void x.push({key:`xpath by ${l}`,value:t});a>1&&(b.length?a<b[0].count&&(b.pop(),b.push({key:`relative xpath by ${l}`,value:t,count:a})):b.push({key:`relative xpath by ${l}`,value:t,count:a}))}}}catch(e){console.log(`${l} xpath-error`,e)}};const Y=(e,t,n,a)=>{if(""!=(e.textContent??"").trim()){const l=(e=>{const t=e?.textContent;return t})(e);if(l)return h(e,t,".",l,n,a)}},L=(e,t,a,l,r)=>{const o=e;try{o.map(e=>{if("className"!==e.name&&"class"!==e.name&&f(e,t,r)){const n=((e,t,n,a,l)=>{let r;return r=e.name,h(t,n,`@${r}`,e.value,a,l)})(e,t,a,l,r);n&&x.push({key:`xpath by ${e.name}${l?" index":""}`,value:n})}});const i=Y(t,a,l,r);if(i&&x.push({key:"xpath by text"+(l?" index":""),value:i}),o.find(e=>"className"===e.name)&&f(o?.find(e=>"className"===e.name),t,r)){let e=((e,t,n,a)=>{let l=e.className;if("string"!=typeof l&&(l=""),l=l?.replace("flndisabled","disabled"),l=l?.replace("removePointers",""),l=l?.trim(),l)return h(e,t,"@class",l,n,a)})(t,a,l,r);e&&x.push({key:"xpath by class",value:e})}if(!x.length){const n=function(e,t,n,a){const{tagName:l}=t,r=t.textContent.trim();for(const o of e)if(f(o,t,a)){let e=o.nodeValue;e=e?.replace("removePointers","")??"";const a=`//${l}[@${o.name}='${e}' and text()='${r}']`;if(a&&1==s(a,t,n))return a}}(e,t,a,r);n&&x.push({key:"xpath by textAttribute",value:n})}if(!x.length&&o.length>1){const e=((e,t,a,l)=>{try{const r=[];if(a.length>1)for(const o of a)if(f(o,e,l)){const a=o.nodeValue;n.test(a)?"class"===o.nodeName?r.push(`contains(@${o.nodeName},"${a}")`):r.push(`@${o.nodeName}="${a}"`):r.push(`normalize-space(@${o.nodeName})="${a}"`);const l=m(e)?`//*[local-name()='${e.tagName}' and ${r.join(" and ")}]`:`//${e.tagName}[${r.join(" and ")}]`;let i;try{i=s(l,e,t)}catch(e){continue}if(1===i&&!l.includes("and"))return"";if(1===i&&l.includes("and"))return l}}catch(e){console.log(`'${JSON.stringify(e,null,2)}'`)}})(t,a,o,r);e&&x.push({key:"xpath by combination",value:e})}}catch(e){console.log(e)}},_=(e,t,n,a,l=[])=>{x=[],console.log(e);const r=e,o=r?.ownerDocument||t,i=r.tagName,{attributes:u}=r,f=l.length>0?l:Array.from(u);if(L(f,r,o,n,a),x.length){const t=x.length;for(let n=0;n<t;n++){let t=x[n].value;t="//*"+t.substring(t.indexOf("//")+2+i.length);1===s(t,e,o)&&x.push({key:`${x[n].key} regex`,value:t})}}return x.length||((e,t,n,a,l)=>{try{let r,o,i=[];if(x=[],console.log(l),l)for(const t of l){let n=p(e,{name:t.name,value:t.value},a);console.log(n),n&&i.push(n)}if(e.textContent){let t=p(e,{name:"text",value:e.textContent},a);console.log(t),t&&i.push(t)}if(i.push(e.tagName),i?.length)for(let l=0;l<i.length;l++)if(!x.length){if(A(e,t,a,i[l]),!x.length&&(r||(r=T(e.parentElement,t,e,a)),console.log(r),r&&(r.includes("@")||r.includes("text()")||r.includes(".="))&&r.match(/\//g)?.length-2<5)){const a=r+`/${i[l]}`,o=s(a,e,t);if(1===o)x.push({key:"relative xpath by relative parent",value:a});else if(o>1&&n){const l=c(a,e,t,o);l&&1===s(l,e,t)&&x.push({key:"relative xpath by relative parent "+(n?"index":""),value:l})}}if(!x.length&&(o||(o=P(e,t,e)),o&&(o.includes("@")||o.includes("text()")||o.includes(".=")))){const a=`/${i[l]+o.substring(1)}`,r=s(a,e,t);if(1===r)x.push({key:"relative xpath by relative child",value:a});else if(r>1&&n){const l=c(a,e,t,r);l&&1===s(l,e,t)&&x.push({key:"relative xpath by relative parent "+(n?"index":""),value:l})}}if(1===x?.length&&x?.[0]?.value?.match(/\[([0-9]+)\]/gm)?.length>3&&e.textContent){const a=Y(e,t,n,!1);a&&x.unshift({key:"xpath by text"+(n?"index":""),value:a})}if(!x.length){let r=C(e.parentElement,t,e,a,i[l],n);r&&x.push({key:"xpath by unique parent",value:r})}}return x}catch(e){console.log(e)}})(r,o,n,a,Array.from(r.attributes)),x};let O=[];const j=(e,t,n)=>{try{const a=n.querySelectorAll(e);return 1===a.length&&a[0]===t}catch{return!1}},V=(e,t="single")=>{const n=[],a=e.getRootNode();try{const l=q(e);if(l&&j(l,e,a)&&(n.push({key:"cssSelector by id",value:l}),"single"===t))return n;const r=I(e);r&&j(r,e,a)&&n.push({key:"cssSelector by class",value:r});const o=H(e);o&&n.push({key:"cssSelector by name",value:o});const s=M(e);s&&j(s,e,a)&&n.push({key:"Absolute cssSelector",value:s})}catch(e){console.error(e)}return n},q=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if(e.id&&!r(e.id)){t+=`#${CSS.escape(e.id)}`,a.unshift(t);break}{let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}a.unshift(t),e=e.parentElement}return a.join(" > ")},z=new Set(["id","class","style"]);function X(e){return Array.from(e.attributes).filter(e=>!z.has(e?.name?.toLowerCase())&&e.value&&!r(e.value)).map(e=>{return`[${e.name}="${t=e.value,String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"]`;var t})}const H=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.getRootNode(),a=e.tagName.toLowerCase();if("style"===a||"script"===a)return;const l=X(e);for(const t of l){const l=`${a}${t}`;if(j(l,e,n))return l}},I=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if("string"!=typeof e.className||!e.className||r(e.className)||O?.find(t=>t.element===e&&"class"===t.attributeName)){let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}else if(e.classList.remove("marked-element-temp"),e.classList.remove("removePointers"),e.className){t+=`.${e.className.trim().replace(/\s+/g,".")}`,a.unshift(t);break}a.unshift(t),e=e.parentElement}return a.join(" > ")},M=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=[];for(;e&&e.nodeType===Node.ELEMENT_NODE;){const t=e.tagName.toLowerCase();if("style"===t||"script"===t)return;let a=t;const l=e.parentNode;if(l){const t=Array.from(l.children).filter(t=>t.tagName===e.tagName);t.length>1&&(a+=`:nth-of-type(${t.indexOf(e)+1})`)}n.unshift(a),e=e.parentElement}return n.join(" > ")},W=(e,t,n)=>{const a=y(e);if(te(t,a)===n)return!0;if(function(e){return/\[(?:\s*\.|\s*contains\s*\(\s*\.|\s*normalize-space\s*\(\s*\.)/.test(e)}(a)){const e=t.evaluate(a,t,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);for(let t=0;t<e.snapshotLength;t++)if(e.snapshotItem(t)===n)return!0}return!1},F=(e,t)=>{try{const n=e.querySelector(t);if(n)return n}catch(e){return console.error("Invalid CSS selector:",t,e),null}return B(e.body,t)},G=(e,t,n)=>F(t,e)===n,U=e=>e.name?.toLowerCase().includes("cssselector")??!1,B=(e,t)=>{const n=Array.from(e.querySelectorAll("*"));try{for(let e=0;e<n.length;e++)if(n[e].shadowRoot){const{shadowRoot:a}=n[e];if(a){const e=B(a,t);if(e)return e;if(a&&!t.includes("dynamic"))return a.querySelector(t)}}}catch(e){console.log(e)}return null},J=e=>e?.id||null,Z=e=>e.className||null,K=e=>e.textContent?.trim()||null,Q=e=>{const t=e;if(t.hasAttribute("name")){return`${t.getAttribute("name")}`||null}return null},ee=["/preceding-sibling","/following-sibling","/parent","/descendant","/ancestor","/self","/ancestor-or-self","/child","/preceding","/following"];function te(e,t){const n=e.defaultView;if(!n)return null;return(new n.XPathEvaluator).evaluate(t,e,null,n.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}function ne(e,t,n){if(e.includes(t)){const a=e.split(t)[0].trim(),l=n.defaultView;if(!l)return null;if(!e.includes("dynamic")){const t=new l.XPathEvaluator;if(t.evaluate(a,n,null,l.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue){let a;return t.evaluate(e,n,null,l.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue?(a=e,a):(console.error("Complete Locator is Invalid:",e),a=e,a)}console.error("Source Locator Not Found:",a)}}return null}const ae=(e,t)=>{const n=t.querySelectorAll("script, style, link[rel='stylesheet'], meta, noscript, embed, object, param, source, svg");n&&n.forEach(e=>{e.remove()});const a=new Set;let l=[];function o(e,t={}){const n=e?.value,r=t.value??e?.value,o={name:t.name??e?.name,type:t.type??e?.type,value:t.value??e?.value,reference:t.reference??e?.reference,status:t.status??e?.status,isRecorded:t.isRecorded??e?.isRecorded},s="Y"===e?.isSelfHealed;o.isSelfHealed=s?"Y":t.hasOwnProperty("isSelfHealed")?t.isSelfHealed:((e,t,n)=>t&&n&&t===n?null:"Y")(o.name,n,r),function(e){const t=`${e.name}:${e.value}`;a.has(t)||(a.add(t),l.push(e))}(o)}function s(e,t,n){if(U(t))return F(e,n);if(t.name.includes("id")||n.startsWith("#"))return e.querySelector("#"+N(n));if(t.name.includes("className")||n.startsWith("."))return e.querySelector("."+n);if("name"===t.name){const t=N(n);return e.querySelector(`[name="${t}"]`)}if("tagName"===t.name)return e.querySelector(n);if("linkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.trim()===n)||null;if("partialLinkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.includes(n))||null;if(!t.name.includes("xpath")&&!n.startsWith("//")||t.type.match("dynamic"))return e.querySelector(n);{const a=te(e,y(n));return a&&o(t,{value:n,isRecorded:String(t.isRecorded).includes("N")?"N":"Y"}),a}}function i(e,t,n){const a=e.querySelectorAll("iframe");for(const e of a)try{const a=e.contentDocument||e.contentWindow?.document;if(!a)continue;const l=s(a,t,n);if(l)return l}catch{continue}return null}const c=(e,t,n)=>{if(!e)return null;let a=e.trim();return a&&"null"!==a.toLowerCase()?(a=a.replace(/\\"/g,'"').replace(/\\'/g,"'"),a=a.replace(/^['"](.+?)['"]$/,"$1"),a=a.replace(/''/g,"'"),a=a.replace(/\[@(id|name)=['"]{2}(.+?)['"]{2}\]/g,"[@$1='$2']"),"id"!==t&&"name"!==t||(a=a.replace(/['"]/g,"").trim()),"xpath"!==t||"Y"!==n||e.startsWith("//")?!a||/^['"]{2}$/.test(a)?null:a:null):null};e:for(const n of e.locators)try{String(n.isRecorded||"");const u=e.locators.filter(e=>"N"===e.isRecorded);if(u.length>0)for(const e of u)o(e);const f=String(n.value||n.type||"");if(f.includes("dynamic")||f.match("dynamic")||f.includes("{")||f.includes("}")){o(n);continue}if(e.isShared.includes("Y"))break e;try{let u=null;const f=n.value.split(">>>");for(const e of f){if(!t){console.error("Element not found at:",e);break}const a=e.trim();if(u=s(t,n,a),u||(u=i(t,n,a)),u||(u=B(t.body,a)),!u){console.error("Element not found at:",a);break}}const m=(e,t)=>{const n=`${e}:${t}`;return a.has(n)};if(u){const n=e.locators.filter(e=>"xpath"===e.name&&e.value),a=e.locators.filter(e=>U(e)&&e.value);for(const e of n)W(e.value,t,u)&&o(e,{isSelfHealed:null});for(const e of a)G(e.value,t,u)&&o(e,{isSelfHealed:null});const s=l.filter(e=>"xpath"===e.name&&e.value),i=new Set(s.map(e=>v(e.value))),f=[],d=J(u);if(d&&!m("id",d)&&!r(d)){const n=e.locators.find(e=>"id"===e.name);E(t,"id",d,u)&&(f.push("id"),o(n,{name:"id",type:"static",isRecorded:"Y",value:d}))}const h=u.tagName;if(h&&!m("tagName",h)){const n=e.locators.find(e=>"tagName"===e.name);E(t,"tagName",h,u)&&(f.push("tagName"),o(n,{name:"tagName",type:"static",isRecorded:"Y",value:h}))}const p=K(u);if(p&&!r(p)){const n=e.locators.find(e=>"linkText"===e.name);E(t,"linkText",p,u)&&(f.push("linkText"),o(n,{name:"linkText",type:"static",isRecorded:"Y",value:p}))}const g=Q(u);if(g&&!m("name",g)&&!r(g)){const n=e.locators.find(e=>"name"===e.name);E(t,"name",g,u)&&(f.push("name"),o(n,{name:"name",type:"static",isRecorded:"Y",value:g}))}const $=Z(u);if($&&""!==$.trim()&&!$.includes(" ")&&!m("className",$)&&!r($)){const n=e.locators.find(e=>"className"===e.name);E(t,"className",$,u)&&(f.push("className"),o(n,{name:"className",type:"static",isRecorded:"Y",value:$}))}V(u,"single").forEach(e=>{e.value&&!m("cssSelector",e.value)&&o(void 0,{name:"cssSelector",value:e.value,type:"static",isRecorded:"Y"})});const y=Array.from(u.attributes).filter(e=>!f.includes(e.name));let N=[];try{N=_(u,t,!1,!0,y)??[]}catch(e){console.error("Error generating XPath candidates:",e)}if(0!==N?.length){const e=n.filter(e=>!W(e.value,t,u));let a=0;for(const t of e){if(a>=e.length)break;const n=v(t.value);if(i.has(n))continue;const l=N.find(e=>e.value&&v(e.value)===n);l?.value&&(o(t,{name:"xpath",value:l.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),i.add(n),a++)}if(a<e.length)for(const t of N){if(a>=e.length)break;if(!t.value)continue;const n=v(t.value);i.has(n)||(o(t,{name:"xpath",value:t.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),i.add(n),a++)}}for(const n of e.locators)try{for(const a of e.locators)if(a.value)for(const e of ee)if(a.value.includes(e)){const l=ne(a.value,e,t);if(l){o(a,{name:"xpath",value:l,isRecorded:""!==n.isRecorded&&null!==n.isRecorded?n.isRecorded:"Y"});break}}}catch(e){console.error("Error processing locator:",n,e)}if(l.length<5){const e=[{name:"id",value:J(u)},{name:"name",value:Q(u)},{name:"className",value:Z(u)},{name:"tagName",value:u.tagName},{name:"linkText",value:K(u)}];for(const n of e){if(l.length>4)break;const{name:e,value:a}=n;a&&(r(a)||m(e,a)||"className"===e&&a.includes(" ")||E(t,e,a,u)&&o(void 0,{name:e,type:"static",value:a,isRecorded:"Y",isSelfHealed:"Y"}))}l.length<5&&V(u,"multiple").forEach(e=>{l.length>4||e.value&&(m("cssSelector",e.value)||E(t,"cssSelector",e.value,u)&&o(void 0,{name:"cssSelector",type:"static",value:e.value,isRecorded:"Y",isSelfHealed:"Y"}))})}const x=l.map(e=>({...e,value:c(e.value,e.name,e.isRecorded)})),b=[{name:`${e.name}`,desc:`${e.desc}`,type:`${e.type}`,locators:x.filter(e=>null!=e?.value&&""!==e.value),isShared:`${e.isShared}`,projectId:`${e.projectId}`,projectType:`${e.projectType}`,isRecorded:`${e.isRecorded}`,folder:`${e.folder}`,parentId:`${e.parentId}`,parentName:`${e.parentName}`,platform:`${e.platform}`,licenseId:`${e.licenseId}`,licenseType:`${e.licenseType}`,userId:`${e.userId}`}];return b}}catch(e){console.error("Error processing locator:",n,e);continue}}catch(e){console.error("Error processing locator:",n,e);continue}return null};async function le(){return{fromHTML:async n=>{const a=new e,l=new t(n.replace(/\\"/g,'"').replace(/\\r/g,"\r").replace(/\\n/g,"\n").replace(/\\t/g,"\t"),{pretendToBeVisual:!0,runScripts:"dangerously",resources:"usable",virtualConsole:a}),{window:r}=l;global.document=r.document,global.Node=r.Node,global.Element=r.Element,global.HTMLElement=r.HTMLElement,global.SVGElement=r.SVGElement;const o=r.CSS??{};return o.escape||(o.escape=e=>String(e).replace(/[^a-zA-Z0-9_-]/g,"\\$&")),global.CSS=o,global.XPathResult=r.XPathResult,global.XPathEvaluator=r.XPathEvaluator,{autoHeal:async e=>ae(e,r.document)}}}}export{le as default};
|
|
1
|
+
import{VirtualConsole as e,JSDOM as t}from"jsdom";const n=/^[\S]+( [\S]+)*$/i;let a=new WeakMap;const r=(e,t)=>{const{owner:n,contextNode:r}=(e=>{const t=9===e.nodeType?e:e.ownerDocument||document;return{owner:t,contextNode:1===e.nodeType||9===e.nodeType||11===e.nodeType?e:t}})(t);let o=a.get(r);o||(o=new Map,a.set(r,o));const l=o.get(e);if(l)return l;const s=n.evaluate(e,r,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null),i={first:s.iterateNext(),second:s.iterateNext()};return o.set(e,i),i};let o=[];const l=new Set(["marked-element-temp","removePointers"]),s=(e,t)=>{return t?"class"===e||"className"===e?(n=t,(n||"").split(/\s+/).map(e=>e.trim()).filter(e=>e&&!l.has(e)).sort()).join(" "):t.replace("removePointers","").trim():"";var n},i=e=>/\d/.test(e),c=(e,t,n)=>t?`//*[local-name()='${n}' and ${e}]`:`//${n}[${e}]`,u=e=>{const t=e?.textContent;return t},f=(e,t,n,a=!1)=>{try{const{first:o,second:l}=r(e,n);if(!o)return 0;if(!l)return o===t?1:0;if(a&&Array.isArray(t)){let a=0;if(t.includes(o)&&a++,t.includes(l)&&a++,2===a)return 2;const r=(9===n.nodeType?n:n.ownerDocument).evaluate(e,n,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);let s;for(;s=r.iterateNext();)if(t.includes(s)&&(a++,2===a))return 2;return 0}return 2}catch(t){return console.error(`Error evaluating XPath: ${e}`,t),0}},m=e=>{if(e){if(-1!==e.indexOf('"'))return`'${e}'`;if(-1!==e.indexOf("'"))return`"${e}"`}return`'${e}'`},d=e=>e?e.replace(/\s\s+/g," ").trim():e,h=(e,t)=>{const n=e.ownerDocument.implementation.createHTMLDocument("shadow-xpath"),a=n.createElement("div");a.innerHTML=e.innerHTML,n.body.appendChild(a);const r=t=>{if(!(t&&t instanceof e.ownerDocument.defaultView.Element))return null;const n=((e,t)=>{const n=[];let a=t;for(;a&&a.parentElement;){const e=a.parentElement;n.unshift(Array.from(e.children).indexOf(a)),a=e}return a&&a.parentNode===e?(n.unshift(Array.from(e.children).indexOf(a)),n):null})(e,t);return n?((e,t)=>{let n=e;for(const e of t){const t=n.children.item(e);if(!t)return null;n=t}return n})(a,n):null},o=Array.isArray(t)?t.map(e=>r(e)).filter(Boolean):[],l=Array.isArray(t)?null:r(null);return{owner:n,contextNode:a,cloneElement:l,cloneElements:o}},g=(e,t,n)=>{const a=s(e.name,e.value);if(!a||"boolean"==typeof e?.value)return!1;if(["true","false","on","off","flntooltip","flutter-highlight-overlay"].some(e=>e===a))return!1;if(["style","locator-data-tooltip","value"].some(t=>t===e.name))return!1;const r=o?.find(n=>n.doc===t.ownerDocument&&n.element===t&&n.attributeName===e.name);return!r&&(!(0===e?.name?.indexOf("on")&&e?.name?.length>3)&&("function"!=typeof e.value&&(!n||!i(a))))},p=e=>e instanceof SVGElement,$=(e,t,a,r,o,l)=>{if(r){const{tagName:s}=e;let u,h="";const g=[];let $;if(r&&(!l||!i(r))&&($=n.test(r)?`//${s}[${a}=${m(r)}]`:c(`normalize-space(${a})=${m(d(r)).trim()}`,p(e),e.tagName.toLowerCase()),u=f($,e,t),1===u&&!o))return $;if(r&&l){const l=r.split(" ");if(l?.length)if(1===l.length){const i=[...new Set(l[0].match(/([^0-9]+)/g))];if(i?.length>=1&&i[0]&&d(i[0].trim())?.length>1&&r.startsWith(i[0])&&(h=n.test(i[0])?`starts-with(${a},${m(i[0]).trim()})`:`starts-with(${a},${m(d(i[0])).trim()})`),i?.length>1&&i[i.length-1]&&d(i[i.length-1].trim())?.length>1&&r.endsWith(i[i.length-1])&&(h=n.test(i[i.length-1])?`ends-with(${a},${m(i[i.length-1]).trim()})`:`ends-with(${a},${m(d(i[i.length-1])).trim()})`),h?.length&&($=p(e)?`//*[local-name()='${s}' and ${h}]`:`//${s}[${h}]`,u=f($,e,t),1===u&&!o))return $}else{const i=l.length%2==0?l.length/2:l.length%2,c=l.slice(0,i).join(" ");let g=[...new Set(c.match(/([^0-9]+)/g))];if(g?.length&&g[0]&&d(g[0].trim())?.length&&r.startsWith(g[0])&&(h=n.test(g[0])?`starts-with(${a},${m(g[0]).trim()})`:`starts-with(${a},${m(d(g[0])).trim()})`),h?.length&&($=p(e)?`//*[local-name()='${s}' and ${h}]`:`//${s}[${h}]`,u=f($,e,t),1===u&&!o))return $;const y=l.slice(i,l.length-1).join(" ");if(g=[...new Set(y.match(/([^0-9]+)/g))],g?.length&&g[0]&&d(g[0].trim())?.length>3&&r.endsWith(g[0])&&(h=n.test(g[0])?`ends-with(${a},${m(g[0]).trim()})`:`ends-with(${a},${m(d(g[0])).trim()})`),h?.length&&($=p(e)?`//*[local-name()='${s}' and ${h}]`:`//${s}[${h}]`,u=f($,e,t),1===u&&!o))return $}}if(r&&l&&i(r)){const l=[...new Set(r.match(/([^0-9]+)/g))];if(l?.length)for(let e=0;e<l?.length;e++)l[e]&&d(l[e].trim())?.length>1&&(n.test(l[e])?g.push(`contains(${a},${m(l[e].trim()).trim()})`):g.push(`contains(${a},${m(d(l[e])).trim()})`));if(g?.length&&($=p(e)?`//*[local-name()='${s}' and ${g.join(" and ")}]`:`//${s}[${g.join(" and ")}]`,u=f($,e,t),1===u&&!o))return $}if($=p(e)?`//*[local-name()='${s}' and text()]`:`//${s}[text()]`,u=f($,e,t),1===u&&!o)return $}},y=(e,t,a,r)=>{try{const o=a.filter(t=>g(t,e,r)),l=a=>{const r=s(a.name,a.nodeValue);if(!r)return[];if("class"===a.name){const n=r.split(/\s+/).map(e=>e.trim()).filter(Boolean);if(!n.length)return[];const a=n.map(n=>{const a=c(`contains(@class,${m(n)})`,p(e),e.tagName.toLowerCase());return{token:n,count:w(a,e,t)}}).sort((e,t)=>e.count!==t.count?e.count-t.count:t.token.length-e.token.length);return a[0]?.token?[`contains(@class,${m(a[0].token)})`]:[]}return n.test(r)?[`@${a.name}="${r}"`]:[`normalize-space(@${a.name})="${r}"`]},i=()=>{const t=d(u(e)?.trim()||"");if(!t||t.length>80||/^\d+$/.test(t))return[];const a=new Set;return n.test(t)&&t.length<=40&&a.add(`text()=${m(t)}`),t.includes(" ")&&a.add(`normalize-space(text())=${m(d(t))}`),a.add(`contains(text(),${m(t)})`),Array.from(a)},h=(e,t)=>{const n=[],a=(r,o)=>{if(o.length!==t)for(let t=r;t<e.length;t++)o.push(e[t]),a(t+1,o),o.pop();else n.push([...o])};return a(0,[]),n},$=t=>p(e)?`//*[local-name()='${e.tagName}' and ${t.join(" and ")}]`:`//${e.tagName}[${t.join(" and ")}]`,y=(()=>{if(!(o.length<2))for(let n=2;n<=o.length;n++){const a=h(o,n);for(const n of a){let a=[];for(const e of n){const t=l(e);t.length&&a.push(...t)}if(a.length<2)continue;const r=$(a);let o;try{o=f(r,e,t)}catch{continue}if(1===o)return r}}})();if(y)return y;const v=i();if(!v.length||!o.length)return;for(let n=1;n<=o.length;n++){const a=h(o,n);for(const n of a){let a=[];for(const e of n)a.push(...l(e));if(a.length)for(const n of v){const r=[...a,n],o=$(r);try{if(1===f(o,e,t))return o}catch{continue}}}}}catch(e){console.log(`XPath generation error: ${JSON.stringify(e,null,2)}`)}},v=(e,t)=>{if(!e.textContent)return"";const a=(r=e,r?.childNodes[0]?.nodeValue||"");var r;let o;return o=n.test(a)?p(e)?`//*[local-name()='${e.tagName}' and .=${m(a)}]`:`//${e.tagName||"*"}[.=${m(a)}]`:p(e)?`//*[local-name()='${e.tagName}' and normalize-space(.)=${m(a)}]`:`//${e.tagName||"*"}[normalize-space(.)=${m(a)}]`,o},N=(e,t,n)=>c(`normalize-space(${t})=${m(d(n)).trim()}`,p(e),e.tagName.toLowerCase()),E=(e,t,n)=>c(`starts-with(${t},${m(d(n)).trim()})`,p(e),e.tagName.toLowerCase()),x=(e,t,n)=>c(`contains(${t},${m(d(n)).trim()})`,p(e),e.tagName.toLowerCase()),S=(e,t)=>{const a=t.map(t=>(t=>{const a=s(t.name,t.value);if(!a)return null;if("class"===t.name){const t=a.split(/\s+/).map(e=>e.trim()).filter(Boolean),n=e.getRootNode?.()??e.ownerDocument,r=t.map(t=>{const a=c(`contains(@class,${m(t)})`,p(e),e.tagName.toLowerCase());return{token:t,count:w(a,e,n)}}).sort((e,t)=>e.count!==t.count?e.count-t.count:t.token.length-e.token.length)[0]?.token;return r?`contains(@class,${m(r)})`:null}return n.test(a)?`@${t.name}=${m(a)}`:`normalize-space(@${t.name})=${m(a)}`})(t)).filter(Boolean);if(a.length>=2)return c(`${a[0]} or ${a[1]}`,p(e),e.tagName.toLowerCase());const r=(()=>{const t=d(u(e)?.trim()||"");return!t||t.length>80||/^\d+$/.test(t)?null:n.test(t)&&t.length<=40?`text()=${m(t)}`:t.includes(" ")?`normalize-space(text())=${m(t)}`:`contains(text(),${m(t)})`})();return 1===a.length&&r?c(`${a[0]} or ${r}`,p(e),e.tagName.toLowerCase()):""},w=(e,t,n)=>{try{return f(e,t,n)}catch(e){return 0}},b=(e,t)=>{const n=[],a=new Set,r=Array.from(e.attributes||[]),o=["id","data-testid","data-test","data-qa","name","aria-label","role"],l=[...o.map(e=>r.find(t=>t.name===e)).filter(Boolean),...r.filter(e=>!o.includes(e.name))],i=r=>{r&&!a.has(r)&&C(r,e,t)&&(a.add(r),n.push(r))};l.forEach(t=>{if(!g(t,e,!1))return;const n="id"===t.name?`//*[@id=${m(s(t.name,t.value))}]`:D(e,t.name,t.value);n&&i(n)});const c=e.textContent?.trim();if(c&&c.length<40&&0===e.children.length){const t=v(e);t&&i(t)}if(r.length>1){const n=y(e,t,r,!1);n&&i(n)}return n},k=(e,t)=>{const n=[];let a=t;for(;a&&a!==e;)n.unshift(L(a)),a=a.parentElement;return a===e?n.join("/"):""},R=(e,t)=>{const n=t??(e.getRootNode?.()||e.ownerDocument),a=L(e),r=p(e)?`//*[local-name()='${e.tagName.toLowerCase()}']`:`//${e.tagName.toLowerCase()}`;for(let t=e.parentElement;t;t=t.parentElement){const r=b(t,n);for(const o of r){console.log(`Trying ancestor XPath: ${o}`);const r=`${o}/descendant::${a}`;console.log(`Trying descendant XPath: ${r}`);const l=w(r,e,n);if(console.log(`Match count: ${l}`),1===l&&T(r,n)===e)return console.log(`Selected XPath: ${r}`),r;const s=k(t,e);if(s){const t=`${o}/${s}`;console.log(`Trying descendant XPath: ${t}`);const a=w(t,e,n);if(console.log(`Match count: ${a}`),1===a&&T(t,n)===e)return console.log(`Selected XPath: ${t}`),t}}}console.log(`Trying descendant XPath: ${r}`);const o=w(r,e,n);return console.log(`Match count: ${o}`),1===o&&T(r,n)===e?(console.log(`Selected XPath: ${r}`),r):(console.log("Selected XPath: "),"")},T=(e,t)=>{try{if(n=t,n?.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&"host"in n){const{owner:n,contextNode:a,cloneElement:r}=h(t);return n.evaluate(e,a,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue||r}return t.evaluate(e,t,null,t.defaultView.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}catch(e){return null}var n},C=(e,t,n)=>{try{const{first:a,second:o}=r(e,n);return!!a&&!o&&a===t}catch{return!1}},L=e=>p(e)?`*[local-name()='${e.tagName.toLowerCase()}']`:e.tagName.toLowerCase(),A=(e,t,n)=>{if(!(e instanceof Element))return[];const a=new Set,r=[],o=Array.from(e.attributes||[]).filter(t=>{return t?.value&&(a=t.value,!/\d/.test(a))&&g(t,e,n);var a}),l=(e,t)=>{t&&!a.has(t)&&(a.add(t),r.push({key:e,value:t}))},s=["id","data-testid","data-test","data-qa","name","aria-label","role"];s.forEach(n=>{const a=o.find(e=>e.name===n);if(!a)return;const r="id"===n?`//*[@id='${a.value}']`:D(e,a.name,a.value);r&&C(r,e,t)&&l(`anchor by ${a.name}`,r)}),o.forEach(n=>{if(s.includes(n.name))return;const a=D(e,n.name,n.value);a&&C(a,e,t)&&l(`anchor by ${n.name}`,a)});const i=e.textContent?.trim();if(i&&i.length<40&&0===e.children.length){const n=v(e);n&&C(n,e,t)&&l("anchor by text",n)}if(o.length>1){const a=y(e,t,o,n);a&&C(a,e,t)&&l("anchor by combination",a)}const c=R(e,t);return c&&C(c,e,t)&&l("anchor by tag",c),r},D=(e,t,n)=>{const a=new RegExp(/^[\S]+( [\S]+)*$/gi);let r="";return(n=s(t,n))&&(r=a.test(n)?"class"===t?p(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${m(n)})]`:`//${e.tagName||"*"}[contains(@${t},${m(n)})]`:p(e)?`//*[local-name()='${e.tagName}' and @${t}=${m(n)}]`:`//${e.tagName||"*"}[@${t}=${m(n)}]`:p(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${m(n)})]`:`//${e.tagName||"*"}[contains(@${t},${m(n)})]`),r},P=e=>e=(e=e.replace(/text\(\)\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1")).replace(/\.\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1");function O(e){const t=function(e){return e.toLowerCase().replace(/'[^']*'/g,"?").replace(/"[^"]*"/g,"?").replace(/\[\d+\]/g,"[?]").replace(/\/\/[a-z0-9_-]+/g,"//node").replace(/\/[a-z0-9_-]+/g,"/node")}(e),n=function(e){const t=e.toLowerCase(),n=t.match(/(ancestor-or-self|ancestor|descendant-or-self|descendant|following-sibling|preceding-sibling|following|preceding|parent|child|self)::/),a=n?.[1]??"none",r=t.match(/@([a-z0-9:-]+)/);return{axis:a,attribute:r?.[1]??"none",usesNormalize:t.includes("normalize-space")}}(e);return["XPATH",`axis:${n.axis}`,`attr:${n.attribute}`,`normalize:${n.usesNormalize}`,`shape:${t}`].join("|")}function _(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/ /g,"\\ ")}function Y(e,t,n,a){const r=a?.getRootNode?.()??e,o=e=>{try{return Array.from(r.querySelectorAll(e))}catch{return[]}};try{switch(t){case"id":return 1===o(`#${_(n)}`).length;case"name":return 1===o(`[name="${_(n)}"]`).length;case"className":return 1===o(`.${n}`).length;case"tagName":case"cssSelector":return 1===o(n).length;case"linkText":return 1===o("a").filter(e=>e.textContent?.trim()===n).length;case"partialLinkText":return 1===o("a").filter(e=>e.textContent?.includes(n)).length;default:return!1}}catch{return!1}}let X=[];const z={andConditions:"and",orConditions:"or",contains:"contains",startsWith:"startsWith",normalizeSpace:"normalizeSpace",axes:"axes",index:"index",hardCodedText:"text",tillTag:"tag"},H=(e,t)=>t.map(t=>({...t,key:t?.key?.includes(e)?t.key:`${e} ${t.key}`.trim()})),M=e=>e?.textContent?.replace(/\s+/g," ")?.trim()||"",V=e=>{if(!(e&&e instanceof Element))return[];const t=[],n=Array.from(e.children||[]);for(;n.length;){const e=n.shift();e.classList?.contains("flntooltip")||(t.push(e),n.push(...Array.from(e.children||[])))}return t},q=(e,t)=>{const n=[];let a="previous"===t?e.previousElementSibling:e.nextElementSibling;for(;a;)a.classList?.contains("flntooltip")||n.push(a),a="previous"===t?a.previousElementSibling:a.nextElementSibling;return n},I=(e,t)=>{const n=[];for(let a=e;a?.parentElement;a=a.parentElement){let e="previous"===t?a.previousElementSibling:a.nextElementSibling;for(;e;)e.classList?.contains("flntooltip")||(n.push(e),n.push({node:e,expand:()=>V(e)})),e="previous"===t?e.previousElementSibling:e.nextElementSibling}return n},j=e=>{if(!(e instanceof Element))return!0;const t=e.textContent?.trim();return!e.id&&!e.className&&0===e.attributes.length&&(!t||t.length>60)&&e.children.length>2},W=(e,t,n,a,r,o,l)=>{const s=L(n),i=[];let c=0;const u=t.includes("descendant")?10:20;for(const m of e){if(c++>u)break;if(i.length>=5)break;const e="node"in m?m.node:m;if(!(e instanceof Element))continue;if(j(e))continue;const d=[()=>A(e,a,r),()=>{const t=[],n=Array.from(e.attributes||[]).filter(t=>t?.value&&!/\d/.test(t.value)&&g(t,e,r));for(const a of n){const n=a.value;if(!n)continue;const r=x(e,`@${a.name}`,n);r&&t.push({key:"contains",value:r});const o=E(e,`@${a.name}`,n);o&&t.push({key:"starts-with",value:o})}return t},()=>[{key:"tag",value:R(e,a)}]];for(let r=0;r<d.length;r++){let c=d[r]();if(!c.length)continue;l||(c=c.filter(e=>!/\[\d+\]/.test(e.value)));const u=new Set;c=c.filter(e=>!u.has(e.value)&&(u.add(e.value),!0)),c=c.slice(0,5);for(const e of c){const r=`${e.value}/${t}::${s}`;if(C(r,n,a)){if(i.push({key:o,value:r}),i.length>=2)return i;if(i.length>=5)return i}}if(l&&r===d.length-1)for(const r of c){const l=f(r.value,e,a);for(let e=1;e<=Math.min(l,3);e++){const l=`(${r.value})[${e}]/${t}::${s}`;if(C(l,n,a)){if(i.push({key:o,value:l}),i.length>=2)return i;if(i.length>=5)return i}}}}if("expand"in m&&m.expand){const e=m.expand();let l=0;for(const c of e){if(l++>15)break;if(i.length>=5)break;if(!(c instanceof Element))continue;if(j(c))continue;const e=A(c,a,r);for(const r of e.slice(0,5)){const e=`${r.value}/${t}::${s}`;if(C(e,n,a)){if(i.push({key:o,value:e}),i.length>=2)return i;if(i.length>=5)return i}}}}}return i},F=(e,t,n,a)=>{const r=[];for(let t=e.parentElement;t;t=t.parentElement)r.push(t);const o=V(e),l=q(e,"previous"),s=q(e,"next"),i=I(e,"previous"),c=I(e,"next"),u=(e,t=20)=>e.length>t?e.slice(0,t):e,f=[[{nodes:u(s),axis:"preceding-sibling",key:"preceding-sibling"},{nodes:u(l),axis:"following-sibling",key:"following-sibling"}],[{nodes:u(r),axis:"child",key:"child"},{nodes:u(o,15),axis:"parent",key:"parent"}],[{nodes:u(r),axis:"descendant",key:"descendant"},{nodes:u(r),axis:"descendant-or-self",key:"descendant-or-self"},{nodes:u(o,10),axis:"ancestor",key:"ancestor"},{nodes:u(o,10),axis:"ancestor-or-self",key:"ancestor-or-self"}],[{nodes:u(i),axis:"following",key:"following"},{nodes:u(c),axis:"preceding",key:"preceding"}]];for(const r of f)for(const o of r){const r=W(o.nodes,o.axis,e,t,n,o.key,a);if(r.length>0)return r}return[]},B=(e,t,n,a,r)=>{const o=(e=>z[e]||e)(a);return"text"===o?H(a,((e,t,n)=>{if(!M(e))return[];if("text"===n){const n=v(e)||G(e,t,!1,!1);return n?[{key:"xpath by text",value:n}]:[]}return[]})(e,t,o)):["contains","startsWith","normalizeSpace"].includes(o)?H(a,((e,t,n)=>{const a=[],r=Array.from(e.attributes||[]).filter(n=>n?.value&&g(n,e,t)),o=M(e),l="contains"===n?x:"startsWith"===n?E:"normalizeSpace"===n?N:null;if(!l)return a;if(r.forEach(t=>{const r=l(e,`@${t.name}`,t.value);r&&a.push({key:`xpath by ${n} ${t.name}`,value:r})}),o){const t=l(e,".",o);t&&a.push({key:`xpath by ${n} text`,value:t})}return a})(e,n,o)):["and","or"].includes(o)?H(a,((e,t,n,a)=>{const r=Array.from(e.attributes||[]).filter(t=>t?.value&&g(t,e,n));if("and"===a){const a=y(e,t,r,n);return a?[{key:"xpath by combination",value:a}]:[]}if("or"===a&&r.length>=1){const t=S(e,r);return t?[{key:"xpath by or",value:t}]:[]}return[]})(e,t,n,o)):"axes"===o?H(a,F(e,t,n,!1)):"index"===o?H(a,((e,t)=>{const n=[],a=e.tagName.toLowerCase(),r=Array.from(t.querySelectorAll(a)).indexOf(e)+1;if(r<=0)return[];n.push({key:"xpath by index",value:`(//${a})[${r}]`});let o=e.parentElement;for(;o&&o!==t.body;){if(o.id||o.className){const t=o.className?.toString().split(" ").map(e=>e.trim()).find(Boolean),r=o.id?`//${o.tagName.toLowerCase()}[@id='${o.id}']`:t?`//${o.tagName.toLowerCase()}[contains(@class,'${t}')]`:"";if(!r){o=o.parentElement;continue}const l=Array.from(o.querySelectorAll(a)).indexOf(e)+1;if(l>0){n.push({key:"xpath by index",value:`(${r}//${a})[${l}]`});break}}o=o.parentElement}return n})(e,t)):"tag"===o?H(a,[{key:"xpath till tag",value:R(e,t)}]):[]};const G=(e,t,n,a)=>{if(""!=(e.textContent??"").trim()){const r=u(e);if(r)return $(e,t,".",r,n,a)}},U=(e,t,n,a,r)=>{const o=e;try{o.map(e=>{if("className"!==e.name&&"class"!==e.name&&g(e,t,r)){const o=((e,t,n,a,r)=>{let o;return o=e.name,$(t,n,`@${o}`,e.value,a,r)})(e,t,n,a,r);o&&X.push({key:`xpath by ${e.name}${a?" index":""}`,value:o})}});const l=G(t,n,a,r);if(l&&X.push({key:"xpath by text"+(a?" index":""),value:l}),o.find(e=>"className"===e.name)&&g(o?.find(e=>"className"===e.name),t,r)){let e=((e,t,n,a)=>{let r=e.className;if("string"!=typeof r&&(r=""),r=r?.replace("flndisabled","disabled"),r=s("class",r),r=r?.trim(),r)return $(e,t,"@class",r,n,a)})(t,n,a,r);e&&X.push({key:"xpath by class",value:e})}if(!X.length&&o.length>1){const e=y(t,n,o,r);e&&X.push({key:"xpath by combination",value:e})}if(!X.length){const a=function(e,t,n,a){const{tagName:r}=t,o=t.textContent.trim();if(o)for(const l of e)if(g(l,t,a)){let e=s(l.name,l.nodeValue);const a=`//${r}[@${l.name}='${e}' and text()='${o}']`;if(a&&1==f(a,t,n))return a}}(e,t,n,r);a&&X.push({key:"xpath by textAttribute",value:a})}}catch(e){console.log(e)}},J=(e,t,n,a,r=[],o=[])=>{X=[],console.log(e);const l=e,s=l?.getRootNode?.(),i=s?.nodeType===Node.DOCUMENT_FRAGMENT_NODE,c=i?s:l?.ownerDocument||t,u=l.tagName,{attributes:m}=l,d=r.length>0?r:Array.from(m);if(U(d,l,c,n,a),o.length){const e=o.flatMap(e=>B(l,c,a,e));return X=(e=>{const t=new Set;return e.filter(e=>!(!e?.value||t.has(e.value)||(t.add(e.value),0)))})(e.filter(e=>1===f(e.value,l,c))),X}if(X.length){const t=X.length;for(let n=0;n<t;n++){let t=X[n].value;t="//*"+t.substring(t.indexOf("//")+2+u.length);1===f(t,e,c)&&X.push({key:`${X[n].key} regex`,value:t})}}return X.length||i||(X=F(l,c,a,n)),X};let Z=[];const K=(e,t,n)=>{try{const a=n.querySelectorAll(e);return 1===a.length&&a[0]===t}catch{return!1}},Q=(e,t="single")=>{const n=[],a=e.getRootNode();try{const r=ee(e);if(r&&K(r,e,a)&&(n.push({key:"cssSelector by id",value:r}),"single"===t))return n;const o=re(e);o&&K(o,e,a)&&n.push({key:"cssSelector by class",value:o});const l=ae(e);l&&n.push({key:"cssSelector by name",value:l})}catch(e){console.error(e)}return n},ee=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if(e.id&&!i(e.id)){t+=`#${CSS.escape(e.id)}`,a.unshift(t);break}{let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}a.unshift(t),e=e.parentElement}return a.join(" > ")},te=new Set(["id","class","style"]);function ne(e){return Array.from(e.attributes).filter(e=>!te.has(e?.name?.toLowerCase())&&e.value&&!i(e.value)).map(e=>{return`[${e.name}="${t=e.value,String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"]`;var t})}const ae=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.getRootNode(),a=e.tagName.toLowerCase();if("style"===a||"script"===a)return;const r=ne(e);for(const t of r){const r=`${a}${t}`;if(K(r,e,n))return r}},re=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if("string"!=typeof e.className||!e.className||i(e.className)||Z?.find(t=>t.element===e&&"class"===t.attributeName)){let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}else if(e.classList.remove("marked-element-temp"),e.classList.remove("removePointers"),e.className){t+=`.${e.className.trim().replace(/\s+/g,".")}`,a.unshift(t);break}a.unshift(t),e=e.parentElement}return a.join(" > ")},oe=(e,t,n)=>{const a=P(e);if(ge(t,a)===n)return!0;if(function(e){return/\[(?:\s*\.|\s*contains\s*\(\s*\.|\s*normalize-space\s*\(\s*\.)/.test(e)}(a)){const e=t.evaluate(a,t,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);for(let t=0;t<e.snapshotLength;t++)if(e.snapshotItem(t)===n)return!0}return!1},le=(e,t)=>{try{const n=e.querySelector(t);if(n)return n}catch(e){return console.error("Invalid CSS selector:",t,e),null}return ce(e.body,t)},se=(e,t,n)=>le(t,e)===n,ie=e=>e.name?.toLowerCase().includes("cssselector")??!1,ce=(e,t)=>{const n=Array.from(e.querySelectorAll("*"));try{for(let e=0;e<n.length;e++)if(n[e].shadowRoot){const{shadowRoot:a}=n[e];if(a){const e=ce(a,t);if(e)return e;if(a&&!t.includes("dynamic"))return a.querySelector(t)}}}catch(e){console.log(e)}return null},ue=e=>e?.id||null,fe=e=>e.className||null,me=e=>e.textContent?.trim()||null,de=e=>{const t=e;if(t.hasAttribute("name")){return`${t.getAttribute("name")}`||null}return null},he=["/preceding-sibling","/following-sibling","/parent","/descendant","/ancestor","/self","/ancestor-or-self","/child","/preceding","/following"];function ge(e,t){const n=e.defaultView;if(!n)return null;return(new n.XPathEvaluator).evaluate(t,e,null,n.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}function pe(e,t,n){if(e.includes(t)){const a=e.split(t)[0].trim(),r=n.defaultView;if(!r)return null;if(!e.includes("dynamic")){const t=new r.XPathEvaluator;if(t.evaluate(a,n,null,r.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue){let a;return t.evaluate(e,n,null,r.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue?(a=e,a):(console.error("Complete Locator is Invalid:",e),a=e,a)}console.error("Source Locator Not Found:",a)}}return null}const $e=(e,t)=>{const n=t.querySelectorAll("script, style, link[rel='stylesheet'], meta, noscript, embed, object, param, source, svg");n&&n.forEach(e=>{e.remove()});const a=new Set;let r=[];function o(e,t={}){const n=e?.value,o=t.value??e?.value,l={name:t.name??e?.name,type:t.type??e?.type,value:t.value??e?.value,reference:t.reference??e?.reference,status:t.status??e?.status,isRecorded:t.isRecorded??e?.isRecorded},s="Y"===e?.isSelfHealed;l.isSelfHealed=s?"Y":t.hasOwnProperty("isSelfHealed")?t.isSelfHealed:((e,t,n)=>t&&n&&t===n?null:"Y")(l.name,n,o),function(e){const t=`${e.name}:${e.value}`;a.has(t)||(a.add(t),r.push(e))}(l)}function l(e,t,n){if(ie(t))return le(e,n);if(t.name.includes("id")||n.startsWith("#"))return e.querySelector("#"+_(n));if(t.name.includes("className")||n.startsWith("."))return e.querySelector("."+n);if("name"===t.name){const t=_(n);return e.querySelector(`[name="${t}"]`)}if("tagName"===t.name)return e.querySelector(n);if("linkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.trim()===n)||null;if("partialLinkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.includes(n))||null;if(!t.name.includes("xpath")&&!n.startsWith("//")||t.type.match("dynamic"))return e.querySelector(n);{const a=ge(e,P(n));return a&&o(t,{value:n,isRecorded:String(t.isRecorded).includes("N")?"N":"Y"}),a}}function s(e,t,n){const a=e.querySelectorAll("iframe");for(const e of a)try{const a=e.contentDocument||e.contentWindow?.document;if(!a)continue;const r=l(a,t,n);if(r)return r}catch{continue}return null}const c=(e,t,n)=>{if(!e)return null;let a=e.trim();return a&&"null"!==a.toLowerCase()?(a=a.replace(/\\"/g,'"').replace(/\\'/g,"'"),a=a.replace(/^['"](.+?)['"]$/,"$1"),a=a.replace(/''/g,"'"),a=a.replace(/\[@(id|name)=['"]{2}(.+?)['"]{2}\]/g,"[@$1='$2']"),"id"!==t&&"name"!==t||(a=a.replace(/['"]/g,"").trim()),"xpath"!==t||"Y"!==n||e.startsWith("//")?!a||/^['"]{2}$/.test(a)?null:a:null):null};e:for(const n of e.locators)try{String(n.isRecorded||"");const u=e.locators.filter(e=>"N"===e.isRecorded);if(u.length>0)for(const e of u)o(e);const f=String(n.value||n.type||"");if(f.includes("dynamic")||f.match("dynamic")||f.includes("{")||f.includes("}")){o(n);continue}if(e.isShared.includes("Y"))break e;try{let u=null;const f=n.value.split(">>>");for(const e of f){if(!t){console.error("Element not found at:",e);break}const a=e.trim();if(u=l(t,n,a),u||(u=s(t,n,a)),u||(u=ce(t.body,a)),!u){console.error("Element not found at:",a);break}}const m=(e,t)=>{const n=`${e}:${t}`;return a.has(n)};if(u){const n=e.locators.filter(e=>"xpath"===e.name&&e.value),a=e.locators.filter(e=>ie(e)&&e.value);for(const e of n)oe(e.value,t,u)&&o(e,{isSelfHealed:null});for(const e of a)se(e.value,t,u)&&o(e,{isSelfHealed:null});const l=r.filter(e=>"xpath"===e.name&&e.value),s=new Set(l.map(e=>O(e.value))),f=[],d=ue(u);if(d&&!m("id",d)&&!i(d)){const n=e.locators.find(e=>"id"===e.name);Y(t,"id",d,u)&&(f.push("id"),o(n,{name:"id",type:"static",isRecorded:"Y",value:d}))}const h=u.tagName;if(h&&!m("tagName",h)){const n=e.locators.find(e=>"tagName"===e.name);Y(t,"tagName",h,u)&&(f.push("tagName"),o(n,{name:"tagName",type:"static",isRecorded:"Y",value:h}))}const g=me(u);if(g&&!i(g)){const n=e.locators.find(e=>"linkText"===e.name);Y(t,"linkText",g,u)&&(f.push("linkText"),o(n,{name:"linkText",type:"static",isRecorded:"Y",value:g}))}const p=de(u);if(p&&!m("name",p)&&!i(p)){const n=e.locators.find(e=>"name"===e.name);Y(t,"name",p,u)&&(f.push("name"),o(n,{name:"name",type:"static",isRecorded:"Y",value:p}))}const $=fe(u);if($&&""!==$.trim()&&!$.includes(" ")&&!m("className",$)&&!i($)){const n=e.locators.find(e=>"className"===e.name);Y(t,"className",$,u)&&(f.push("className"),o(n,{name:"className",type:"static",isRecorded:"Y",value:$}))}Q(u,"single").forEach(e=>{e.value&&!m("cssSelector",e.value)&&o(void 0,{name:"cssSelector",value:e.value,type:"static",isRecorded:"Y"})});const y=Array.from(u.attributes).filter(e=>!f.includes(e.name));let v=[];try{v=J(u,t,!1,!0,y)??[]}catch(e){console.error("Error generating XPath candidates:",e)}if(0!==v?.length){const e=n.filter(e=>!oe(e.value,t,u));let a=0;for(const t of e){if(a>=e.length)break;const n=O(t.value);if(s.has(n))continue;const r=v.find(e=>e.value&&O(e.value)===n);r?.value&&(o(t,{name:"xpath",value:r.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),a++)}if(a<e.length)for(const t of v){if(a>=e.length)break;if(!t.value)continue;const n=O(t.value);s.has(n)||(o(t,{name:"xpath",value:t.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),a++)}}for(const n of e.locators)try{for(const a of e.locators)if(a.value)for(const e of he)if(a.value.includes(e)){const r=pe(a.value,e,t);if(r){o(a,{name:"xpath",value:r,isRecorded:""!==n.isRecorded&&null!==n.isRecorded?n.isRecorded:"Y"});break}}}catch(e){console.error("Error processing locator:",n,e)}if(r.length<5){const e=[{name:"id",value:ue(u)},{name:"name",value:de(u)},{name:"className",value:fe(u)},{name:"tagName",value:u.tagName},{name:"linkText",value:me(u)}];for(const n of e){if(r.length>4)break;const{name:e,value:a}=n;a&&(i(a)||m(e,a)||"className"===e&&a.includes(" ")||Y(t,e,a,u)&&o(void 0,{name:e,type:"static",value:a,isRecorded:"Y",isSelfHealed:"Y"}))}r.length<5&&Q(u,"multiple").forEach(e=>{r.length>4||e.value&&(m("cssSelector",e.value)||Y(t,"cssSelector",e.value,u)&&o(void 0,{name:"cssSelector",type:"static",value:e.value,isRecorded:"Y",isSelfHealed:"Y"}))})}const N=r.map(e=>({...e,value:c(e.value,e.name,e.isRecorded)})),E=[{name:`${e.name}`,desc:`${e.desc}`,type:`${e.type}`,locators:N.filter(e=>null!=e?.value&&""!==e.value),isShared:`${e.isShared}`,projectId:`${e.projectId}`,projectType:`${e.projectType}`,isRecorded:`${e.isRecorded}`,folder:`${e.folder}`,parentId:`${e.parentId}`,parentName:`${e.parentName}`,platform:`${e.platform}`,licenseId:`${e.licenseId}`,licenseType:`${e.licenseType}`,userId:`${e.userId}`}];return E}}catch(e){console.error("Error processing locator:",n,e);continue}}catch(e){console.error("Error processing locator:",n,e);continue}return null};async function ye(){return{fromHTML:async n=>{const a=new e,r=new t(n.replace(/\\"/g,'"').replace(/\\r/g,"\r").replace(/\\n/g,"\n").replace(/\\t/g,"\t"),{pretendToBeVisual:!0,runScripts:"dangerously",resources:"usable",virtualConsole:a}),{window:o}=r;global.document=o.document,global.Node=o.Node,global.Element=o.Element,global.HTMLElement=o.HTMLElement,global.SVGElement=o.SVGElement;const l=o.CSS??{};return l.escape||(l.escape=e=>String(e).replace(/[^a-zA-Z0-9_-]/g,"\\$&")),global.CSS=l,global.XPathResult=o.XPathResult,global.XPathEvaluator=o.XPathEvaluator,{autoHeal:async e=>$e(e,o.document)}}}}export{ye as default};
|
|
2
2
|
//# sourceMappingURL=xpath.mjs.map
|