appium-android-driver 7.8.3 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/build/lib/commands/app-management.d.ts +129 -5
  3. package/build/lib/commands/app-management.d.ts.map +1 -1
  4. package/build/lib/commands/app-management.js +433 -128
  5. package/build/lib/commands/app-management.js.map +1 -1
  6. package/build/lib/commands/appearance.d.ts +17 -4
  7. package/build/lib/commands/appearance.d.ts.map +1 -1
  8. package/build/lib/commands/appearance.js +32 -33
  9. package/build/lib/commands/appearance.js.map +1 -1
  10. package/build/lib/commands/context/cache.d.ts +19 -0
  11. package/build/lib/commands/context/cache.d.ts.map +1 -0
  12. package/build/lib/commands/context/cache.js +32 -0
  13. package/build/lib/commands/context/cache.js.map +1 -0
  14. package/build/lib/commands/context/exports.d.ts +141 -0
  15. package/build/lib/commands/context/exports.d.ts.map +1 -0
  16. package/build/lib/commands/context/exports.js +351 -0
  17. package/build/lib/commands/context/exports.js.map +1 -0
  18. package/build/lib/commands/context/helpers.d.ts +98 -0
  19. package/build/lib/commands/context/helpers.d.ts.map +1 -0
  20. package/build/lib/commands/context/helpers.js +715 -0
  21. package/build/lib/commands/context/helpers.js.map +1 -0
  22. package/build/lib/commands/device/common.d.ts +23 -0
  23. package/build/lib/commands/device/common.d.ts.map +1 -0
  24. package/build/lib/commands/device/common.js +230 -0
  25. package/build/lib/commands/device/common.js.map +1 -0
  26. package/build/lib/commands/device/emulator-actions.d.ts +114 -0
  27. package/build/lib/commands/device/emulator-actions.d.ts.map +1 -0
  28. package/build/lib/commands/device/emulator-actions.js +197 -0
  29. package/build/lib/commands/device/emulator-actions.js.map +1 -0
  30. package/build/lib/commands/device/emulator-console.d.ts +7 -0
  31. package/build/lib/commands/device/emulator-console.d.ts.map +1 -0
  32. package/build/lib/commands/device/emulator-console.js +24 -0
  33. package/build/lib/commands/device/emulator-console.js.map +1 -0
  34. package/build/lib/commands/device/utils.d.ts +50 -0
  35. package/build/lib/commands/device/utils.d.ts.map +1 -0
  36. package/build/lib/commands/device/utils.js +238 -0
  37. package/build/lib/commands/device/utils.js.map +1 -0
  38. package/build/lib/commands/deviceidle.d.ts +8 -5
  39. package/build/lib/commands/deviceidle.d.ts.map +1 -1
  40. package/build/lib/commands/deviceidle.js +31 -37
  41. package/build/lib/commands/deviceidle.js.map +1 -1
  42. package/build/lib/commands/element.d.ts +99 -5
  43. package/build/lib/commands/element.d.ts.map +1 -1
  44. package/build/lib/commands/element.js +152 -116
  45. package/build/lib/commands/element.js.map +1 -1
  46. package/build/lib/commands/execute.d.ts +12 -4
  47. package/build/lib/commands/execute.d.ts.map +1 -1
  48. package/build/lib/commands/execute.js +83 -78
  49. package/build/lib/commands/execute.js.map +1 -1
  50. package/build/lib/commands/file-actions.d.ts +42 -5
  51. package/build/lib/commands/file-actions.d.ts.map +1 -1
  52. package/build/lib/commands/file-actions.js +230 -194
  53. package/build/lib/commands/file-actions.js.map +1 -1
  54. package/build/lib/commands/find.d.ts +5 -4
  55. package/build/lib/commands/find.d.ts.map +1 -1
  56. package/build/lib/commands/find.js +7 -10
  57. package/build/lib/commands/find.js.map +1 -1
  58. package/build/lib/commands/geolocation.d.ts +45 -0
  59. package/build/lib/commands/geolocation.d.ts.map +1 -0
  60. package/build/lib/commands/geolocation.js +182 -0
  61. package/build/lib/commands/geolocation.js.map +1 -0
  62. package/build/lib/commands/ime.d.ts +25 -5
  63. package/build/lib/commands/ime.d.ts.map +1 -1
  64. package/build/lib/commands/ime.js +59 -42
  65. package/build/lib/commands/ime.js.map +1 -1
  66. package/build/lib/commands/intent.d.ts +56 -5
  67. package/build/lib/commands/intent.d.ts.map +1 -1
  68. package/build/lib/commands/intent.js +135 -83
  69. package/build/lib/commands/intent.js.map +1 -1
  70. package/build/lib/commands/keyboard.d.ts +58 -4
  71. package/build/lib/commands/keyboard.d.ts.map +1 -1
  72. package/build/lib/commands/keyboard.js +119 -17
  73. package/build/lib/commands/keyboard.js.map +1 -1
  74. package/build/lib/commands/lock/exports.d.ts +301 -0
  75. package/build/lib/commands/lock/exports.d.ts.map +1 -0
  76. package/build/lib/commands/lock/exports.js +121 -0
  77. package/build/lib/commands/lock/exports.js.map +1 -0
  78. package/build/lib/commands/lock/helpers.d.ts +349 -0
  79. package/build/lib/commands/lock/helpers.d.ts.map +1 -0
  80. package/build/lib/commands/lock/helpers.js +375 -0
  81. package/build/lib/commands/lock/helpers.js.map +1 -0
  82. package/build/lib/commands/log.d.ts +59 -5
  83. package/build/lib/commands/log.d.ts.map +1 -1
  84. package/build/lib/commands/log.js +150 -140
  85. package/build/lib/commands/log.js.map +1 -1
  86. package/build/lib/commands/media-projection.d.ts +16 -5
  87. package/build/lib/commands/media-projection.d.ts.map +1 -1
  88. package/build/lib/commands/media-projection.js +69 -58
  89. package/build/lib/commands/media-projection.js.map +1 -1
  90. package/build/lib/commands/memory.d.ts +9 -5
  91. package/build/lib/commands/memory.d.ts.map +1 -1
  92. package/build/lib/commands/memory.js +19 -24
  93. package/build/lib/commands/memory.js.map +1 -1
  94. package/build/lib/commands/misc.d.ts +42 -0
  95. package/build/lib/commands/misc.d.ts.map +1 -0
  96. package/build/lib/commands/misc.js +100 -0
  97. package/build/lib/commands/misc.js.map +1 -0
  98. package/build/lib/commands/network.d.ts +61 -5
  99. package/build/lib/commands/network.d.ts.map +1 -1
  100. package/build/lib/commands/network.js +196 -189
  101. package/build/lib/commands/network.js.map +1 -1
  102. package/build/lib/commands/performance.d.ts +67 -27
  103. package/build/lib/commands/performance.d.ts.map +1 -1
  104. package/build/lib/commands/performance.js +105 -80
  105. package/build/lib/commands/performance.js.map +1 -1
  106. package/build/lib/commands/permissions.d.ts +12 -6
  107. package/build/lib/commands/permissions.d.ts.map +1 -1
  108. package/build/lib/commands/permissions.js +65 -62
  109. package/build/lib/commands/permissions.js.map +1 -1
  110. package/build/lib/commands/recordscreen.d.ts +44 -5
  111. package/build/lib/commands/recordscreen.d.ts.map +1 -1
  112. package/build/lib/commands/recordscreen.js +131 -126
  113. package/build/lib/commands/recordscreen.js.map +1 -1
  114. package/build/lib/commands/resources.d.ts +16 -0
  115. package/build/lib/commands/resources.d.ts.map +1 -0
  116. package/build/lib/commands/resources.js +91 -0
  117. package/build/lib/commands/resources.js.map +1 -0
  118. package/build/lib/commands/shell.d.ts +8 -5
  119. package/build/lib/commands/shell.d.ts.map +1 -1
  120. package/build/lib/commands/shell.js +29 -33
  121. package/build/lib/commands/shell.js.map +1 -1
  122. package/build/lib/commands/streamscreen.d.ts +34 -6
  123. package/build/lib/commands/streamscreen.d.ts.map +1 -1
  124. package/build/lib/commands/streamscreen.js +166 -162
  125. package/build/lib/commands/streamscreen.js.map +1 -1
  126. package/build/lib/commands/system-bars.d.ts +18 -13
  127. package/build/lib/commands/system-bars.d.ts.map +1 -1
  128. package/build/lib/commands/system-bars.js +68 -64
  129. package/build/lib/commands/system-bars.js.map +1 -1
  130. package/build/lib/commands/time.d.ts +14 -0
  131. package/build/lib/commands/time.d.ts.map +1 -0
  132. package/build/lib/commands/time.js +39 -0
  133. package/build/lib/commands/time.js.map +1 -0
  134. package/build/lib/commands/touch.d.ts +99 -6
  135. package/build/lib/commands/touch.d.ts.map +1 -1
  136. package/build/lib/commands/touch.js +399 -280
  137. package/build/lib/commands/touch.js.map +1 -1
  138. package/build/lib/commands/types.d.ts +110 -2
  139. package/build/lib/commands/types.d.ts.map +1 -1
  140. package/build/lib/doctor/checks.d.ts.map +1 -1
  141. package/build/lib/doctor/checks.js +4 -4
  142. package/build/lib/doctor/checks.js.map +1 -1
  143. package/build/lib/driver.d.ts +224 -27
  144. package/build/lib/driver.d.ts.map +1 -1
  145. package/build/lib/driver.js +232 -7
  146. package/build/lib/driver.js.map +1 -1
  147. package/build/lib/index.d.ts +1 -4
  148. package/build/lib/index.d.ts.map +1 -1
  149. package/build/lib/index.js +1 -13
  150. package/build/lib/index.js.map +1 -1
  151. package/build/lib/logger.js.map +1 -1
  152. package/build/lib/method-map.d.ts +0 -23
  153. package/build/lib/method-map.d.ts.map +1 -1
  154. package/build/lib/method-map.js +0 -11
  155. package/build/lib/method-map.js.map +1 -1
  156. package/build/lib/utils.d.ts +12 -0
  157. package/build/lib/utils.d.ts.map +1 -1
  158. package/build/lib/utils.js +38 -2
  159. package/build/lib/utils.js.map +1 -1
  160. package/lib/commands/app-management.js +470 -145
  161. package/lib/commands/appearance.js +29 -36
  162. package/lib/commands/context/cache.js +29 -0
  163. package/lib/commands/context/exports.js +379 -0
  164. package/lib/commands/context/helpers.js +802 -0
  165. package/lib/commands/device/common.js +264 -0
  166. package/lib/commands/device/emulator-actions.js +194 -0
  167. package/lib/commands/device/emulator-console.js +24 -0
  168. package/lib/commands/device/utils.js +285 -0
  169. package/lib/commands/deviceidle.js +31 -44
  170. package/lib/commands/element.js +149 -142
  171. package/lib/commands/execute.js +86 -87
  172. package/lib/commands/file-actions.js +249 -222
  173. package/lib/commands/find.ts +13 -19
  174. package/lib/commands/geolocation.js +179 -0
  175. package/lib/commands/ime.js +53 -45
  176. package/lib/commands/intent.js +149 -91
  177. package/lib/commands/keyboard.js +114 -17
  178. package/lib/commands/lock/exports.js +139 -0
  179. package/lib/commands/lock/helpers.js +379 -0
  180. package/lib/commands/log.js +170 -166
  181. package/lib/commands/media-projection.js +75 -70
  182. package/lib/commands/memory.js +17 -29
  183. package/lib/commands/misc.js +94 -0
  184. package/lib/commands/network.js +209 -223
  185. package/lib/commands/performance.js +88 -73
  186. package/lib/commands/permissions.js +83 -84
  187. package/lib/commands/recordscreen.js +171 -170
  188. package/lib/commands/resources.js +96 -0
  189. package/lib/commands/shell.js +28 -42
  190. package/lib/commands/streamscreen.js +207 -206
  191. package/lib/commands/system-bars.js +76 -77
  192. package/lib/commands/time.js +36 -0
  193. package/lib/commands/touch.js +442 -346
  194. package/lib/commands/types.ts +123 -2
  195. package/lib/doctor/checks.js +24 -16
  196. package/lib/driver.ts +454 -12
  197. package/lib/index.ts +1 -13
  198. package/lib/logger.js +1 -1
  199. package/lib/method-map.js +0 -11
  200. package/lib/utils.js +40 -3
  201. package/package.json +1 -1
  202. package/build/lib/commands/actions.d.ts +0 -8
  203. package/build/lib/commands/actions.d.ts.map +0 -1
  204. package/build/lib/commands/actions.js +0 -207
  205. package/build/lib/commands/actions.js.map +0 -1
  206. package/build/lib/commands/alert.d.ts +0 -8
  207. package/build/lib/commands/alert.d.ts.map +0 -1
  208. package/build/lib/commands/alert.js +0 -29
  209. package/build/lib/commands/alert.js.map +0 -1
  210. package/build/lib/commands/context.d.ts +0 -10
  211. package/build/lib/commands/context.d.ts.map +0 -1
  212. package/build/lib/commands/context.js +0 -431
  213. package/build/lib/commands/context.js.map +0 -1
  214. package/build/lib/commands/emu-console.d.ts +0 -7
  215. package/build/lib/commands/emu-console.d.ts.map +0 -1
  216. package/build/lib/commands/emu-console.js +0 -27
  217. package/build/lib/commands/emu-console.js.map +0 -1
  218. package/build/lib/commands/general.d.ts +0 -9
  219. package/build/lib/commands/general.d.ts.map +0 -1
  220. package/build/lib/commands/general.js +0 -293
  221. package/build/lib/commands/general.js.map +0 -1
  222. package/build/lib/commands/index.d.ts +0 -28
  223. package/build/lib/commands/index.d.ts.map +0 -1
  224. package/build/lib/commands/index.js +0 -57
  225. package/build/lib/commands/index.js.map +0 -1
  226. package/build/lib/commands/mixins.d.ts +0 -747
  227. package/build/lib/commands/mixins.d.ts.map +0 -1
  228. package/build/lib/commands/mixins.js +0 -19
  229. package/build/lib/commands/mixins.js.map +0 -1
  230. package/build/lib/helpers/android.d.ts +0 -163
  231. package/build/lib/helpers/android.d.ts.map +0 -1
  232. package/build/lib/helpers/android.js +0 -818
  233. package/build/lib/helpers/android.js.map +0 -1
  234. package/build/lib/helpers/index.d.ts +0 -7
  235. package/build/lib/helpers/index.d.ts.map +0 -1
  236. package/build/lib/helpers/index.js +0 -29
  237. package/build/lib/helpers/index.js.map +0 -1
  238. package/build/lib/helpers/types.d.ts +0 -122
  239. package/build/lib/helpers/types.d.ts.map +0 -1
  240. package/build/lib/helpers/types.js +0 -3
  241. package/build/lib/helpers/types.js.map +0 -1
  242. package/build/lib/helpers/unlock.d.ts +0 -32
  243. package/build/lib/helpers/unlock.d.ts.map +0 -1
  244. package/build/lib/helpers/unlock.js +0 -273
  245. package/build/lib/helpers/unlock.js.map +0 -1
  246. package/build/lib/helpers/webview.d.ts +0 -74
  247. package/build/lib/helpers/webview.d.ts.map +0 -1
  248. package/build/lib/helpers/webview.js +0 -448
  249. package/build/lib/helpers/webview.js.map +0 -1
  250. package/lib/commands/actions.js +0 -244
  251. package/lib/commands/alert.js +0 -34
  252. package/lib/commands/context.js +0 -507
  253. package/lib/commands/emu-console.js +0 -31
  254. package/lib/commands/general.js +0 -343
  255. package/lib/commands/index.ts +0 -54
  256. package/lib/commands/mixins.ts +0 -976
  257. package/lib/helpers/android.ts +0 -1153
  258. package/lib/helpers/index.ts +0 -6
  259. package/lib/helpers/types.ts +0 -136
  260. package/lib/helpers/unlock.ts +0 -329
  261. package/lib/helpers/webview.ts +0 -610
