UNPKG

testplane

Version:

Tests framework based on mocha and wdio

78 lines 3.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientBridge = void 0; const node_util_1 = require("node:util"); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const debug_1 = __importDefault(require("debug")); const error_1 = require("./error"); const debug = (0, debug_1.default)("testplane:client-bridge"); const bundlesCache = {}; // eslint-disable-next-line @typescript-eslint/no-explicit-any class ClientBridge { // eslint-disable-next-line @typescript-eslint/no-explicit-any static async create(browser, namespace, opts) { const needsCompatLib = opts?.needsCompatLib ?? false; const scriptFileName = needsCompatLib ? "bundle.compat.js" : "bundle.native.js"; const scriptFilePath = path_1.default.join(__dirname, "..", "client-scripts", namespace, "build", scriptFileName); let debugBrowserId = ""; if (debug.enabled) { debugBrowserId = `${browser?.capabilities?.browserName} ${browser?.capabilities?.browserVersion}:${browser?.sessionId}`; } if (bundlesCache[scriptFilePath]) { debug(`creating ClientBridge with cached script for namespace ${namespace} at ${scriptFilePath} for browser ${debugBrowserId}`); return new ClientBridge(browser, bundlesCache[scriptFilePath], namespace); } const bundle = await fs_1.default.promises.readFile(scriptFilePath, { encoding: "utf8" }); bundlesCache[scriptFilePath] = bundle; debug(`creating ClientBridge with new script for namespace ${namespace} at ${scriptFilePath} for browser ${debugBrowserId}`); return new this(browser, bundle, namespace); } constructor(browser, script, namespace) { this._browser = browser; this._script = script; this._namespace = namespace; } async call(name, args) { return this._callCommand(this._clientMethodCommand(name, args), true); } async _callCommand(command, shouldInjectScriptBeforeCall) { try { if (debug.enabled) { debug(` > calling command ${command}`); } const result = await this._browser.execute(command); if (debug.enabled) { debug(` < result for command ${command.slice(0, 256)}: ${(0, node_util_1.inspect)(result, { depth: null })}`); } if (!result || !result.isClientScriptNotInjected) { return result; } if (shouldInjectScriptBeforeCall) { await this._inject(); return this._callCommand(command, false); } throw new error_1.ClientBridgeError("Unable to inject client script"); } catch (e) { throw new error_1.ClientBridgeError(`Failed to call command ${command} due to error: ${e?.message ?? "Unknown error"}`, { cause: e }); } } _clientMethodCommand(name, args) { const params = args.map(arg => (arg !== undefined ? JSON.stringify(arg) : "undefined")).join(", "); const call = `__geminiCore['${this._namespace}'].${String(name)}(${params})`; return this._guardClientCall(call); } _guardClientCall(call) { return `return (typeof __geminiCore !== "undefined" && typeof __geminiCore['${this._namespace}'] !== "undefined") ? ${call} : {isClientScriptNotInjected: true}`; } async _inject() { debug(` > injecting script into namespace ${this._namespace}: ${this._script.slice(0, 256)}...`); await this._browser.execute(this._script, this._namespace); } } exports.ClientBridge = ClientBridge; //# sourceMappingURL=index.js.map