UNPKG

appium-xcuitest-driver

Version:

Appium driver for iOS using XCUITest for backend

171 lines 6.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConditionInducer = createConditionInducer; const appium_ios_device_1 = require("appium-ios-device"); /** * RemoteXPC-based implementation for iOS 18+. */ class RemoteXPCConditionInducer { udid; log; remoteXPCFacade; connection = null; constructor(udid, log, remoteXPCFacade) { this.udid = udid; this.log = log; this.remoteXPCFacade = remoteXPCFacade; } 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 DVT service connection for device ${this.udid}`); await connection.dvtService.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 DVT service connection for device ${this.udid}`); await this.close(); } } async close() { if (this.connection) { await this.connection.dvtService.close(); this.connection = null; } } isActive() { return this.connection !== null; } async startConnection() { return this.remoteXPCFacade.requireService('condition inducer', (Services) => 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 session facade is eligible and probe succeeds; otherwise legacy instrument service. */ async function createConditionInducer(params) { const { udid, log, remoteXPCFacade = null } = params; if (!remoteXPCFacade?.eligible) { return new InstrumentConditionInducer(udid, log); } if (!(await remoteXPCFacade.determineAvailability())) { return new InstrumentConditionInducer(udid, log); } const xpcInducer = new RemoteXPCConditionInducer(udid, log, remoteXPCFacade); try { const connection = await xpcInducer.startConnection(); await connection.dvtService.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