How to use this.adb.setAirplaneMode method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

network.js

Source: network.js Github

copy
1import log from '../logger';
2import _ from 'lodash';
3import { errors } from 'appium-base-driver';
4import B from 'bluebird';
5
6let commands = {}, helpers = {}, extensions = {};
7
8commands.getNetworkConnection = async function () {
9  log.info("Getting network connection");
10  let airplaneModeOn = await this.adb.isAirplaneModeOn();
11  let connection = airplaneModeOn ? 1 : 0;
12
13  // no need to check anything else if we are in airplane mode
14  if (!airplaneModeOn) {
15    let wifiOn = await this.isWifiOn();
16    connection += (wifiOn ? 2 : 0);
17    let dataOn = await this.adb.isDataOn();
18    connection += (dataOn ? 4 : 0);
19  }
20
21  return connection;
22};
23
24/**
25 * decoupling to override the behaviour in other drivers like UiAutomator2.
26 */
27commands.isWifiOn = async function () {
28  return await this.adb.isWifiOn();
29};
30
31commands.setNetworkConnection = async function (type) {
32  log.info("Setting network connection");
33  // decode the input
34  let airplaneMode = type % 2;
35  type >>= 1;
36  let wifi = type % 2;
37  type >>= 1;
38  let data = type % 2;
39
40  await this.wrapBootstrapDisconnect(async () => {
41    await this.adb.setAirplaneMode(airplaneMode);
42  });
43  await this.wrapBootstrapDisconnect(async () => {
44    await this.adb.broadcastAirplaneMode(airplaneMode);
45  });
46  if (!airplaneMode) {
47    await this.wrapBootstrapDisconnect(async () => {
48      await this.setWifiState(wifi);
49      await this.adb.setDataState(data, this.isEmulator());
50    });
51  }
52
53  return await this.getNetworkConnection();
54};
55
56/**
57 * decoupling to override behaviour in other drivers like UiAutomator2.
58 */
59commands.setWifiState = async function (wifi) {
60  await this.adb.setWifiState(wifi, this.isEmulator());
61};
62
63commands.toggleData = async function () {
64  let data = !(await this.adb.isDataOn());
65  log.info(`Turning network data ${data ? 'on' : 'off'}`);
66  await this.wrapBootstrapDisconnect(async () => {
67    await this.adb.setWifiAndData({data}, this.isEmulator());
68  });
69};
70
71commands.toggleWiFi = async function () {
72  let wifi = !(await this.adb.isWifiOn());
73  log.info(`Turning WiFi ${wifi ? 'on' : 'off'}`);
74  await this.wrapBootstrapDisconnect(async () => {
75    await this.adb.setWifiAndData({wifi}, this.isEmulator());
76  });
77};
78
79commands.toggleFlightMode = async function () {
80  /*
81   * TODO: Implement isRealDevice(). This method fails on
82   * real devices, it should throw a NotYetImplementedError
83   */
84  let flightMode = !(await this.adb.isAirplaneModeOn());
85  log.info(`Turning flight mode ${flightMode ? 'on' : 'off'}`);
86  await this.wrapBootstrapDisconnect(async () => {
87    await this.adb.setAirplaneMode(flightMode);
88  });
89  await this.wrapBootstrapDisconnect(async () => {
90    await this.adb.broadcastAirplaneMode(flightMode);
91  });
92};
93
94commands.setGeoLocation = async function (location) {
95  return await this.adb.setGeoLocation(location, this.isEmulator());
96};
97
98commands.toggleLocationServices = async function () {
99  log.info("Toggling location services");
100  let api = await this.adb.getApiLevel();
101  if (this.isEmulator()) {
102    let providers = await this.adb.getLocationProviders();
103    let isGpsEnabled = providers.indexOf('gps') !== -1;
104    await this.adb.toggleGPSLocationProvider(!isGpsEnabled);
105    return;
106  }
107
108  if (api > 15) {
109    let seq = [19, 19]; // up, up
110    if (api === 16) {
111      // This version of Android has a "parent" button in its action bar
112      seq.push(20); // down
113    } else if (api >= 19) {
114      // Newer versions of Android have the toggle in the Action bar
115      seq = [22, 22, 19]; // right, right, up
116      /*
117       * Once the Location services switch is OFF, it won't receive focus
118       * when going back to the Location Services settings screen unless we
119       * send a dummy keyevent (UP) *before* opening the settings screen
120       */
121      await this.adb.keyevent(19);
122    }
123    await this.toggleSetting('LOCATION_SOURCE_SETTINGS', seq);
124  } else {
125    // There's no global location services toggle on older Android versions
126    throw new errors.NotYetImplementedError();
127  }
128};
129
130helpers.toggleSetting = async function (setting, preKeySeq) {
131  /*
132   * preKeySeq is the keyevent sequence to send over ADB in order
133   * to position the cursor on the right option.
134   * By default it's [up, up, down] because we usually target the 1st item in
135   * the screen, and sometimes when opening settings activities the cursor is
136   * already positionned on the 1st item, but we can't know for sure
137   */
138  if (_.isNull(preKeySeq)) {
139    preKeySeq = [19, 19, 20]; // up, up, down
140  }
141
142  await this.openSettingsActivity(setting);
143
144  for (let key of preKeySeq) {
145    await this.doKey(key);
146  }
147
148  let {appPackage, appActivity} = await this.adb.getFocusedPackageAndActivity();
149
150  /*
151   * Click and handle potential ADB disconnect that occurs on official
152   * emulator when the network connection is disabled
153   */
154  await this.wrapBootstrapDisconnect(async () => {
155    await this.doKey(23);
156  });
157
158  /*
159   * In one particular case (enable Location Services), a pop-up is
160   * displayed on some platforms so the user accepts or refuses that Google
161   * collects location data. So we wait for that pop-up to open, if it
162   * doesn't then proceed
163   */
164  try {
165    await this.adb.waitForNotActivity(appPackage, appActivity, 5000);
166    await this.doKey(22); // right
167    await this.doKey(23); // click
168    await this.adb.waitForNotActivity(appPackage, appActivity, 5000);
169  } catch (ign) {}
170
171  await this.adb.back();
172};
173
174helpers.doKey = async function (key) {
175  // TODO: Confirm we need this delay. Seems to work without it.
176  await B.delay(2000);
177  await this.adb.keyevent(key);
178};
179
180helpers.wrapBootstrapDisconnect = async function (wrapped) {
181  this.bootstrap.ignoreUnexpectedShutdown = true;
182  try {
183    await wrapped();
184    await this.adb.restart();
185    await this.bootstrap.start(this.opts.appPackage, this.opts.disableAndroidWatchers, this.opts.acceptSslCerts);
186  } finally {
187    this.bootstrap.ignoreUnexpectedShutdown = false;
188  }
189};
190
191Object.assign(extensions, commands, helpers);
192export { commands, helpers };
193export default extensions;
194
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 Android 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)