UNPKG

playwright-reselect

Version:

A tiny helper to wright test once and reuse the logic anywhere

8 lines 2.57 kB
import{expect as g}from"@playwright/test";function T(e){let t=g(e);function o(i,a){let r={};for(let s of Object.keys(i)){let c=i[s];typeof c=="function"?r[s]=async(...p)=>(await c.apply(i,p),a):typeof c=="object"&&c!==null&&(r[s]=o(c,a))}return r}let n={};return Object.assign(n,o(t,n)),n}function f(e){return Object.assign(e,{debug:h(e),expectChain:()=>T(e)})}function m(e){let t=[],o=/(>)(<)(\/*)/g;e=e.replace(o,`$1 $2$3`);let n=0,i=" ";return e.split(` `).forEach(a=>{let r=a.trim();r.match(/^<\/\w/)&&(n=Math.max(n-1,0)),t.push(i.repeat(n)+r),r.match(/^<\w[^>]*[^\/]>$/)&&n++}),t.join(` `)}function D(e,t=[]){let o=new Map;if(e.children)for(let n of Object.keys(e.children)){let i=e.children[n];i.alias&&o.set(i.alias,{path:[...t,n],descriptor:i}),D(i,[...t,n]).forEach((r,s)=>{o.set(s,r)})}return o}function h(e){return async()=>{try{let t=await e.evaluate(n=>n.outerHTML),o=` [DEBUG] ${e.toString()} ${m(t)} `;return console.log(o),o}catch(t){return console.error("[DEBUG ERROR] Could not read innerHTML:",t),t}}}function d(e,t,o){let n=e.locator._selector,i=D(t),a={get:()=>{let r=e.page.locator(e.locator._selector);return e.locator=e.page.locator(":root"),f(r)},debug:h(e.locator),expectChain:()=>T(e.locator),skipToAlias:()=>{let r={};return i.forEach((s,c)=>{r[c]=()=>{e.locator=e.page.locator(n);let p=t;for(let u of s.path){if(!p.children||!p.children[u])throw new Error(`Invalid path to alias "${c}"`);p=p.children[u],p.build(e)}return d(e,s.descriptor,[...o||[],...s.path])}}),r}};if(t.children)for(let r of Object.keys(t.children)){let s=t.children[r];a[r]=()=>{s.build(e);let c=o?[...o,r]:[r];return d(e,s,c)}}if(t.custom)for(let r of Object.keys(t.custom)){let s=t.custom[r];a[r]=(...c)=>{let p=s(e,...c);return e.locator=e.page.locator(":root"),f(p)}}return Object.assign(a,{inspect:()=>(console.log("[INSPECT] "+e.locator._selector),a)}),a}function C(e){let t={};for(let o of Object.keys(e.descriptor)){let n=e.descriptor[o];t[o]=()=>(n.build(e),d(e,n,[o]))}return t}var y=class{constructor(t,o){if(!o)throw new Error("new TestLocatorResolver(page, descriptor): descriptor is required");if(!t)throw new Error("new TestLocatorResolver(page, descriptor): page is required");this.page=t,this.descriptor=o,this.locator=t.locator(":root"),Object.assign(this,C(this))}},b=e=>t=>new y(t,e);import{expect as A}from"@playwright/test";var k=e=>e,N=e=>e,l=A,x={};for(let e of Object.keys(l)){let t=l[e];typeof t=="function"&&(x[e]=async(...o)=>(await t.apply(l,o),x))}export{k as defineBranch,N as defineTree,b as reselectTree}; //# sourceMappingURL=index.js.map