How to use services.startNotificationProxyService method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Sign up Free
_

ios-deploy.js

Source: ios-deploy.js Github

copy
1/* eslint-disable promise/prefer-await-to-callbacks */
2import { fs } from 'appium-support';
3import path from 'path';
4import { services } from 'appium-ios-device';
5import B from 'bluebird';
6import log from './logger';
7import _ from 'lodash';
8
9const APPLICATION_INSTALLED_NOTIFICATION = 'com.apple.mobile.application_installed';
10const INSTALLATION_STAGING_DIR = 'PublicStaging';
11const ITEM_PUSH_TIMEOUT = 30 * 1000;
12const APPLICATION_NOTIFICATION_TIMEOUT = 30 * 1000;
13
14class IOSDeploy {
15
16  constructor (udid) {
17    this.udid = udid;
18  }
19
20  async remove (bundleid) {
21    const service = await services.startInstallationProxyService(this.udid);
22    try {
23      await service.uninstallApplication(bundleid);
24    } finally {
25      service.close();
26    }
27  }
28
29  async removeApp (bundleId) {
30    await this.remove(bundleId);
31  }
32
33  async install (app) {
34    const start = new Date();
35    try {
36      const bundlePathOnPhone = await this.pushAppBundle(app);
37      await this.installApplication(bundlePathOnPhone);
38      log.info(`Installation is successful after ${new Date() - start}ms`);
39    } catch (e) {
40      log.error('Error was thrown during the installation process', e);
41      throw new Error(`Could not install app: '${e.message}'`);
42    }
43  }
44
45  async installApplication (bundlePathOnPhone) {
46    const notificationService = await services.startNotificationProxyService(this.udid);
47    const installationService = await services.startInstallationProxyService(this.udid);
48    const appInstalledNotification = new B((resolve) => {
49      notificationService.observeNotification(APPLICATION_INSTALLED_NOTIFICATION, { notification: resolve });
50    });
51    try {
52      await installationService.installApplication(bundlePathOnPhone, { PackageType: 'Developer'});
53      try {
54        await appInstalledNotification.timeout(APPLICATION_NOTIFICATION_TIMEOUT, `Couldn't get the application installed notification within ${APPLICATION_NOTIFICATION_TIMEOUT}ms but we will continue`);
55      } catch (e) {
56        log.warn(`Failed to receive the notification. Error: ${e.message}`);
57      }
58    } finally {
59      installationService.close();
60      notificationService.close();
61    }
62  }
63
64  async pushAppBundle (app) {
65    const start = new Date();
66    const afcService = await services.startAfcService(this.udid);
67    // We are pushing serially due to this https://github.com/appium/appium/issues/13115. There is nothing else we can do besides this
68    try {
69      const bundlePathOnPhone = await this.createAppPath(afcService, app);
70      await fs.walkDir(app, true, async (itemPath, isDir) => {
71        const pathOnPhone = path.join(bundlePathOnPhone, path.relative(app, itemPath));
72        if (isDir) {
73          await afcService.createDirectory(pathOnPhone);
74        } else {
75          const readStream = fs.createReadStream(itemPath, {autoClose: true});
76          const writeStream = await afcService.createWriteStream(pathOnPhone, {autoDestroy: true });
77          writeStream.on('finish', writeStream.destroy);
78          const itemPushWait = new B((resolve, reject) => {
79            writeStream.on('close', resolve);
80            writeStream.on('error', reject);
81          });
82          readStream.pipe(writeStream);
83          await itemPushWait.timeout(ITEM_PUSH_TIMEOUT, `Couldn't push '${itemPath}' within the timeout ${ITEM_PUSH_TIMEOUT}ms`);
84        }
85      });
86      log.debug(`Pushed the app files successfully after ${new Date() - start}ms`);
87      return bundlePathOnPhone;
88    } finally {
89      afcService.close();
90    }
91  }
92
93  async createAppPath (afcService, localAppPath) {
94    const basename = path.basename(localAppPath);
95    const relativePath = path.join(INSTALLATION_STAGING_DIR, basename);
96    try {
97      await afcService.deleteDirectory(relativePath);
98    } catch (ign) {}
99    await afcService.createDirectory(relativePath);
100    return relativePath;
101  }
102
103  async installApp (app) {
104    await this.install(app);
105  }
106
107  /**
108   * Return an application object if test app has 'bundleid'.
109   * The target bundleid can be User and System apps.
110   * @param {string} bundleid The bundleId to ensure it is installed
111   * @return {boolean} Returns True if the bundleid exists in the result of 'listApplications' like:
112   * { "com.apple.Preferences":{
113   *   "UIRequiredDeviceCapabilities":["arm64"],
114   *   "UIRequiresFullScreen":true,
115   *   "CFBundleInfoDictionaryVersion":"6.0",
116   *   "Entitlements":
117   *     {"com.apple.frontboard.delete-application-snapshots":true,..
118   */
119  async isAppInstalled (bundleid) {
120    const service = await services.startInstallationProxyService(this.udid);
121    try {
122      const applications = await service.lookupApplications({ bundleIds: bundleid });
123      return !!applications[bundleid];
124    } finally {
125      service.close();
126    }
127  }
128
129  /**
130   * @param {string} bundleName The name of CFBundleName in Info.plist
131   *
132   * @returns {Array<string>} A list of User level apps' bundle ids which has
133   *                          'CFBundleName' attribute as 'bundleName'.
134   */
135  async getUserInstalledBundleIdsByBundleName (bundleName) {
136    const service = await services.startInstallationProxyService(this.udid);
137    try {
138      const applications = await service.listApplications({applicationType: 'User'});
139      return _.reduce(applications, (acc, {CFBundleName}, key) => {
140        if (CFBundleName === bundleName) {
141          acc.push(key);
142        }
143        return acc;
144      }, []);
145    } finally {
146      service.close();
147    }
148  }
149}
150
151export default IOSDeploy;
152
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Xcuitest Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)