UNPKG

appium-xcuitest-driver

Version:

Appium driver for iOS using XCUITest for backend

176 lines 8.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceDiscovery = void 0; const appium_ios_simulator_1 = require("appium-ios-simulator"); const helpers_1 = require("../commands/helpers"); const constants_1 = require("../constants"); const utils_1 = require("../utils"); const real_device_management_1 = require("./real-device-management"); const wda_host_ops_1 = require("./wda-host-ops"); class DeviceDiscovery { config; createdSimulator = false; iosSdkVersion = null; platformVersion; constructor(config) { this.config = config; } get log() { return this.config.log; } get sessionOpts() { return this.config.driverOpts; } get resolvedSessionOpts() { return { ...this.sessionOpts, platformVersion: this.platformVersion, }; } async determine() { // in the one case where we create a sim, we will set this state this.createdSimulator = false; this.iosSdkVersion = null; this.platformVersion = this.sessionOpts.platformVersion; const isStrictHostMode = (0, wda_host_ops_1.isStrictHostUtilityMode)(this.sessionOpts); return await this.selectStrategy(isStrictHostMode).determine(); } selectStrategy(isStrictHostMode) { const strategies = [ { isApplicable: ({ udid }) => udid?.toLowerCase() === constants_1.UDID_AUTO, determine: async () => await this.determineDeviceWithAutoUdid(isStrictHostMode), }, { isApplicable: ({ udid }) => Boolean(udid), determine: async () => await this.determineDeviceWithExplicitUdid(isStrictHostMode), }, { isApplicable: () => true, determine: async () => await this.determineSimulatorDevice(isStrictHostMode), }, ]; return strategies.find((strategy) => strategy.isApplicable(this.sessionOpts)); } async setupSimulatorPlatformVersion() { const iosSdkVersion = await (0, helpers_1.getAndCheckIosSdkVersion)(); this.iosSdkVersion = iosSdkVersion; this.log.info(`iOS SDK Version set to '${iosSdkVersion}'`); if (!this.sessionOpts.platformVersion && iosSdkVersion) { this.log.info(`No platformVersion specified. Using the latest version Xcode supports: '${iosSdkVersion}'. ` + `This may cause problems if a simulator does not exist for this platform version.`); this.platformVersion = (0, utils_1.normalizePlatformVersion)(iosSdkVersion); } } async ensurePlatformVersion(device) { if (this.platformVersion) { return; } this.platformVersion = await device.getPlatformVersion(); this.log.info(`No platformVersion specified. Using device version: '${this.platformVersion}'`); } async createRealDeviceForUdid(udid) { this.log.debug(`Creating iDevice object with udid '${udid}'`); const device = new real_device_management_1.RealDevice(udid, this.sessionOpts, this.log); await this.ensurePlatformVersion(device); return this.toResult({ device, realDevice: true, udid }); } async determineDeviceWithAutoUdid(isStrictHostMode) { if (isStrictHostMode) { throw new Error(`Automatic device selection requires macOS device discovery utilities. ` + `Set an explicit real-device 'appium:udid' when running from '${process.platform}'.`); } try { const udid = await this.config.detectUdid(); return await this.createRealDeviceForUdid(udid); } catch (err) { this.log.warn(`Cannot detect any connected real devices. Falling back to Simulator. Original error: ${err.message}`); await this.setupSimulatorPlatformVersion(); const device = await this.config.getExistingSimulator(this.resolvedSessionOpts); if (!device) { throw this.log.errorWithException(`Cannot detect udid for ${this.sessionOpts.deviceName} Simulator running iOS ${this.platformVersion}`); } await this.ensurePlatformVersion(device); return this.toResult({ device, realDevice: false, udid: device.udid }); } } async determineDeviceWithExplicitUdid(isStrictHostMode) { const udid = this.sessionOpts.udid; let isRealDeviceUdid = false; // If webDriverAgentUrl is set with a real device, assume the user prepared the device. const shouldCheckAvailableRealDevices = !this.sessionOpts.webDriverAgentUrl; if (shouldCheckAvailableRealDevices) { if (isStrictHostMode) { isRealDeviceUdid = true; } else { const devices = await (0, real_device_management_1.getConnectedDevices)(this.sessionOpts); this.log.debug(`Available real devices: ${devices.join(', ')}`); isRealDeviceUdid = devices.includes(udid); } } if (!isRealDeviceUdid) { const simulatorDevice = await this.determineSimulatorWithExplicitUdid(udid, shouldCheckAvailableRealDevices, isStrictHostMode); if (simulatorDevice) { return simulatorDevice; } } return await this.createRealDeviceForUdid(udid); } async determineSimulatorWithExplicitUdid(udid, shouldCheckAvailableRealDevices, isStrictHostMode) { if (isStrictHostMode) { this.log.debug(`Skipping Simulator lookup for '${udid}' because the selected session ` + `strategy must not use macOS Simulator utilities`); return null; } try { const device = await (0, appium_ios_simulator_1.getSimulator)(udid, { devicesSetPath: this.sessionOpts.simulatorDevicesSetPath, logger: this.log, }); await this.ensurePlatformVersion(device); return this.toResult({ device, realDevice: false, udid }); } catch { if (shouldCheckAvailableRealDevices) { throw new Error(`Unknown device or simulator UDID: '${udid}'`); } this.log.debug('Skipping checking of the real devices availability since the session specifies appium:webDriverAgentUrl'); return null; } } async determineSimulatorDevice(isStrictHostMode) { this.log.info(`No real device udid has been provided in capabilities. ` + `Will select a matching simulator to run the test.`); if (isStrictHostMode) { throw new Error(`A real-device 'appium:udid' is required when running from '${process.platform}' without ` + `macOS Simulator utilities.`); } await this.setupSimulatorPlatformVersion(); if (this.sessionOpts.enforceFreshSimulatorCreation) { this.log.debug(`New simulator is requested. If this is not wanted, set 'enforceFreshSimulatorCreation' capability to false`); } else { const device = await this.config.getExistingSimulator(this.resolvedSessionOpts); if (device) { await this.ensurePlatformVersion(device); return this.toResult({ device, realDevice: false, udid: device.udid }); } } this.log.info('Using desired caps to create a new simulator'); this.createdSimulator = true; const device = await this.config.createSimulator(this.resolvedSessionOpts); await this.ensurePlatformVersion(device); return this.toResult({ device, realDevice: false, udid: device.udid }); } toResult(result) { return { ...result, createdSimulator: this.createdSimulator, iosSdkVersion: this.iosSdkVersion, platformVersion: this.platformVersion, }; } } exports.DeviceDiscovery = DeviceDiscovery; //# sourceMappingURL=device-discovery.js.map