UNPKG

appium-ios-simulator

Version:
161 lines 5.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.installApp = installApp; exports.getUserInstalledBundleIdsByBundleName = getUserInstalledBundleIdsByBundleName; exports.isAppInstalled = isAppInstalled; exports.removeApp = removeApp; exports.launchApp = launchApp; exports.terminateApp = terminateApp; exports.isAppRunning = isAppRunning; exports.scrubApp = scrubApp; const node_path_1 = __importDefault(require("node:path")); const support_1 = require("@appium/support"); const asyncbox_1 = require("asyncbox"); const utils_1 = require("../utils"); /** * Install valid .app package on Simulator. * * @param app The path to the .app package. */ async function installApp(app) { return await this.simctl.installApp(app); } /** * Returns user installed bundle ids which has 'bundleName' in their Info.Plist as 'CFBundleName' * * @param bundleName The bundle name of the application to be checked. * @return The list of bundle ids which have 'bundleName' */ async function getUserInstalledBundleIdsByBundleName(bundleName) { const appsRoot = node_path_1.default.resolve(this.getDir(), 'Containers', 'Bundle', 'Application'); // glob all Info.plist from simdir/data/Containers/Bundle/Application const infoPlists = await support_1.fs.glob('*/*.app/Info.plist', { cwd: appsRoot, absolute: true, }); if (infoPlists.length === 0) { return []; } const bundleInfoPromises = []; for (const infoPlist of infoPlists) { bundleInfoPromises.push((async () => { try { return await support_1.plist.parsePlistFile(infoPlist); } catch { return null; } })()); } const bundleInfos = (await Promise.all(bundleInfoPromises)).filter(utils_1.isPlainObject); const bundleIds = bundleInfos .filter(({ CFBundleName }) => CFBundleName === bundleName) .map(({ CFBundleIdentifier }) => CFBundleIdentifier); if (bundleIds.length === 0) { return []; } this.log.debug(`The simulator has ${support_1.util.pluralize('bundle', bundleIds.length, true)} which ` + `have '${bundleName}' as their 'CFBundleName': ${JSON.stringify(bundleIds)}`); return bundleIds; } /** * Verify whether the particular application is installed on Simulator. * * @param bundleId The bundle id of the application to be checked. * @return True if the given application is installed. */ async function isAppInstalled(bundleId) { try { const appContainer = await this.simctl.getAppContainer(bundleId); if (!appContainer.endsWith('.app')) { return false; } return await support_1.fs.exists(appContainer); } catch { // get_app_container subcommand fails for system applications, // so we try the hidden appinfo subcommand, which prints correct info for // system/hidden apps try { await this.simctl.appInfo(bundleId); return true; } catch { return false; } } } /** * Uninstall the given application from the current Simulator. * * @param bundleId The bundle ID of the application to be removed. */ async function removeApp(bundleId) { await this.simctl.removeApp(bundleId); } /** * Starts the given application on Simulator * * @param bundleId The bundle ID of the application to be launched * @param opts Launch options */ async function launchApp(bundleId, opts = {}) { await this.simctl.launchApp(bundleId); const { wait = false, timeoutMs = 10000 } = opts; if (!wait) { return; } try { await (0, asyncbox_1.waitForCondition)(async () => await this.isAppRunning(bundleId), { waitMs: timeoutMs, intervalMs: 300, }); } catch { throw new Error(`App '${bundleId}' is not runnning after ${timeoutMs}ms timeout.`); } } /** * Stops the given application on Simulator. * * @param bundleId The bundle ID of the application to be stopped */ async function terminateApp(bundleId) { await this.simctl.terminateApp(bundleId); } /** * Check if app with the given identifier is running. * * @param bundleId The bundle ID of the application to be checked. */ async function isAppRunning(bundleId) { return (await this.ps()).some(({ name }) => name === bundleId); } /** * Scrub (delete the preferences and changed files) the particular application on Simulator. * The app will be terminated automatically if it is running. * * @param bundleId Bundle identifier of the application. * @throws {Error} if the given app is not installed. */ async function scrubApp(bundleId) { const appDataRoot = await this.simctl.getAppContainer(bundleId, 'data'); const appFiles = await support_1.fs.glob('**/*', { cwd: appDataRoot, nodir: true, absolute: true, }); this.log.info(`Found ${appFiles.length} ${bundleId} app ${support_1.util.pluralize('file', appFiles.length, false)} to scrub`); if (appFiles.length === 0) { return; } try { await this.terminateApp(bundleId); } catch { } await Promise.all(appFiles.map((p) => support_1.fs.rimraf(p))); } //# sourceMappingURL=applications.js.map