UNPKG

appium-remote-debugger

Version:
156 lines 6.13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.launchSafari = launchSafari; exports.startTimeline = startTimeline; exports.stopTimeline = stopTimeline; exports.overrideUserAgent = overrideUserAgent; exports.isJavascriptExecutionBlocked = isJavascriptExecutionBlocked; exports.garbageCollect = garbageCollect; const utils_1 = require("../utils"); const bluebird_1 = __importStar(require("bluebird")); const property_accessors_1 = require("./property-accessors"); const SAFARI_BUNDLE_ID = 'com.apple.mobilesafari'; const GARBAGE_COLLECT_TIMEOUT_MS = 5000; /** * Launches Safari application on the device by sending a launch command * to the remote debugger. */ async function launchSafari() { await this.requireRpcClient().send('launchApplication', { bundleId: SAFARI_BUNDLE_ID, }); } /** * Starts recording the timeline by registering an event listener and * sending the Timeline.start command to the remote debugger. * * @param fn - Event listener function that will be called when timeline events are recorded. * @returns A promise that resolves when the timeline recording has started. */ async function startTimeline(fn) { this.log.debug('Starting to record the timeline'); this.requireRpcClient().on('Timeline.eventRecorded', fn); return await this.requireRpcClient().send('Timeline.start', { appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), }); } /** * Stops recording the timeline by sending the Timeline.stop command * to the remote debugger. */ async function stopTimeline() { this.log.debug('Stopping to record the timeline'); await this.requireRpcClient().send('Timeline.stop', { appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), }); } /** * Overrides the user agent string for the current page. * Note: This may not work for mobile Safari. * * @param value - The user agent string to set. * @returns A promise that resolves when the user agent has been overridden. */ async function overrideUserAgent(value) { this.log.debug('Setting overrideUserAgent'); return await this.requireRpcClient().send('Page.overrideUserAgent', { appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), value, }); } /** * Checks whether JavaScript execution is currently blocked on the page * by attempting to execute a simple JavaScript command with a timeout. * * @param timeoutMs - The maximum amount of milliseconds to wait for a JavaScript * command response. Defaults to 1000ms. * @returns A promise that resolves to true if JavaScript execution is blocked, * false if it is not blocked. */ async function isJavascriptExecutionBlocked(timeoutMs = 1000) { try { await bluebird_1.default.resolve(this.requireRpcClient().send('Runtime.evaluate', { expression: '1+1;', returnByValue: true, appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), })).timeout(timeoutMs); return false; } catch { return true; } } /** * Triggers garbage collection on the page's JavaScript heap. * This method will gracefully handle cases where garbage collection cannot * be performed (e.g., when not connected to a page). * * @param timeoutMs - Maximum time in milliseconds to wait for garbage collection * to complete. Defaults to GARBAGE_COLLECT_TIMEOUT_MS (5000ms). */ async function garbageCollect(timeoutMs = GARBAGE_COLLECT_TIMEOUT_MS) { this.log.debug(`Garbage collecting with ${timeoutMs}ms timeout`); try { (0, utils_1.checkParams)({ appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), }); } catch { this.log.debug(`Unable to collect garbage at this time`); return; } try { await bluebird_1.default.resolve(this.requireRpcClient().send('Heap.gc', { appIdKey: (0, property_accessors_1.getAppIdKey)(this), pageIdKey: (0, property_accessors_1.getPageIdKey)(this), })).timeout(timeoutMs); this.log.debug(`Garbage collection successful`); } catch (e) { if (e instanceof bluebird_1.TimeoutError) { this.log.debug(`Garbage collection timed out after ${timeoutMs}ms`); } else { this.log.debug(`Unable to collect garbage: ${e.message}`); } } } //# sourceMappingURL=misc.js.map