ff-dom 3.0.0-beta.1 → 3.0.0-beta.3
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 +8 -0
- package/dist/types/browser/utils/xpathHelpers.d.ts +22 -0
- package/dist/types/node/utils/xpathHelpers.d.ts +22 -0
- package/dist/xpath.mjs +1 -1
- package/dist/xpath.mjs.map +1 -1
- package/package.json +1 -1
package/dist/xpath.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
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};
|
|
1
|
+
import{VirtualConsole as e,JSDOM as t}from"jsdom";const n=/^[\S]+( [\S]+)*$/i;let r=new WeakMap;const a=(e,t)=>{const{owner:n,contextNode:a}=(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=r.get(a);o||(o=new Map,r.set(a,o));const l=o.get(e);if(l)return l;const s=n.evaluate(e,a,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=>(e||"").split(/\s+/).map(e=>e.trim()).filter(e=>e&&!l.has(e)).sort(),i=e=>/\d/.test(e||""),c=(e,t,n,r=!1)=>{const a=s(t),o=a.filter(e=>!!r||!i(e));return o.length?1===a.length&&1===o.length?[`@class=${p(o[0])}`]:o.map(t=>{const r=d(`contains(@class,${p(t)})`,b(e),e.tagName.toLowerCase());return{token:t,count:D(r,e,n)}}).sort((e,t)=>e.token.length!==t.token.length?e.token.length-t.token.length:e.count-t.count).map(({token:e})=>`contains(@class,${p(e)})`):[]},u=new Set(["data-testid","data-test-id","data-test","data-cy","testid"]),f=(e,t)=>t?"class"===e||"className"===e?s(t).join(" "):t.replace("removePointers","").trim():"",m=e=>/\d/.test(e),d=(e,t,n)=>t?`//*[local-name()='${n}' and ${e}]`:`//${n}[${e}]`,h=e=>{const t=e?.textContent;return t},g=(e,t,n,r=!1)=>{try{if(N(n)){const{owner:a,contextNode:o,cloneElement:l,cloneElements:s}=x(n,t),i=a.evaluate(e,o,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null),c=i.iterateNext(),u=i.iterateNext();if(!c)return 0;if(!u)return c===l?1:0;if(r&&Array.isArray(t)){let e,t=0;if(s.includes(c)&&t++,s.includes(u)&&t++,2===t)return 2;for(;e=i.iterateNext();)if(s.includes(e)&&(t++,2===t))return 2;return 0}return 2}const{first:o,second:l}=a(e,n);if(!o)return 0;if(!l)return o===t?1:0;if(r&&Array.isArray(t)){let r=0;if(t.includes(o)&&r++,t.includes(l)&&r++,2===r)return 2;const a=(9===n.nodeType?n:n.ownerDocument).evaluate(e,n,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);let s;for(;s=a.iterateNext();)if(t.includes(s)&&(r++,2===r))return 2;return 0}return 2}catch(t){return console.error(`Error evaluating XPath: ${e}`,t),0}},p=e=>{if(e){if(-1!==e.indexOf('"'))return`'${e}'`;if(-1!==e.indexOf("'"))return`"${e}"`}return`'${e}'`},$=e=>e?e.replace(/\s\s+/g," ").trim():e,y=(e,t)=>{const n=$((t||"").trim());if(!e.children.length||n.length<=50)return"";const r=n.split(/\s+/).slice(0,3).join(" ");return r.length>2?`contains(normalize-space(.),${p(r)})`:""},v=e=>{const t=$((e||"").trim());if(!t||!i(t))return{prefix:t,fragment:t};const n=t.split(/[0-9]/)[0].replace(/[₹$€£¥₫₽₩₦₱₲₴₵₡₭₮₺฿៛]+/g,"").trim(),r=n.split(/[^a-zA-Z]+/).map(e=>$(e).trim()).filter(e=>e.length>2&&/[a-zA-Z]/.test(e)).join(" ").trim();return{prefix:n.length>2&&/[a-zA-Z]/.test(n)?n:"",fragment:r.length>2?r:""}},E=e=>v(e).fragment,N=e=>e?.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&"host"in e,x=(e,t)=>{const n=e.ownerDocument.implementation.createHTMLDocument("shadow-xpath"),r=n.createElement("div");r.innerHTML=e.innerHTML,n.body.appendChild(r);const a=t=>{if(!(t&&t instanceof e.ownerDocument.defaultView.Element))return null;const n=((e,t)=>{const n=[];let r=t;for(;r&&r.parentElement;){const e=r.parentElement;n.unshift(Array.from(e.children).indexOf(r)),r=e}return r&&r.parentNode===e?(n.unshift(Array.from(e.children).indexOf(r)),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})(r,n):null},o=Array.isArray(t)?t.map(e=>a(e)).filter(Boolean):[],l=Array.isArray(t)?null:a(t??null);return{owner:n,contextNode:r,cloneElement:l,cloneElements:o}},S=(e,t,n)=>{const r=f(e.name,e.value);if(!r||"boolean"==typeof e?.value)return!1;if(["true","false","on","off","flntooltip","flutter-highlight-overlay"].some(e=>e===r))return!1;if(["style","locator-data-tooltip","value"].some(t=>t===e.name))return!1;const a=o?.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&&(l=e.name,s=r,(!u.has(l.toLowerCase())||!(e=>/(?:^|[-_:])\d{4,}(?:$|[-_:])/.test(e))(s))&&("class"===e.name||!i(r)))));var l,s},b=e=>e instanceof SVGElement,w=(e,t,r,a,o,l)=>{if(a){const{tagName:s}=e;let u,f="";const h=[];let v;const N=r.startsWith("@"),x="."===r||"text()"===r,S=x?".":r,w=x&&/\s/.test(a.trim()),k=x?E(a):a,R=x&&i(a)?k:a,T=x?y(e,R):"";if("@class"===r){for(const n of c(e,a,t))if(v=d(n,b(e),e.tagName.toLowerCase()),u=g(v,e,t),1===u&&!o)return v;return}if(x&&i(a)&&!k)return;if(a&&(!N||!m(a))&&(v=T?d(T,b(e),e.tagName.toLowerCase()):w?d(`normalize-space(${S})=${p($(R)).trim()}`,b(e),e.tagName.toLowerCase()):n.test(a)?`//${s}[${r}=${p(R)}]`:d(`normalize-space(${r})=${p($(R)).trim()}`,b(e),e.tagName.toLowerCase()),u=g(v,e,t),1===u&&!o))return v;if(N&&i(a))return;if(a&&l){const l=a.split(" ");if(l?.length)if(1===l.length){const i=[...new Set(l[0].match(/([^0-9]+)/g))];if(i?.length>=1&&i[0]&&$(i[0].trim())?.length>1&&a.startsWith(i[0])&&(f=n.test(i[0])?`starts-with(${r},${p(i[0]).trim()})`:`starts-with(${r},${p($(i[0])).trim()})`),i?.length>1&&i[i.length-1]&&$(i[i.length-1].trim())?.length>1&&a.endsWith(i[i.length-1])&&(f=n.test(i[i.length-1])?`ends-with(${r},${p(i[i.length-1]).trim()})`:`ends-with(${r},${p($(i[i.length-1])).trim()})`),f?.length&&(v=b(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v}else{const i=l.length%2==0?l.length/2:l.length%2,c=l.slice(0,i).join(" ");let m=[...new Set(c.match(/([^0-9]+)/g))];if(m?.length&&m[0]&&$(m[0].trim())?.length&&a.startsWith(m[0])&&(f=n.test(m[0])?`starts-with(${r},${p(m[0]).trim()})`:`starts-with(${r},${p($(m[0])).trim()})`),f?.length&&(v=b(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v;const d=l.slice(i,l.length-1).join(" ");if(m=[...new Set(d.match(/([^0-9]+)/g))],m?.length&&m[0]&&$(m[0].trim())?.length>3&&a.endsWith(m[0])&&(f=n.test(m[0])?`ends-with(${r},${p(m[0]).trim()})`:`ends-with(${r},${p($(m[0])).trim()})`),f?.length&&(v=b(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v}}if(a&&l&&m(a)){const l=[...new Set(a.match(/([^0-9]+)/g))];if(l?.length)for(let e=0;e<l?.length;e++)l[e]&&$(l[e].trim())?.length>1&&(n.test(l[e])?h.push(`contains(${r},${p(l[e].trim()).trim()})`):h.push(`contains(${r},${p($(l[e])).trim()})`));if(h?.length&&(v=b(e)?`//*[local-name()='${s}' and ${h.join(" and ")}]`:`//${s}[${h.join(" and ")}]`,u=g(v,e,t),1===u&&!o))return v}if(v=b(e)?`//*[local-name()='${s}' and text()]`:`//${s}[text()]`,u=g(v,e,t),1===u&&!o)return v}},k=(e,t,r,a)=>{try{const a=r.filter(t=>S(t,e)),o=r=>{const a=f(r.name,r.nodeValue);return a?"class"===r.name?c(e,a,t):i(a)?[]:n.test(a)?[`@${r.name}="${a}"`]:[`normalize-space(@${r.name})="${a}"`]:[]},l=e=>{const t=o(e);return"class"===e.name?t.map(e=>[e]):t.length?[t]:[]},s=e=>e.reduce((e,t)=>{const n=l(t);return n.length?e.flatMap(e=>n.map(t=>[...e,...t])):[]},[[]]),u=()=>{const t=$(h(e)?.trim()||""),r=E(t);if(i(t))return r?[`contains(normalize-space(.),${p(r)})`]:[];if(!t||t.length>80||/^\d+$/.test(t)){const n=y(e,t);return n?[n]:[]}const a=new Set,o=y(e,t);return o?(a.add(o),Array.from(a)):(n.test(t)&&t.length<=40&&!t.includes(" ")&&a.add(`text()=${p(t)}`),t.includes(" ")&&a.add(`normalize-space(.)=${p($(t))}`),a.add(`contains(text(),${p(t)})`),Array.from(a))},m=(e,t)=>{const n=[],r=(a,o)=>{if(o.length!==t)for(let t=a;t<e.length;t++)o.push(e[t]),r(t+1,o),o.pop();else n.push([...o])};return r(0,[]),n},d=t=>b(e)?`//*[local-name()='${e.tagName}' and ${t.join(" and ")}]`:`//${e.tagName}[${t.join(" and ")}]`,v=(()=>{if(!(a.length<2))for(let n=2;n<=a.length;n++){const r=m(a,n);for(const n of r){const r=s(n);for(const n of r){if(n.length<2)continue;const r=d(n);let a;try{a=g(r,e,t)}catch{continue}if(1===a)return r}}}})();if(v)return v;const N=u();if(!N.length||!a.length)return;for(let n=1;n<=a.length;n++){const r=m(a,n);for(const n of r){const r=s(n);for(const n of r)if(n.length)for(const r of N){const a=[...n,r],o=d(a);try{if(1===g(o,e,t))return o}catch{continue}}}}}catch(e){console.log(`XPath generation error: ${JSON.stringify(e,null,2)}`)}},R=(e,t)=>{if(!e.textContent)return"";const r=(a=e,a?.childNodes[0]?.nodeValue||"");var a;const o=E(r);if(i(r)&&!o)return"";const l=i(r)?o:r,s=y(e,l);let c;return c=s?d(s,b(e),e.tagName||"*"):i(r)?d(`contains(normalize-space(.),${p(l)})`,b(e),e.tagName||"*"):!n.test(r)||/\s/.test(r.trim())?d(`normalize-space(.)=${p(l)}`,b(e),e.tagName||"*"):b(e)?`//*[local-name()='${e.tagName}' and .=${p(l)}]`:`//${e.tagName||"*"}[.=${p(l)}]`,c},T=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{fragment:t}=v(n);return t?d(`contains(normalize-space(.),${p(t)})`,b(e),e.tagName.toLowerCase()):""}return d(`normalize-space(${t})=${p($(n)).trim()}`,b(e),e.tagName.toLowerCase())},C=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{prefix:t,fragment:r}=v(n),a=t||r;return a?d(`starts-with(normalize-space(.),${p(a)})`,b(e),e.tagName.toLowerCase()):""}return d(`starts-with(${t},${p($(n)).trim()})`,b(e),e.tagName.toLowerCase())},A=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{fragment:t}=v(n);return t?d(`contains(normalize-space(.),${p(t)})`,b(e),e.tagName.toLowerCase()):""}return d(`contains(${t},${p($(n)).trim()})`,b(e),e.tagName.toLowerCase())},L=(e,t)=>{const r=t.flatMap(t=>(t=>{const r=f(t.name,t.value);if(!r)return[];if("class"===t.name){const t=e.getRootNode?.()??e.ownerDocument;return c(e,r,t)}return i(r)?[]:n.test(r)?[`@${t.name}=${p(r)}`]:[`normalize-space(@${t.name})=${p(r)}`]})(t)).filter(Boolean);if(r.length>=2)return d(`${r[0]} or ${r[1]}`,b(e),e.tagName.toLowerCase());const a=(()=>{const t=$(h(e)?.trim()||""),r=E(t);if(i(t))return r?`contains(normalize-space(.),${p(r)})`:null;const a=y(e,t);return a||(!t||t.length>80||/^\d+$/.test(t)?null:n.test(t)&&t.length<=40&&!t.includes(" ")?`text()=${p(t)}`:t.includes(" ")?`normalize-space(.)=${p(t)}`:`contains(text(),${p(t)})`)})();return 1===r.length&&a?d(`${r[0]} or ${a}`,b(e),e.tagName.toLowerCase()):""},D=(e,t,n)=>{try{return g(e,t,n)}catch(e){return 0}},P=(e,t)=>{const n=[],r=new Set,a=Array.from(e.attributes||[]),o=["id","data-testid","data-test","data-qa","name","aria-label","role"],l=[...o.map(e=>a.find(t=>t.name===e)).filter(Boolean),...a.filter(e=>!o.includes(e.name))],s=a=>{a&&!r.has(a)&&Y(a,e,t)&&(r.add(a),n.push(a))};l.forEach(t=>{if(S(t,e))for(const n of M(e,t.name,t.value))s(n)});const i=e.textContent?.trim();if(i&&i.length<40&&0===e.children.length){const t=R(e);t&&s(t)}if(a.length>1){const n=k(e,t,a);n&&s(n)}return n},O=(e,t)=>{const n=[];let r=t;for(;r&&r!==e;)n.unshift(X(r)),r=r.parentElement;return r===e?n.join("/"):""},_=(e,t)=>{const n=t??(e.getRootNode?.()||e.ownerDocument),r=X(e),a=b(e)?`//*[local-name()='${e.tagName.toLowerCase()}']`:`//${e.tagName.toLowerCase()}`;for(let t=e.parentElement;t;t=t.parentElement){const a=P(t,n);for(const o of a){console.log(`Trying ancestor XPath: ${o}`);const a=`${o}/descendant::${r}`;console.log(`Trying descendant XPath: ${a}`);const l=D(a,e,n);if(console.log(`Match count: ${l}`),1===l&&z(a,n)===e)return console.log(`Selected XPath: ${a}`),a;const s=O(t,e);if(s){const t=`${o}/${s}`;console.log(`Trying descendant XPath: ${t}`);const r=D(t,e,n);if(console.log(`Match count: ${r}`),1===r&&z(t,n)===e)return console.log(`Selected XPath: ${t}`),t}}}console.log(`Trying descendant XPath: ${a}`);const o=D(a,e,n);return console.log(`Match count: ${o}`),1===o&&z(a,n)===e?(console.log(`Selected XPath: ${a}`),a):(console.log("Selected XPath: "),"")},z=(e,t)=>{try{if(N(t)){const{owner:n,contextNode:r,cloneElement:a}=x(t);return n.evaluate(e,r,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue||a}return t.evaluate(e,t,null,t.defaultView.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}catch(e){return null}},Y=(e,t,n)=>{try{const{first:r,second:o}=a(e,n);return!!r&&!o&&r===t}catch{return!1}},X=e=>b(e)?`*[local-name()='${e.tagName.toLowerCase()}']`:e.tagName.toLowerCase(),j=(e,t,n)=>{if(!(e instanceof Element))return[];const r=new Set,a=[],o=Array.from(e.attributes||[]).filter(t=>{return t?.value&&(n=t.value,!/\d/.test(n))&&S(t,e);var n}),l=(e,t)=>{t&&!r.has(t)&&(r.add(t),a.push({key:e,value:t}))},s=["id","data-testid","data-test","data-qa","name","aria-label","role"];s.forEach(n=>{const r=o.find(e=>e.name===n);if(r)for(const n of M(e,r.name,r.value))n&&Y(n,e,t)&&l(`anchor by ${r.name}`,n)}),o.forEach(n=>{if(!s.includes(n.name))for(const r of M(e,n.name,n.value))r&&Y(r,e,t)&&l(`anchor by ${n.name}`,r)});const i=e.textContent?.trim();if(i&&i.length<40&&0===e.children.length){const n=R(e);n&&Y(n,e,t)&&l("anchor by text",n)}if(o.length>1){const n=k(e,t,o);n&&Y(n,e,t)&&l("anchor by combination",n)}const c=_(e,t);return c&&Y(c,e,t)&&l("anchor by tag",c),a},M=(e,t,n)=>{const r=new RegExp(/^[\S]+( [\S]+)*$/gi);let a="";if(n=f(t,n)){if("class"===t){const t=e.getRootNode?.()??e.ownerDocument;return c(e,n,t).map(t=>b(e)?`//*[local-name()='${e.tagName}' and ${t}]`:`//${e.tagName||"*"}[${t}]`)}if(i(n))return[];a=r.test(n)?b(e)?`//*[local-name()='${e.tagName}' and @${t}=${p(n)}]`:`//${e.tagName||"*"}[@${t}=${p(n)}]`:b(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${p(n)})]`:`//${e.tagName||"*"}[contains(@${t},${p(n)})]`}return a?[a]:[]},H=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 I(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)::/),r=n?.[1]??"none",a=t.match(/@([a-z0-9:-]+)/);return{axis:r,attribute:a?.[1]??"none",usesNormalize:t.includes("normalize-space")}}(e);return["XPATH",`axis:${n.axis}`,`attr:${n.attribute}`,`normalize:${n.usesNormalize}`,`shape:${t}`].join("|")}function V(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/ /g,"\\ ")}function q(e,t,n,r){const a=r?.getRootNode?.()??e,o=e=>{try{return Array.from(a.querySelectorAll(e))}catch{return[]}};try{switch(t){case"id":return 1===o(`#${V(n)}`).length;case"name":return 1===o(`[name="${V(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 W=[];const F={andConditions:"and",orConditions:"or",contains:"contains",startsWith:"startsWith",normalizeSpace:"normalizeSpace",axes:"axes",index:"index",hardCodedText:"text",tillTag:"tag"},G=e=>F[e]||e,Z=(e,t)=>t.map(t=>({...t,key:t?.key?.includes(e)?t.key:`${e} ${t.key}`.trim()})),B=(e,t)=>t?e:e.filter(e=>!(e=>{let t=null;for(let n=0;n<e.length;n++){const r=e[n];if(t){r===t&&(t=null);continue}if("'"===r||'"'===r){t=r;continue}if("["!==r)continue;const a=e.indexOf("]",n+1);if(-1===a)return!1;if(/^\s*\d+\s*$/.test(e.slice(n+1,a)))return!0}return!1})(e.value)),U=e=>e?.textContent?.replace(/\s+/g," ")?.trim()||"",J=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},K=(e,t)=>{const n=[];let r="previous"===t?e.previousElementSibling:e.nextElementSibling;for(;r;)r.classList?.contains("flntooltip")||n.push(r),r="previous"===t?r.previousElementSibling:r.nextElementSibling;return n},Q=(e,t)=>{const n=[];for(let r=e;r?.parentElement;r=r.parentElement){let e="previous"===t?r.previousElementSibling:r.nextElementSibling;for(;e;)e.classList?.contains("flntooltip")||(n.push(e),n.push({node:e,expand:()=>J(e)})),e="previous"===t?e.previousElementSibling:e.nextElementSibling}return n},ee=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},te=e=>{let t=0;e.id&&(t+=100),e.getAttribute("data-testid")&&(t+=90),e.className&&(t+=50),0===e.children.length&&(t+=30);const n=e.textContent?.trim();return n&&n.length<40&&(t+=40),t},ne=(e,t,n,r,a,o,l)=>{const s=X(n),i=[];let c=0;const u=t.includes("descendant")?10:20;for(const a of e){if(c++>u)break;if(i.length>=5)break;const e="node"in a?a.node:a;if(!(e instanceof Element))continue;if(ee(e))continue;const f=[()=>j(e,r),()=>{const t=[],n=Array.from(e.attributes||[]).filter(t=>t?.value&&!/\d/.test(t.value)&&S(t,e));for(const r of n){const n=r.value;if(!n)continue;const a=A(e,`@${r.name}`,n);a&&t.push({key:"contains",value:a});const o=C(e,`@${r.name}`,n);o&&t.push({key:"starts-with",value:o})}return t},()=>[{key:"tag",value:_(e,r)}]];for(let a=0;a<f.length;a++){let c=f[a]();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 a=`${e.value}/${t}::${s}`;if(Y(a,n,r)){if(i.push({key:o,value:a}),i.length>=2)return i;if(i.length>=5)return i}}if(l&&a===f.length-1)for(const a of c){const l=g(a.value,e,r);for(let e=1;e<=Math.min(l,3);e++){const l=`(${a.value})[${e}]/${t}::${s}`;if(Y(l,n,r)){if(i.push({key:o,value:l}),i.length>=2)return i;if(i.length>=5)return i}}}}if("expand"in a&&a.expand){const e=a.expand();let l=0;for(const a of e){if(l++>15)break;if(i.length>=5)break;if(!(a instanceof Element))continue;if(ee(a))continue;const e=j(a,r);for(const a of e.slice(0,5)){const e=`${a.value}/${t}::${s}`;if(Y(e,n,r)){if(i.push({key:o,value:e}),i.length>=2)return i;if(i.length>=5)return i}}}}}return i},re=(e,t)=>{const n=[];let r=e;for(;r&&n.length<t;)n.push(r),r=r.parentElement;return n},ae=(e,t)=>{for(let n=e.nextElementSibling;n;n=n.nextElementSibling)if(n===t)return"following-sibling";for(let n=e.previousElementSibling;n;n=n.previousElementSibling)if(n===t)return"preceding-sibling";return""},oe=(e,t)=>{const n=[];for(let r=e;r&&r!==t;r=r.parentElement){const e=r.parentElement;if(!e)return"";n.push(`/parent::${X(e)}`)}return n.join("")},le=(e,t)=>{const n=X(t);return t.parentElement===e?[`/${n}`,`/descendant::${n}`]:[`/descendant::${n}`]},se=(e,t,n)=>{const r=[],a=new Set,o=(e=>{const t=[],n=new Set,r=r=>{r&&r!==e&&!n.has(r)&&(r.contains(e)||e.contains(r)||(n.add(r),t.push(r)))};for(let n=e.parentElement,a=0;n&&a<4&&t.length<20;n=n.parentElement,a++){const e=[K(n,"previous").slice(0,4),K(n,"next").slice(0,4)];for(const t of e)for(const e of t){r(e);let t=0;for(const n of J(e).sort((e,t)=>te(t)-te(e))){if(t++>=8)break;r(n)}}}return t.sort((e,t)=>te(t)-te(e)).slice(0,20)})(e),l=re(e.parentElement,3);for(const n of o){const o=j(n,t).filter(e=>"anchor by tag"!==e.key).slice(0,2);if(!o.length)continue;const s=re(n,3);for(const i of o)for(const o of s){const s=oe(n,o);if(o===n||s)for(const n of l){if(!n||!n.contains(e))continue;const l=ae(o,n);if(!l)continue;const c=`/${l}::${X(n)}`;for(const o of le(n,e)){const n=`${i.value}${s}${c}${o}`;if(!a.has(n)&&(a.add(n),Y(n,e,t)&&(r.push({key:"chained axes fallback",value:n}),r.length>=2)))return r}}}}return r},ie=(e,t)=>{const n=[];for(let r=t;r&&r!==e;r=r.parentElement)n.unshift(X(r));if(!n.length)return"";const r=n.pop();return`${n.length?`/${n.join("/")}`:""}/child::${r}`},ce=(e,t,n,r)=>{const a=new Set,o=[],l=(e,t)=>{t&&!a.has(t)&&(!r&&/\[\d+\]/.test(t)||(a.add(t),o.push({key:e,value:t})))};return j(e,t).filter(e=>"anchor by tag"!==e.key).forEach(e=>l(e.key,e.value)),Array.from(e.attributes||[]).forEach(t=>{if(!(e=>!(!e?.value||["style","locator-data-tooltip","value"].includes(e.name)||0===e.name.indexOf("on")&&e.name.length>3))(t)||!i(t.value)||"class"===t.name)return;const n=(e=>{const t=(e.split(/\d/)[0]||"").replace(/[-_:\s.]+$/g,"").trim();return t.length>2&&/[a-zA-Z]/.test(t)?t:""})(t.value);if(!n)return;const r=A(e,`@${t.name}`,n);l(`anchor by stable ${t.name}`,r)}),o},ue=(e,t,n,r)=>{const a=[];for(let t=e.parentElement;t;t=t.parentElement)a.push(t);const o=J(e),l=K(e,"previous"),s=K(e,"next"),i=Q(e,"previous"),c=Q(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(a),axis:"child",key:"child"},{nodes:u(o,15),axis:"parent",key:"parent"}],[{nodes:u(a),axis:"descendant",key:"descendant"},{nodes:u(a),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"}]],m=((e,t,n,r)=>{const a=[],o=new Set;for(let n=e.parentElement,l=0;n&&l<8;n=n.parentElement,l++){const l=ie(n,e);if(!l)continue;const s=ce(n,t,0,r);for(const n of s.slice(0,3)){const r=`${n.value}${l}`;if(!o.has(r)&&(o.add(r),Y(r,e,t)&&(a.push({key:"child chain",value:r}),a.length>=2)))return a}}return a})(e,t,0,r);if(m.length)return m;for(const n of f)for(const a of n){const n=ne(a.nodes,a.axis,e,t,0,a.key,r);if(n.length>0)return n}return se(e,t)||[]},fe=(e,t,n,r,a)=>{const o=G(r);return"text"===o?Z(r,((e,t,n)=>{if(!U(e))return[];if("text"===n){const n=R(e)||me(e,t,!1,!1);return n?[{key:"xpath by text",value:n}]:[]}return[]})(e,t,o)):["contains","startsWith","normalizeSpace"].includes(o)?Z(r,((e,t,n)=>{const r=[],a=Array.from(e.attributes||[]).filter(t=>t?.value&&S(t,e)),o=U(e),l="contains"===n?A:"startsWith"===n?C:"normalizeSpace"===n?T:null;if(!l)return r;if(a.forEach(t=>{const a=l(e,`@${t.name}`,t.value);a&&r.push({key:`xpath by ${n} ${t.name}`,value:a})}),o){const t=l(e,".",o);t&&r.push({key:`xpath by ${n} text`,value:t})}return r})(e,0,o)):["and","or"].includes(o)?Z(r,((e,t,n,r)=>{const a=Array.from(e.attributes||[]).filter(t=>t?.value&&S(t,e));if("and"===r){const n=k(e,t,a);return n?[{key:"xpath by combination",value:n}]:[]}if("or"===r&&a.length>=1){const t=L(e,a);return t?[{key:"xpath by or",value:t}]:[]}return[]})(e,t,0,o)):"axes"===o?Z(r,ue(e,t,0,!1)):"index"===o?Z(r,((e,t)=>{const n=[],r=e.tagName.toLowerCase(),a=Array.from(t.querySelectorAll(r)).indexOf(e)+1;if(a<=0)return[];n.push({key:"xpath by index",value:`(//${r})[${a}]`});let o=e.parentElement;for(;o&&o!==t.body;){if(o.id||o.className){const t=o.id?M(o,"id",o.id):M(o,"class",o.className.toString());for(const a of t){const t=Array.from(o.querySelectorAll(r)).indexOf(e)+1;if(t>0){n.push({key:"xpath by index",value:`(${a}//${r})[${t}]`});break}}if(n.length>1)break}o=o.parentElement}return n})(e,t)):"tag"===o?Z(r,[{key:"xpath till tag",value:_(e,t)}]):[]};const me=(e,t,n,r)=>{if(""!=(e.textContent??"").trim()){const a=h(e);if(a)return w(e,t,".",a,n,r)}},de=(e,t,n,r,a)=>{const o=e;try{o.map(e=>{if("className"!==e.name&&"class"!==e.name&&S(e,t)){const o=((e,t,n,r,a)=>{let o;if(o=e.name,S(e,t))return w(t,n,`@${o}`,e.value,r,a)})(e,t,n,r,a);o&&W.push({key:`xpath by ${e.name}${r?" index":""}`,value:o})}});const l=me(t,n,r,a);if(l&&W.push({key:"xpath by text"+(r?" index":""),value:l}),o.find(e=>"className"===e.name)&&S(o?.find(e=>"className"===e.name),t)){let e=((e,t,n,r)=>{let a=e.className;if("string"!=typeof a&&(a=""),a=a?.replace("flndisabled","disabled"),a=f("class",a),a=a?.trim(),a&&S({name:"class",value:a},e))return w(e,t,"@class",a,n,r)})(t,n,r,a);e&&W.push({key:"xpath by class",value:e})}if(!W.length&&o.length>1){const e=k(t,n,o);e&&W.push({key:"xpath by combination",value:e})}if(!W.length){const r=function(e,t,n){const{tagName:r}=t,a=t.textContent.trim();if(!a)return;const o=$(a),l=E(o);if(i(o)&&!l)return;const s=i(o)?l:o,c=y(t,s);for(const l of e)if(S(l,t)){let e=f(l.name,l.nodeValue);const u=`//${r}[@${l.name}='${e}' and ${c||(i(o)?`contains(normalize-space(.),${p(s)})`:/\s/.test(a)?`normalize-space(.)=${p(s)}`:`text()=${p(s)}`)}]`;if(u&&1==g(u,t,n))return u}}(e,t,n);r&&W.push({key:"xpath by textAttribute",value:r})}}catch(e){console.log(e)}},he=(e,t,n,r,a=[],o=[])=>{W=[],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:f}=l,m=a.length>0?a:Array.from(f);if(de(m,l,c,n,r),o.length){const e=n||((e=[])=>e.some(e=>"index"===G(e)))(o),t=o.flatMap(e=>fe(l,c,0,e));return W=(e=>{const t=new Set;return e.filter(e=>!(!e?.value||t.has(e.value)||(t.add(e.value),0)))})(t.filter(e=>1===g(e.value,l,c))),B(W,e)}if(W.length){const t=W.length;for(let n=0;n<t;n++){let t=W[n].value;t="//*"+t.substring(t.indexOf("//")+2+u.length);1===g(t,e,c)&&W.push({key:`${W[n].key} regex`,value:t})}}return W.length||i||(W=ue(l,c,0,n)),B(W,n)};let ge=[];const pe=(e,t,n)=>{try{const r=n.querySelectorAll(e);return 1===r.length&&r[0]===t}catch{return!1}},$e=(e,t="single")=>{const n=[],r=e.getRootNode();try{const a=ye(e);if(a&&pe(a,e,r)&&(n.push({key:"cssSelector by id",value:a}),"single"===t))return n;const o=xe(e);o&&pe(o,e,r)&&n.push({key:"cssSelector by class",value:o});const l=Ne(e);l&&n.push({key:"cssSelector by name",value:l})}catch(e){console.error(e)}return n},ye=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 r=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if(e.id&&!m(e.id)){t+=`#${CSS.escape(e.id)}`,r.unshift(t);break}{let n=e,r=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&r++;1!==r&&(t+=`:nth-of-type(${r})`),1===r&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${r})`)}r.unshift(t),e=e.parentElement}return r.join(" > ")},ve=new Set(["id","class","style"]);function Ee(e){return Array.from(e.attributes).filter(e=>!ve.has(e?.name?.toLowerCase())&&e.value&&!m(e.value)).map(e=>{return`[${e.name}="${t=e.value,String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"]`;var t})}const Ne=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.getRootNode(),r=e.tagName.toLowerCase();if("style"===r||"script"===r)return;const a=Ee(e);for(const t of a){const a=`${r}${t}`;if(pe(a,e,n))return a}},xe=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 r=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if("string"!=typeof e.className||!e.className||m(e.className)||ge?.find(t=>t.element===e&&"class"===t.attributeName)){let n=e,r=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&r++;1!==r&&(t+=`:nth-of-type(${r})`),1===r&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${r})`)}else if(e.classList.remove("marked-element-temp"),e.classList.remove("removePointers"),e.className){t+=`.${e.className.trim().replace(/\s+/g,".")}`,r.unshift(t);break}r.unshift(t),e=e.parentElement}return r.join(" > ")},Se=(e,t,n)=>{const r=H(e);if(Pe(t,r)===n)return!0;if(function(e){return/\[(?:\s*\.|\s*contains\s*\(\s*\.|\s*normalize-space\s*\(\s*\.)/.test(e)}(r)){const e=t.evaluate(r,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},be=(e,t)=>{try{const n=e.querySelector(t);if(n)return n}catch(e){return console.error("Invalid CSS selector:",t,e),null}return Re(e.body,t)},we=(e,t,n)=>be(t,e)===n,ke=e=>e.name?.toLowerCase().includes("cssselector")??!1,Re=(e,t)=>{const n=Array.from(e.querySelectorAll("*"));try{for(let e=0;e<n.length;e++)if(n[e].shadowRoot){const{shadowRoot:r}=n[e];if(r){const e=Re(r,t);if(e)return e;if(r&&!t.includes("dynamic"))return r.querySelector(t)}}}catch(e){console.log(e)}return null},Te=e=>e?.id||null,Ce=e=>e.className||null,Ae=e=>e.textContent?.trim()||null,Le=e=>{const t=e;if(t.hasAttribute("name")){return`${t.getAttribute("name")}`||null}return null},De=["/preceding-sibling","/following-sibling","/parent","/descendant","/ancestor","/self","/ancestor-or-self","/child","/preceding","/following"];function Pe(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 Oe(e,t,n){if(e.includes(t)){const r=e.split(t)[0].trim(),a=n.defaultView;if(!a)return null;if(!e.includes("dynamic")){const t=new a.XPathEvaluator;if(t.evaluate(r,n,null,a.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue){let r;return t.evaluate(e,n,null,a.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue?(r=e,r):(console.error("Complete Locator is Invalid:",e),r=e,r)}console.error("Source Locator Not Found:",r)}}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 r=new Set;let a=[];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}`;r.has(t)||(r.add(t),a.push(e))}(l)}function l(e,t,n){if(ke(t))return be(e,n);if(t.name.includes("id")||n.startsWith("#"))return e.querySelector("#"+V(n));if(t.name.includes("className")||n.startsWith("."))return e.querySelector("."+n);if("name"===t.name){const t=V(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 r=Pe(e,H(n));return r&&o(t,{value:n,isRecorded:String(t.isRecorded).includes("N")?"N":"Y"}),r}}function s(e,t,n){const r=e.querySelectorAll("iframe");for(const e of r)try{const r=e.contentDocument||e.contentWindow?.document;if(!r)continue;const a=l(r,t,n);if(a)return a}catch{continue}return null}const i=(e,t,n)=>{if(!e)return null;let r=e.trim();return r&&"null"!==r.toLowerCase()?(r=r.replace(/\\"/g,'"').replace(/\\'/g,"'"),r=r.replace(/^['"](.+?)['"]$/,"$1"),r=r.replace(/''/g,"'"),r=r.replace(/\[@(id|name)=['"]{2}(.+?)['"]{2}\]/g,"[@$1='$2']"),"id"!==t&&"name"!==t||(r=r.replace(/['"]/g,"").trim()),"xpath"!==t||"Y"!==n||e.startsWith("//")?!r||/^['"]{2}$/.test(r)?null:r:null):null};e:for(const n of e.locators)try{String(n.isRecorded||"");const c=e.locators.filter(e=>"N"===e.isRecorded);if(c.length>0)for(const e of c)o(e);const u=String(n.value||n.type||"");if(u.includes("dynamic")||u.match("dynamic")||u.includes("{")||u.includes("}")){o(n);continue}if(e.isShared.includes("Y"))break e;try{let c=null;const u=n.value.split(">>>");for(const e of u){if(!t){console.error("Element not found at:",e);break}const r=e.trim();if(c=l(t,n,r),c||(c=s(t,n,r)),c||(c=Re(t.body,r)),!c){console.error("Element not found at:",r);break}}const f=(e,t)=>{const n=`${e}:${t}`;return r.has(n)};if(c){const n=e.locators.filter(e=>"xpath"===e.name&&e.value),r=e.locators.filter(e=>ke(e)&&e.value);for(const e of n)Se(e.value,t,c)&&o(e,{isSelfHealed:null});for(const e of r)we(e.value,t,c)&&o(e,{isSelfHealed:null});const l=a.filter(e=>"xpath"===e.name&&e.value),s=new Set(l.map(e=>I(e.value))),u=[],d=Te(c);if(d&&!f("id",d)&&!m(d)){const n=e.locators.find(e=>"id"===e.name);q(t,"id",d,c)&&(u.push("id"),o(n,{name:"id",type:"static",isRecorded:"Y",value:d}))}const h=c.tagName;if(h&&!f("tagName",h)){const n=e.locators.find(e=>"tagName"===e.name);q(t,"tagName",h,c)&&(u.push("tagName"),o(n,{name:"tagName",type:"static",isRecorded:"Y",value:h}))}const g=Ae(c);if(g&&!m(g)){const n=e.locators.find(e=>"linkText"===e.name);q(t,"linkText",g,c)&&(u.push("linkText"),o(n,{name:"linkText",type:"static",isRecorded:"Y",value:g}))}const p=Le(c);if(p&&!f("name",p)&&!m(p)){const n=e.locators.find(e=>"name"===e.name);q(t,"name",p,c)&&(u.push("name"),o(n,{name:"name",type:"static",isRecorded:"Y",value:p}))}const $=Ce(c);if($&&""!==$.trim()&&!$.includes(" ")&&!f("className",$)&&!m($)){const n=e.locators.find(e=>"className"===e.name);q(t,"className",$,c)&&(u.push("className"),o(n,{name:"className",type:"static",isRecorded:"Y",value:$}))}$e(c,"single").forEach(e=>{e.value&&!f("cssSelector",e.value)&&o(void 0,{name:"cssSelector",value:e.value,type:"static",isRecorded:"Y"})});const y=Array.from(c.attributes).filter(e=>!u.includes(e.name));let v=[];try{v=he(c,t,!1,!0,y)??[]}catch(e){console.error("Error generating XPath candidates:",e)}if(0!==v?.length){const e=n.filter(e=>!Se(e.value,t,c));let r=0;for(const t of e){if(r>=e.length)break;const n=I(t.value);if(s.has(n))continue;const a=v.find(e=>e.value&&I(e.value)===n);a?.value&&(o(t,{name:"xpath",value:a.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),r++)}if(r<e.length)for(const t of v){if(r>=e.length)break;if(!t.value)continue;const n=I(t.value);s.has(n)||(o(t,{name:"xpath",value:t.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),r++)}}for(const n of e.locators)try{for(const r of e.locators)if(r.value)for(const e of De)if(r.value.includes(e)){const a=Oe(r.value,e,t);if(a){o(r,{name:"xpath",value:a,isRecorded:""!==n.isRecorded&&null!==n.isRecorded?n.isRecorded:"Y"});break}}}catch(e){console.error("Error processing locator:",n,e)}if(a.length<5){const e=[{name:"id",value:Te(c)},{name:"name",value:Le(c)},{name:"className",value:Ce(c)},{name:"tagName",value:c.tagName},{name:"linkText",value:Ae(c)}];for(const n of e){if(a.length>4)break;const{name:e,value:r}=n;r&&(m(r)||f(e,r)||"className"===e&&r.includes(" ")||q(t,e,r,c)&&o(void 0,{name:e,type:"static",value:r,isRecorded:"Y",isSelfHealed:"Y"}))}a.length<5&&$e(c,"multiple").forEach(e=>{a.length>4||e.value&&(f("cssSelector",e.value)||q(t,"cssSelector",e.value,c)&&o(void 0,{name:"cssSelector",type:"static",value:e.value,isRecorded:"Y",isSelfHealed:"Y"}))})}const E=a.map(e=>({...e,value:i(e.value,e.name,e.isRecorded)})),N=[{name:`${e.name}`,desc:`${e.desc}`,type:`${e.type}`,locators:E.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 N}}catch(e){console.error("Error processing locator:",n,e);continue}}catch(e){console.error("Error processing locator:",n,e);continue}return null};async function ze(){return{fromHTML:async n=>{const r=new e,a=new t(n.replace(/\\"/g,'"').replace(/\\r/g,"\r").replace(/\\n/g,"\n").replace(/\\t/g,"\t"),{pretendToBeVisual:!0,runScripts:"dangerously",resources:"usable",virtualConsole:r}),{window:o}=a;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{ze as default};
|
|
2
2
|
//# sourceMappingURL=xpath.mjs.map
|