UNPKG

appium-xcuitest-driver

Version:

Appium driver for iOS using XCUITest for backend

169 lines 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConditionInducer = createConditionInducer; const appium_ios_device_1 = require("appium-ios-device"); const utils_1 = require("../utils"); const remotexpc_utils_1 = require("./remotexpc-utils"); /** * RemoteXPC-based implementation for iOS 18+. */ class RemoteXPCConditionInducer { udid; log; connection = null; constructor(udid, log) { this.udid = udid; this.log = log; } async list() { let connection = null; try { connection = await this.startConnection(); const result = await connection.conditionInducer.list(); return result; } catch (err) { this.log.error(`Failed to list condition inducers via RemoteXPC: ${err.message}`); throw err; } finally { if (connection) { this.log.info(`Closing remoteXPC connection for device ${this.udid}`); await connection.remoteXPC.close(); } } } async enable(_conditionID, profileID) { if (this.connection) { throw new Error(`Condition inducer is already running. Disable it first in order to call 'enable' again.`); } try { this.connection = await this.startConnection(); await this.connection.conditionInducer.set(profileID); this.log.info(`Successfully enabled condition profile: ${profileID}`); return true; } catch (err) { await this.close(); this.log.error(`Condition inducer '${profileID}' cannot be enabled: '${err.message}'`); throw err; } } async disable() { if (!this.connection) { this.log.warn('Condition inducer connection is not active'); return false; } try { await this.connection.conditionInducer.disable(); this.log.info('Successfully disabled condition inducer'); return true; } catch (err) { this.log.warn(`Failed to disable condition inducer via RemoteXPC: ${err.message}`); return false; } finally { this.log.info(`Closing remoteXPC connection for device ${this.udid}`); await this.close(); } } async close() { if (this.connection) { await this.connection.remoteXPC.close(); this.connection = null; } } isActive() { return this.connection !== null; } async startConnection() { const Services = await (0, remotexpc_utils_1.getRemoteXPCServices)(); return Services.startDVTService(this.udid); } } /** * Instrument service implementation (appium-ios-device) for iOS < 18 and RemoteXPC fallback. */ class InstrumentConditionInducer { udid; log; service = null; constructor(udid, log) { this.udid = udid; this.log = log; } async list() { const service = (await appium_ios_device_1.services.startInstrumentService(this.udid)); try { const ret = await service.callChannel(appium_ios_device_1.INSTRUMENT_CHANNEL.CONDITION_INDUCER, 'availableConditionInducers'); return ret.selector; } finally { service.close(); } } async enable(conditionID, profileID) { if (this.service && !this.service._socketClient.destroyed) { throw new Error(`Condition inducer has been started. A condition is already active.`); } this.service = (await appium_ios_device_1.services.startInstrumentService(this.udid)); const ret = await this.service.callChannel(appium_ios_device_1.INSTRUMENT_CHANNEL.CONDITION_INDUCER, 'enableConditionWithIdentifier:profileIdentifier:', conditionID, profileID); if (typeof ret.selector !== 'boolean') { this.service.close(); this.service = null; throw new Error(`Enable condition inducer error: '${JSON.stringify(ret.selector)}'`); } return ret.selector; } async disable() { if (!this.service) { this.log.warn('Condition inducer server has not started'); return false; } try { const ret = await this.service.callChannel(appium_ios_device_1.INSTRUMENT_CHANNEL.CONDITION_INDUCER, 'disableActiveCondition'); if (typeof ret.selector !== 'boolean') { this.log.warn(`Disable condition inducer error: '${JSON.stringify(ret.selector)}'`); return false; } return ret.selector; } finally { if (this.service) { this.service.close(); this.service = null; } } } async close() { if (this.service) { this.service.close(); this.service = null; } } isActive() { return this.service !== null && !this.service._socketClient.destroyed; } } /** * Picks RemoteXPC when the platform is iOS/tvOS 18+ and probe succeeds; otherwise legacy instrument service. */ async function createConditionInducer(params) { const { udid, log, platformVersion } = params; if (!(0, utils_1.isIos18OrNewerPlatform)(platformVersion)) { return new InstrumentConditionInducer(udid, log); } const xpcInducer = new RemoteXPCConditionInducer(udid, log); try { const connection = await xpcInducer.startConnection(); await connection.remoteXPC.close(); } catch (err) { log.warn(`Unable to use RemoteXPC-based condition inducer for device ${udid}, ` + `falling back to the legacy implementation: ${err.message}`); return new InstrumentConditionInducer(udid, log); } return xpcInducer; } //# sourceMappingURL=condition-inducer-client.js.map