@@ -1,507 +0,0 @@
1
- /* eslint-disable require-await */
2
- // @ts-check
3
- import {util} from '@appium/support';
4
- import Chromedriver from 'appium-chromedriver';
5
- import {errors} from 'appium/driver';
6
- import _ from 'lodash';
7
- import {
8
- APP_STATE,
9
- CHROMIUM_WIN,
10
- KNOWN_CHROME_PACKAGE_NAMES,
11
- NATIVE_WIN,
12
- WEBVIEW_BASE,
13
- WEBVIEW_WIN,
14
- WebviewHelpers,
15
- } from '../helpers';
16
- import {mixin} from './mixins';
17
- import net from 'node:net';
18
- import {findAPortNotInUse} from 'portscanner';
19
-
20
- const CHROMEDRIVER_AUTODOWNLOAD_FEATURE = 'chromedriver_autodownload';
21
-
22
- /**
23
- * @returns {Promise<number>}
24
- */
25
- async function getFreePort() {
26
- return await new Promise((resolve, reject) => {
27
- const srv = net.createServer();
28
- srv.listen(0, () => {
29
- const address = srv.address();
30
- let port;
31
- if (_.has(address, 'port')) {
32
- // @ts-ignore The above condition covers possible errors
33
- port = address.port;
34
- } else {
35
- reject(new Error('Cannot determine any free port number'));
36
- }
37
- srv.close(() => resolve(port));
38
- });
39
- });
40
- }
41
-
42
- /**
43
- * @type {import('./mixins').ContextMixin & ThisType<import('../driver').AndroidDriver>}
44
- * @satisfies {import('@appium/types').ExternalDriver}
45
- */
46
- const ContextMixin = {
47
- /* -------------------------------
48
- * Actual MJSONWP command handlers
49
- * ------------------------------- */
50
- async getCurrentContext() {
51
- // if the current context is `null`, indicating no context
52
- // explicitly set, it is the default context
53
- return this.curContext || this.defaultContextName();
54
- },
55
-
56
- async getContexts() {
57
- const webviewsMapping = await WebviewHelpers.getWebViewsMapping(this.adb, this.opts);
58
- return this.assignContexts(webviewsMapping);
59
- },
60
-
61
- async setContext(name) {
62
- if (!util.hasValue(name)) {
63
- name = this.defaultContextName();
64
- } else if (name === WEBVIEW_WIN) {
65
- // handle setContext "WEBVIEW"
66
- name = this.defaultWebviewName();
67
- }
68
- // if we're already in the context we want, do nothing
69
- if (name === this.curContext) {
70
- return;
71
- }
72
-
73
- const webviewsMapping = await WebviewHelpers.getWebViewsMapping(this.adb, this.opts);
74
- const contexts = this.assignContexts(webviewsMapping);
75
- // if the context we want doesn't exist, fail
76
- if (!_.includes(contexts, name)) {
77
- throw new errors.NoSuchContextError();
78
- }
79
-
80
- await this.switchContext(name, webviewsMapping);
81
- this.curContext = name;
82
- },
83
-
84
- async mobileGetContexts(opts = {}) {
85
- const _opts = {
86
- androidDeviceSocket: this.opts.androidDeviceSocket,
87
- ensureWebviewsHavePages: true,
88
- webviewDevtoolsPort: this.opts.webviewDevtoolsPort,
89
- enableWebviewDetailsCollection: true,
90
- waitForWebviewMs: opts.waitForWebviewMs || 0,
91
- };
92
- return await WebviewHelpers.getWebViewsMapping(/** @type {ADB} */ (this.adb), _opts);
93
- },
94
-
95
- assignContexts(webviewsMapping) {
96
- const opts = Object.assign({isChromeSession: this.isChromeSession}, this.opts);
97
- const webviews = WebviewHelpers.parseWebviewNames(webviewsMapping, opts);
98
- this.contexts = [NATIVE_WIN, ...webviews];
99
- this.log.debug(`Available contexts: ${JSON.stringify(this.contexts)}`);
100
- return this.contexts;
101
- },
102
-
103
- async switchContext(name, webviewsMapping) {
104
- // We have some options when it comes to webviews. If we want a
105
- // Chromedriver webview, we can only control one at a time.
106
- if (this.isChromedriverContext(name)) {
107
- // start proxying commands directly to chromedriver
108
- await this.startChromedriverProxy(name, webviewsMapping);
109
- } else if (this.isChromedriverContext(this.curContext)) {
110
- // if we're moving to a non-chromedriver webview, and our current context
111
- // _is_ a chromedriver webview, if caps recreateChromeDriverSessions is set
112
- // to true then kill chromedriver session using stopChromedriverProxies or
113
- // else simply suspend proxying to the latter
114
- if (this.opts.recreateChromeDriverSessions) {
115
- this.log.debug('recreateChromeDriverSessions set to true; killing existing chromedrivers');
116
- await this.stopChromedriverProxies();
117
- } else {
118
- this.suspendChromedriverProxy();
119
- }
120
- } else {
121
- throw new Error(`Didn't know how to handle switching to context '${name}'`);
122
- }
123
- },
124
-
125
- /* ---------------------------------
126
- * On-object context-related helpers
127
- * --------------------------------- */
128
-
129
- // The reason this is a function and not just a constant is that both android-
130
- // driver and selendroid-driver use this logic, and each one returns
131
- // a different default context name
132
- defaultContextName() {
133
- return NATIVE_WIN;
134
- },
135
-
136
- defaultWebviewName() {
137
- return WEBVIEW_BASE + (this.opts.autoWebviewName || this.opts.appPackage);
138
- },
139
-
140
- isWebContext() {
141
- return this.curContext !== null && this.curContext !== NATIVE_WIN;
142
- },
143
-
144
- // Turn on proxying to an existing Chromedriver session or a new one
145
- async startChromedriverProxy(context, webviewsMapping) {
146
- this.log.debug(`Connecting to chrome-backed webview context '${context}'`);
147
-
148
- let cd;
149
- if (this.sessionChromedrivers[context]) {
150
- // in the case where we've already set up a chromedriver for a context,
151
- // we want to reconnect to it, not create a whole new one
152
- this.log.debug(`Found existing Chromedriver for context '${context}'. Using it.`);
153
- cd = this.sessionChromedrivers[context];
154
- await this.setupExistingChromedriver(this.log, cd);
155
- } else {
156
- // XXX: this suppresses errors about putting arbitrary stuff on opts
157
- const opts = /** @type {any} */ (_.cloneDeep(this.opts));
158
- opts.chromeUseRunningApp = true;
159
-
160
- // if requested, tell chromedriver to attach to the android package we have
161
- // associated with the context name, rather than the package of the AUT.
162
- // And turn this on by default for chrome--if chrome pops up with a webview
163
- // and someone wants to switch to it, we should let chromedriver connect to
164
- // chrome rather than staying stuck on the AUT
165
- if (opts.extractChromeAndroidPackageFromContextName || context === `${WEBVIEW_BASE}chrome`) {
166
- let androidPackage = context.match(`${WEBVIEW_BASE}(.+)`);
167
- if (androidPackage && androidPackage.length > 0) {
168
- opts.chromeAndroidPackage = androidPackage[1];
169
- }
170
- if (!opts.extractChromeAndroidPackageFromContextName) {
171
- if (
172
- _.has(this.opts, 'enableWebviewDetailsCollection') &&
173
- !this.opts.enableWebviewDetailsCollection
174
- ) {
175
- // When enableWebviewDetailsCollection capability is explicitly disabled, try to identify
176
- // chromeAndroidPackage based on contexts, known chrome variant packages and queryAppState result
177
- // since webviewsMapping does not have info object
178
- const contexts = webviewsMapping.map((wm) => wm.webviewName);
179
- for (const knownPackage of KNOWN_CHROME_PACKAGE_NAMES) {
180
- if (_.includes(contexts, `${WEBVIEW_BASE}${knownPackage}`)) {
181
- continue;
182
- }
183
- const appState = await this.queryAppState(knownPackage);
184
- if (
185
- _.includes(
186
- [APP_STATE.RUNNING_IN_BACKGROUND, APP_STATE.RUNNING_IN_FOREGROUND],
187
- appState,
188
- )
189
- ) {
190
- opts.chromeAndroidPackage = knownPackage;
191
- this.log.debug(
192
- `Identified chromeAndroidPackage as '${opts.chromeAndroidPackage}' ` +
193
- `for context '${context}' by querying states of Chrome app packages`,
194
- );
195
- break;
196
- }
197
- }
198
- } else {
199
- for (const wm of webviewsMapping) {
200
- if (wm.webviewName === context && _.has(wm?.info, 'Android-Package')) {
201
- // XXX: should be a type guard here
202
- opts.chromeAndroidPackage =
203
- /** @type {NonNullable<import('./types').WebviewsMapping['info']>} */ (wm.info)[
204
- 'Android-Package'
205
- ];
206
- this.log.debug(
207
- `Identified chromeAndroidPackage as '${opts.chromeAndroidPackage}' ` +
208
- `for context '${context}' by CDP`,
209
- );
210
- break;
211
- }
212
- }
213
- }
214
- }
215
- }
216
-
217
- cd = await this.setupNewChromedriver(
218
- opts,
219
- /** @type {string} */ (this.adb.curDeviceId),
220
- this.adb,
221
- context,
222
- );
223
- // bind our stop/exit handler, passing in context so we know which
224
- // one stopped unexpectedly
225
- cd.on(Chromedriver.EVENT_CHANGED, (msg) => {
226
- if (msg.state === Chromedriver.STATE_STOPPED) {
227
- this.onChromedriverStop(context);
228
- }
229
- });
230
- // save the chromedriver object under the context
231
- this.sessionChromedrivers[context] = cd;
232
- }
233
- // hook up the local variables so we can proxy this biz
234
- this.chromedriver = cd;
235
- this.proxyReqRes = this.chromedriver.proxyReq.bind(this.chromedriver);
236
- this.proxyCommand = /** @type {import('@appium/types').ExternalDriver['proxyCommand']} */ (
237
- this.chromedriver.jwproxy.command.bind(this.chromedriver.jwproxy)
238
- );
239
- this.jwpProxyActive = true;
240
- },
241
-
242
- // Stop proxying to any Chromedriver
243
- suspendChromedriverProxy() {
244
- this.chromedriver = undefined;
245
- this.proxyReqRes = undefined;
246
- this.proxyCommand = undefined;
247
- this.jwpProxyActive = false;
248
- },
249
-
250
- // Handle an out-of-band Chromedriver stop event
251
- async onChromedriverStop(context) {
252
- this.log.warn(`Chromedriver for context ${context} stopped unexpectedly`);
253
- if (context === this.curContext) {
254
- // we exited unexpectedly while automating the current context and so want
255
- // to shut down the session and respond with an error
256
- let err = new Error('Chromedriver quit unexpectedly during session');
257
- await this.startUnexpectedShutdown(err);
258
- } else {
259
- // if a Chromedriver in the non-active context barfs, we don't really
260
- // care, we'll just make a new one next time we need the context.
261
- this.log.warn(
262
- "Chromedriver quit unexpectedly, but it wasn't the active " + 'context, ignoring',
263
- );
264
- delete this.sessionChromedrivers[context];
265
- }
266
- },
267
-
268
- // Intentionally stop all the chromedrivers currently active, and ignore
269
- // their exit events
270
- async stopChromedriverProxies() {
271
- this.suspendChromedriverProxy(); // make sure we turn off the proxy flag
272
- for (let context of _.keys(this.sessionChromedrivers)) {
273
- let cd = this.sessionChromedrivers[context];
274
- this.log.debug(`Stopping chromedriver for context ${context}`);
275
- // stop listening for the stopped state event
276
- cd.removeAllListeners(Chromedriver.EVENT_CHANGED);
277
- try {
278
- await cd.stop();
279
- } catch (err) {
280
- this.log.warn(`Error stopping Chromedriver: ${/** @type {Error} */ (err).message}`);
281
- }
282
- delete this.sessionChromedrivers[context];
283
- }
284
- },
285
-
286
- isChromedriverContext(viewName) {
287
- return _.includes(viewName, WEBVIEW_WIN) || viewName === CHROMIUM_WIN;
288
- },
289
-
290
- shouldDismissChromeWelcome() {
291
- return (
292
- !!this.opts.chromeOptions &&
293
- _.isArray(this.opts.chromeOptions.args) &&
294
- this.opts.chromeOptions.args.includes('--no-first-run')
295
- );
296
- },
297
-
298
- async dismissChromeWelcome() {
299
- this.log.info('Trying to dismiss Chrome welcome');
300
- let activity = await this.getCurrentActivity();
301
- if (activity !== 'org.chromium.chrome.browser.firstrun.FirstRunActivity') {
302
- this.log.info('Chrome welcome dialog never showed up! Continuing');
303
- return;
304
- }
305
- let el = await this.findElOrEls('id', 'com.android.chrome:id/terms_accept', false);
306
- await this.click(/** @type {string} */ (el.ELEMENT));
307
- try {
308
- let el = await this.findElOrEls('id', 'com.android.chrome:id/negative_button', false);
309
- await this.click(/** @type {string} */ (el.ELEMENT));
310
- } catch (e) {
311
- // DO NOTHING, THIS DEVICE DIDNT LAUNCH THE SIGNIN DIALOG
312
- // IT MUST BE A NON GMS DEVICE
313
- this.log.warn(
314
- `This device did not show Chrome SignIn dialog, ${/** @type {Error} */ (e).message}`,
315
- );
316
- }
317
- },
318
-
319
- async startChromeSession() {
320
- this.log.info('Starting a chrome-based browser session');
321
- // XXX: this suppresses errors about putting arbitrary stuff on opts
322
- const opts = /** @type {any} */ (_.cloneDeep(this.opts));
323
-
324
- const knownPackages = [
325
- 'org.chromium.chrome.shell',
326
- 'com.android.chrome',
327
- 'com.chrome.beta',
328
- 'org.chromium.chrome',
329
- 'org.chromium.webview_shell',
330
- ];
331
-
332
- if (_.includes(knownPackages, this.opts.appPackage)) {
333
- opts.chromeBundleId = this.opts.appPackage;
334
- } else {
335
- opts.chromeAndroidActivity = this.opts.appActivity;
336
- }
337
- this.chromedriver = await this.setupNewChromedriver(
338
- opts,
339
- /** @type {string} */ (this.adb.curDeviceId),
340
- this.adb,
341
- );
342
- this.chromedriver.on(Chromedriver.EVENT_CHANGED, (msg) => {
343
- if (msg.state === Chromedriver.STATE_STOPPED) {
344
- this.onChromedriverStop(CHROMIUM_WIN);
345
- }
346
- });
347
-
348
- // Now that we have a Chrome session, we ensure that the context is
349
- // appropriately set and that this chromedriver is added to the list
350
- // of session chromedrivers so we can switch back and forth
351
- this.curContext = CHROMIUM_WIN;
352
- this.sessionChromedrivers[CHROMIUM_WIN] = this.chromedriver;
353
- this.proxyReqRes = this.chromedriver.proxyReq.bind(this.chromedriver);
354
- this.proxyCommand = /** @type {import('@appium/types').ExternalDriver['proxyCommand']} */ (
355
- this.chromedriver.jwproxy.command.bind(this.chromedriver.jwproxy)
356
- );
357
- this.jwpProxyActive = true;
358
-
359
- if (this.shouldDismissChromeWelcome()) {
360
- // dismiss Chrome welcome dialog
361
- await this.dismissChromeWelcome();
362
- }
363
- },
364
-
365
- /* --------------------------
366
- * Internal library functions
367
- * -------------------------- */
368
-
369
- async setupExistingChromedriver(log, chromedriver) {
370
- // check the status by sending a simple window-based command to ChromeDriver
371
- // if there is an error, we want to recreate the ChromeDriver session
372
- if (!(await chromedriver.hasWorkingWebview())) {
373
- log.debug('ChromeDriver is not associated with a window. ' + 'Re-initializing the session.');
374
- await chromedriver.restart();
375
- }
376
- return chromedriver;
377
- },
378
-
379
- async getChromedriverPort(portSpec, log) {
380
- // if the user didn't give us any specific information about chromedriver
381
- // port ranges, just find any free port
382
- if (!portSpec) {
383
- const port = await getFreePort();
384
- log?.debug(`A port was not given, using random free port: ${port}`);
385
- return port;
386
- }
387
-
388
- // otherwise find the free port based on a list or range provided by the user
389
- log?.debug(`Finding a free port for chromedriver using spec ${JSON.stringify(portSpec)}`);
390
- let foundPort = null;
391
- for (const potentialPort of portSpec) {
392
- /** @type {number} */
393
- let port;
394
- /** @type {number} */
395
- let stopPort;
396
- if (_.isArray(potentialPort)) {
397
- [port, stopPort] = potentialPort.map((p) => parseInt(String(p), 10));
398
- } else {
399
- port = parseInt(String(potentialPort), 10); // ensure we have a number and not a string
400
- stopPort = port;
401
- }
402
- log?.debug(`Checking port range ${port}:${stopPort}`);
403
- try {
404
- foundPort = await findAPortNotInUse(port, stopPort);
405
- break;
406
- } catch (e) {
407
- log?.debug(`Nothing in port range ${port}:${stopPort} was available`);
408
- }
409
- }
410
-
411
- if (foundPort === null) {
412
- throw new Error(
413
- `Could not find a free port for chromedriver using ` +
414
- `chromedriverPorts spec ${JSON.stringify(portSpec)}`,
415
- );
416
- }
417
-
418
- log?.debug(`Using free port ${foundPort} for chromedriver`);
419
- return foundPort;
420
- },
421
-
422
- isChromedriverAutodownloadEnabled() {
423
- if (this.isFeatureEnabled(CHROMEDRIVER_AUTODOWNLOAD_FEATURE)) {
424
- return true;
425
- }
426
- this?.log?.debug(
427
- `Automated Chromedriver download is disabled. ` +
428
- `Use '${CHROMEDRIVER_AUTODOWNLOAD_FEATURE}' server feature to enable it`,
429
- );
430
- return false;
431
- },
432
-
433
- async setupNewChromedriver(opts, curDeviceId, adb, context) {
434
- // @ts-ignore TODO: Remove the legacy
435
- if (opts.chromeDriverPort) {
436
- this?.log?.warn(
437
- `The 'chromeDriverPort' capability is deprecated. Please use 'chromedriverPort' instead`,
438
- );
439
- // @ts-ignore TODO: Remove the legacy
440
- opts.chromedriverPort = opts.chromeDriverPort;
441
- }
442
-
443
- if (opts.chromedriverPort) {
444
- this?.log?.debug(`Using user-specified port ${opts.chromedriverPort} for chromedriver`);
445
- } else {
446
- // if a single port wasn't given, we'll look for a free one
447
- opts.chromedriverPort = await this.getChromedriverPort(opts.chromedriverPorts, this?.log);
448
- }
449
-
450
- const details = context ? WebviewHelpers.getWebviewDetails(adb, context) : undefined;
451
- if (!_.isEmpty(details)) {
452
- this?.log?.debug(
453
- 'Passing web view details to the Chromedriver constructor: ' +
454
- JSON.stringify(details, null, 2),
455
- );
456
- }
457
-
458
- const chromedriver = new Chromedriver({
459
- port: String(opts.chromedriverPort),
460
- executable: opts.chromedriverExecutable,
461
- // eslint-disable-next-line object-shorthand
462
- adb: /** @type {any} */ (adb),
463
- cmdArgs: /** @type {string[]} */ (opts.chromedriverArgs),
464
- verbose: !!opts.showChromedriverLog,
465
- executableDir: opts.chromedriverExecutableDir,
466
- mappingPath: opts.chromedriverChromeMappingFile,
467
- // @ts-expect-error arbitrary value on opts?
468
- bundleId: opts.chromeBundleId,
469
- useSystemExecutable: opts.chromedriverUseSystemExecutable,
470
- disableBuildCheck: opts.chromedriverDisableBuildCheck,
471
- // @ts-expect-error FIXME: chromedriver typing are probably too strict
472
- details,
473
- isAutodownloadEnabled: this?.isChromedriverAutodownloadEnabled?.(),
474
- });
475
-
476
- // make sure there are chromeOptions
477
- opts.chromeOptions = opts.chromeOptions || {};
478
- // try out any prefixed chromeOptions,
479
- // and strip the prefix
480
- for (const opt of _.keys(opts)) {
481
- if (opt.endsWith(':chromeOptions')) {
482
- this?.log?.warn(
483
- `Merging '${opt}' into 'chromeOptions'. This may cause unexpected behavior`,
484
- );
485
- _.merge(opts.chromeOptions, opts[opt]);
486
- }
487
- }
488
-
489
- const caps = /** @type {any} */ (
490
- WebviewHelpers.createChromedriverCaps(opts, curDeviceId, details)
491
- );
492
- this?.log?.debug(
493
- `Before starting chromedriver, androidPackage is '${caps.chromeOptions.androidPackage}'`,
494
- );
495
- await chromedriver.start(caps);
496
- return chromedriver;
497
- },
498
- };
499
-
500
- mixin(ContextMixin);
501
-
502
- export default ContextMixin;
503
- export const setupNewChromedriver = ContextMixin.setupNewChromedriver;
504
-
505
- /**
506
- * @typedef {import('appium-adb').ADB} ADB
507
- */
@@ -1,31 +0,0 @@
1
- // @ts-check
2
-
3
- import {mixin} from './mixins';
4
- import {errors} from 'appium/driver';
5
-
6
- const EMU_CONSOLE_FEATURE = 'emulator_console';
7
- /**
8
- * @type {import('./mixins').EmulatorConsoleMixin & ThisType<import('../driver').AndroidDriver>}
9
- * @satisfies {import('@appium/types').ExternalDriver}
10
- */
11
- const EmulatorConsoleMixin = {
12
- async mobileExecEmuConsoleCommand(opts) {
13
- this.ensureFeatureEnabled(EMU_CONSOLE_FEATURE);
14
-
15
- const {command, execTimeout, connTimeout, initTimeout} = opts;
16
-
17
- if (!command) {
18
- throw new errors.InvalidArgumentError(`The 'command' argument is mandatory`);
19
- }
20
-
21
- return await /** @type {import('appium-adb').ADB} */ (this.adb).execEmuConsoleCommand(command, {
22
- execTimeout,
23
- connTimeout,
24
- initTimeout,
25
- });
26
- },
27
- };
28
-
29
- mixin(EmulatorConsoleMixin);
30
-
31
- export default EmulatorConsoleMixin;