How to use sim.updateSettings 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
_

settings.js

Source: settings.js Github

copy
1import _ from 'lodash';
2import logger from './logger';
3
4
5const SETTINGS_CAPS = [
6  'locationServicesEnabled',
7  'locationServicesAuthorized',
8];
9const SAFARI_SETTINGS_CAPS = [
10  'safariAllowPopups',
11  'safariIgnoreFraudWarning',
12  'safariOpenLinksInBackground',
13];
14
15async function launchAndQuitSimulator (sim, safari) {
16  logger.debug('No simulator directories found.');
17  return await sim.launchAndQuit(safari);
18}
19
20function checkPreferences (settings, opts = {}) {
21  for (let setting of settings) {
22    if (_.has(opts, setting)) {
23      return true;
24    }
25  }
26  return false;
27}
28
29async function setLocaleAndPreferences (sim, opts, safari = false, shutdownFn = _.noop) {
30  const localConfig = await setLocale(sim, opts, {}, safari);
31  const prefsUpdated = await setPreferences(sim, opts, safari);
32  if (localConfig._updated || prefsUpdated) {
33    logger.debug("Updated settings. Rebooting the simulator if it's already open");
34    await shutdownFn(sim);
35  }
36  delete localConfig._updated;
37  return localConfig;
38}
39
40// pass in the simulator so that other systems that use the function can supply
41// whatever they have
42async function setLocale (sim, opts, localeConfig = {}, safari = false) {
43  if (!opts.language && !opts.locale && !opts.calendarFormat) {
44    logger.debug("No reason to set locale");
45    return {
46      _updated: false,
47    };
48  }
49
50  // we need the simulator to have its directories in place
51  if (await sim.isFresh()) {
52    await launchAndQuitSimulator(sim, safari);
53  }
54
55  logger.debug('Setting locale information');
56  localeConfig = {
57    language: opts.language || localeConfig.language,
58    locale: opts.locale || localeConfig.locale,
59    calendarFormat: opts.calendarFormat || localeConfig.calendarFormat,
60    _updated: false,
61  };
62
63  try {
64    let updated = await sim.updateLocale(opts.language, opts.locale, opts.calendarFormat);
65    if (updated) {
66      localeConfig._updated = true;
67    }
68  } catch (e) {
69    logger.errorAndThrow(`Appium was unable to set locale info: ${e}`);
70  }
71
72  return localeConfig;
73}
74
75async function setPreferences (sim, opts, safari = false) {
76  let needToSetPrefs = checkPreferences(SETTINGS_CAPS, opts);
77  let needToSetSafariPrefs = checkPreferences(SAFARI_SETTINGS_CAPS, opts);
78  if (!needToSetPrefs && !needToSetSafariPrefs) {
79    logger.debug("No iOS / app preferences to set");
80    return false;
81  }
82
83  logger.debug("Setting iOS and app preferences");
84
85  if (await sim.isFresh()) {
86    await launchAndQuitSimulator(sim, safari);
87  }
88
89  try {
90    if (needToSetPrefs) {
91      await setLocServicesPrefs(sim, opts);
92    }
93  } catch (e) {
94    logger.error("Error setting location services preferences, prefs will not work");
95    logger.error(e);
96  }
97
98  try {
99    if (needToSetSafariPrefs) {
100      await setSafariPrefs(sim, opts);
101    }
102  } catch (e) {
103    logger.error("Error setting safari preferences, prefs will not work");
104    logger.error(e);
105  }
106
107  return true;
108}
109
110async function setLocServicesPrefs (sim, opts = {}) {
111  let locServ = _.find([opts.locationServicesEnabled, opts.locationServicesAuthorized], (c) => {
112    return !_.isUndefined(c);
113  });
114  if (!_.isUndefined(locServ)) {
115    locServ = locServ ? 1 : 0;
116    logger.debug(`Setting location services to ${locServ}`);
117    await sim.updateSettings('locationServices', {
118      LocationServicesEnabled: locServ,
119      'LocationServicesEnabledIn7.0': locServ,
120      'LocationServicesEnabledIn8.0': locServ
121    });
122  }
123  if (!_.isUndefined(opts.locationServicesAuthorized)) {
124    if (!opts.bundleId) {
125      let msg = "Can't set location services for app without bundle ID";
126      logger.errorAndThrow(msg);
127    }
128    let locAuth = !!opts.locationServicesAuthorized;
129    if (locAuth) {
130      logger.debug("Authorizing location services for app");
131    } else {
132      logger.debug("De-authorizing location services for app");
133    }
134    await sim.updateLocationSettings(opts.bundleId, locAuth);
135  }
136}
137
138async function setSafariPrefs (sim, opts = {}) {
139  let safariSettings = {};
140
141  if (_.has(opts, 'safariAllowPopups')) {
142    let val = !!opts.safariAllowPopups;
143    logger.debug(`Setting javascript window opening to '${val}'`);
144    safariSettings.WebKitJavaScriptCanOpenWindowsAutomatically = val;
145    safariSettings.JavaScriptCanOpenWindowsAutomatically = val;
146  }
147  if (_.has(opts, 'safariIgnoreFraudWarning')) {
148    let val = !opts.safariIgnoreFraudWarning;
149    logger.debug(`Setting fraudulent website warning to '${val}'`);
150    safariSettings.WarnAboutFraudulentWebsites = val;
151  }
152  if (_.has(opts, 'safariOpenLinksInBackground')) {
153    let val = opts.safariOpenLinksInBackground ? 1 : 0;
154    logger.debug(`Setting opening links in background to '${!!val}'`);
155    safariSettings.OpenLinksInBackground = val;
156  }
157  if (_.size(safariSettings) > 0) {
158    await sim.updateSafariSettings(safariSettings);
159  }
160}
161
162export { setLocale, setPreferences, setLocaleAndPreferences };
163
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)