UNPKG

appium-remote-debugger

Version:
86 lines 2.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getScriptForAtom = exports.getAtom = void 0; const support_1 = require("@appium/support"); const path_1 = __importDefault(require("path")); const lodash_1 = __importDefault(require("lodash")); const logger_1 = __importDefault(require("./logger")); const utils_1 = require("./utils"); const ATOMS_CACHE = {}; /** * @param {any} obj * @returns {string} */ function atomsStringify(obj) { if (typeof obj === 'undefined') { return 'undefined'; } return JSON.stringify(obj); } /** * * @param {string} atomName * @returns {Promise<Buffer>} */ async function getAtom(atomName) { // check if we have already loaded and cached this atom if (!lodash_1.default.has(ATOMS_CACHE, atomName)) { const atomFileName = path_1.default.resolve((0, utils_1.getModuleRoot)(), 'atoms', `${atomName}.js`); try { ATOMS_CACHE[atomName] = await support_1.fs.readFile(atomFileName); } catch (e) { throw new Error(`Unable to load Atom '${atomName}' from file '${atomFileName}'`); } } return ATOMS_CACHE[atomName]; } exports.getAtom = getAtom; /** * @param {string} script * @param {string} frame * @returns {Promise<string>} */ async function wrapScriptForFrame(script, frame) { logger_1.default.debug(`Wrapping script for frame '${frame}'`); const elFromCache = await getAtom('get_element_from_cache'); return `(function (window) { var document = window.document; ` + `return (${script}); })((${elFromCache.toString('utf8')})(${atomsStringify(frame)}))`; } /** * * @param {string} atom * @param {any[]} [args] * @param {string[]} [frames] * @param {string?} [asyncCallBack] * @returns {Promise<string>} */ async function getScriptForAtom(atom, args = [], frames = [], asyncCallBack = null) { const atomSrc = (await getAtom(atom)).toString('utf8'); let script; if (frames.length > 0) { script = atomSrc; for (const frame of frames) { script = await wrapScriptForFrame(script, frame); } } else { logger_1.default.debug(`Executing '${atom}' atom in default context`); script = `(${atomSrc})`; } // add the arguments, as strings args = args.map(atomsStringify); if (asyncCallBack) { script += `(${args.join(',')}, ${asyncCallBack}, true)`; } else { script += `(${args.join(',')})`; } return script; } exports.getScriptForAtom = getScriptForAtom; exports.default = getAtom; //# sourceMappingURL=atoms.js.map