UNPKG

kayle

Version:

Extremely fast and accurate accessibility engine built for any headless tool like playwright or puppeteer.

1 lines 4.11 kB
Object.defineProperty(exports,"__esModule",{value:!0}),exports.runActionsList=exports.runAction=exports.actions=void 0;const e="Failed action: no element matching selector";async function t(e,t,a,n,r){let o=(r??exports.actions).find(e=>e.match.test(n));if(!o)return console.error(`Failed action: "${n}" cannot be resolved`);await o.run(e,t,a,n.match(o.match))}exports.actions=[{name:"navigate-url",match:/^navigate to( url)? (.+)$/i,run:async(e,t,a,n)=>{let r=n[2];try{await t.goto(r)}catch(e){console.error(`Failed action: Could not navigate to "${r}"`)}}},{name:"click-element",match:/^click( element)? (.+)$/i,run:async(t,a,n,r)=>{let o=r[2];try{await a.click(o)}catch(t){console.error(`${e} "${o}"`)}}},{name:"set-field-value",match:/^set( field)? (.+?) to (.+)$/i,run:async(t,a,n,r)=>{let o=r[2],c=r[3];try{await a.evaluate((e,t)=>{let a=document.querySelector(e);if(!a)return Promise.reject(Error("No element found"));let n=Object.getPrototypeOf(a),{set:r}=Object.getOwnPropertyDescriptor(n,"value")||{};return r?r.call(a,t):a.value=t,a.dispatchEvent(new Event("input",{bubbles:!0})),Promise.resolve()},o,c)}catch(t){console.error(`${e} "${o}"`)}}},{name:"clear-field-value",match:/^clear( field)? (.+?)$/i,run:async(t,a,n,r)=>{let o=r[2];try{await a.evaluate(e=>{let t=document.querySelector(e);if(!t)return Promise.reject(Error("No element found"));let a=Object.getPrototypeOf(t),{set:n}=Object.getOwnPropertyDescriptor(a,"value")||{};return n?n.call(t,""):t.value="",t.dispatchEvent(new Event("input",{bubbles:!0})),Promise.resolve()},o)}catch(t){console.error(`${e} "${o}"`)}}},{name:"check-field",match:/^(check|uncheck)( field)? (.+)$/i,run:async(t,a,n,r)=>{let o="uncheck"!==r[1],c=r[3];try{await a.evaluate((e,t)=>{let a=document.querySelector(e);return a?(a.checked=t,a.dispatchEvent(new Event("change",{bubbles:!0})),Promise.resolve()):Promise.reject(Error("No element found"))},c,o)}catch(t){console.error(`${e} "${c}"`)}}},{name:"style",match:/^style ( content)? (.+?)$/i,run:async(e,t,a,n)=>{await t.addStyleTag({content:n[2]})}},{name:"screen-capture",match:/^(screen[ -]?capture|capture[ -]?screen)( to)? (.+)$/i,run:async(e,t,a,n)=>{await t.screenshot({path:n[3],fullPage:!0})}},{name:"pdf",match:/^pdf( to)? (.+)$/i,run:async(e,t,a,n)=>{await t.pdf({path:n[3]})}},{name:"drag-and-drop",match:/^drag-and-drop ( element)? (.+)( to ) (target)$/i,run:async(e,t,a,n)=>{if("function"==typeof t.dragAndDrop)await t.dragAndDrop(n[1],n[3]);else{t?.mouse?.dragAndDrop;{let e=await t.$$eval([n[1],n[3]],e=>e.map(e=>e.getBoundingClientRect));await t.dragAndDrop(e[0],e[1])}}}},{name:"wait-for-url",match:/^wait for (fragment|hash|host|path|url)( to (not )?be)? ([^\s]+)$/i,run:async(e,t,a,n)=>{let r=n[4],o=void 0!==n[3],c=n[1],i="";switch(c){case"fragment":case"hash":i="hash";break;case"host":i="host";break;case"path":i="pathname";break;default:i="href"}await t.waitForFunction(function(e,t,a){return a?window.location[e]!==t:window.location[e]===t},{},i,r,o)}},{name:"wait-for-element-state",match:/^wait for( element)? (.+)( to be) (added|removed|visible|hidden)$/i,run:async(e,t,a,n)=>{let r=n[2],o=n[4];await t.waitForFunction((e,t)=>{let a=document.querySelector(e);if(a&&"added"===t||!a&&"removed"===t)return!0;let n=!!(a&&(a.offsetWidth||a.offsetHeight||a.getClientRects().length));return"visible"===t&&n||"hidden"===t&&!n},{polling:200},r,o)}},{name:"wait-for-element-event",match:/^wait for( element)? (.+) to emit (.+)$/i,run:async(t,a,n,r)=>{let o=r[2],c=r[3];try{await a.evaluate((e,t)=>{let a=document.querySelector(e);if(!a)return Promise.reject(Error("No element found"));a.addEventListener(t,()=>{window._a11yWaitForElementEventFired=!0},{once:!0})},o,c),await a.waitForFunction(()=>!!window._a11yWaitForElementEventFired&&(delete window._a11yWaitForElementEventFired,!0),{polling:200})}catch(t){return console.error(`${e} "${o}"`)}}},{name:"wait",match:/^wait (\d+)ms$/i,run:async(e,t,a,n)=>{let r=parseInt(n[1],10);await new Promise(e=>setTimeout(e,r))}}],exports.runAction=t;const a=async e=>{for(let a of e.actions)await t(e.browser,e.page,e,a)};exports.runActionsList=a;