UNPKG

webdriverio

Version:

Next-gen browser and mobile automation test framework for Node.js

57 lines (56 loc) • 1.95 kB
// src/scripts/react.ts var waitToLoadReact = async function waitToLoadReact2() { return new Promise((resolve) => { const start = Date.now(); const check = () => { const api = window.__wdioReact; if (api && api.findRoots().some(api.isRendered) || Date.now() - start >= 5e3) { return resolve(); } setTimeout(check, 200); }; check(); }); }; var react$ = function react$2(selector, props, state, reactElement) { const api = window.__wdioReact; const fiber = api && reactElement && api.findFiber(reactElement); const fibers = !api ? [] : !reactElement ? api.findRoots() : fiber ? [fiber] : []; if (!api || !fibers.length) { throw new Error( reactElement ? "Could not find instance of React in given element" : "Could not find the root element of your application" ); } let element; for (let i = 0; !element && i < fibers.length; i++) { [element] = api.query(selector, props || {}, state || {}, fibers[i]); } if (!element) { return { message: `React element with selector "${selector}" wasn't found` }; } return Array.isArray(element.node) ? element.node[0] : element.node; }; var react$$ = function react$$2(selector, props, state, reactElement) { const api = window.__wdioReact; const fiber = api && reactElement && api.findFiber(reactElement); const fibers = !api ? [] : !reactElement ? api.findRoots() : fiber ? [fiber] : []; if (!api || !fibers.length) { throw new Error( reactElement ? "Could not find instance of React in given element" : "Could not find the root element of your application" ); } const nodes = []; for (const element of fibers.flatMap((fiber2) => api.query(selector, props || {}, state || {}, fiber2))) { for (const node of [element.node].flat()) { if (node && !nodes.includes(node)) { nodes.push(node); } } } return nodes; }; export { react$, react$$, waitToLoadReact };