appium-android-driver 5.14.7 → 6.0.1

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 (210) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/build/lib/commands/actions.d.ts +6 -224
  3. package/build/lib/commands/actions.d.ts.map +1 -1
  4. package/build/lib/commands/actions.js +306 -405
  5. package/build/lib/commands/actions.js.map +1 -1
  6. package/build/lib/commands/alert.d.ts +7 -9
  7. package/build/lib/commands/alert.d.ts.map +1 -1
  8. package/build/lib/commands/alert.js +24 -18
  9. package/build/lib/commands/alert.js.map +1 -1
  10. package/build/lib/commands/app-management.d.ts +7 -313
  11. package/build/lib/commands/app-management.d.ts.map +1 -1
  12. package/build/lib/commands/app-management.js +135 -293
  13. package/build/lib/commands/app-management.js.map +1 -1
  14. package/build/lib/commands/context.d.ts +8 -92
  15. package/build/lib/commands/context.d.ts.map +1 -1
  16. package/build/lib/commands/context.js +381 -439
  17. package/build/lib/commands/context.js.map +1 -1
  18. package/build/lib/commands/element.d.ts +8 -35
  19. package/build/lib/commands/element.d.ts.map +1 -1
  20. package/build/lib/commands/element.js +153 -136
  21. package/build/lib/commands/element.js.map +1 -1
  22. package/build/lib/commands/emu-console.d.ts +6 -48
  23. package/build/lib/commands/emu-console.d.ts.map +1 -1
  24. package/build/lib/commands/emu-console.js +19 -34
  25. package/build/lib/commands/emu-console.js.map +1 -1
  26. package/build/lib/commands/execute.d.ts +6 -5
  27. package/build/lib/commands/execute.d.ts.map +1 -1
  28. package/build/lib/commands/execute.js +77 -66
  29. package/build/lib/commands/execute.js.map +1 -1
  30. package/build/lib/commands/file-actions.d.ts +7 -128
  31. package/build/lib/commands/file-actions.d.ts.map +1 -1
  32. package/build/lib/commands/file-actions.js +183 -219
  33. package/build/lib/commands/file-actions.js.map +1 -1
  34. package/build/lib/commands/find.d.ts +8 -12
  35. package/build/lib/commands/find.d.ts.map +1 -1
  36. package/build/lib/commands/find.js +19 -23
  37. package/build/lib/commands/find.js.map +1 -1
  38. package/build/lib/commands/general.d.ts +9 -132
  39. package/build/lib/commands/general.d.ts.map +1 -1
  40. package/build/lib/commands/general.js +281 -312
  41. package/build/lib/commands/general.js.map +1 -1
  42. package/build/lib/commands/ime.d.ts +7 -10
  43. package/build/lib/commands/ime.d.ts.map +1 -1
  44. package/build/lib/commands/ime.js +47 -35
  45. package/build/lib/commands/ime.js.map +1 -1
  46. package/build/lib/commands/index.d.ts +27 -2
  47. package/build/lib/commands/index.d.ts.map +1 -1
  48. package/build/lib/commands/index.js +41 -19
  49. package/build/lib/commands/index.js.map +1 -1
  50. package/build/lib/commands/intent.d.ts +7 -417
  51. package/build/lib/commands/intent.d.ts.map +1 -1
  52. package/build/lib/commands/intent.js +104 -216
  53. package/build/lib/commands/intent.js.map +1 -1
  54. package/build/lib/commands/keyboard.d.ts +6 -5
  55. package/build/lib/commands/keyboard.d.ts.map +1 -1
  56. package/build/lib/commands/keyboard.js +16 -8
  57. package/build/lib/commands/keyboard.js.map +1 -1
  58. package/build/lib/commands/log.d.ts +7 -44
  59. package/build/lib/commands/log.d.ts.map +1 -1
  60. package/build/lib/commands/log.js +146 -108
  61. package/build/lib/commands/log.js.map +1 -1
  62. package/build/lib/commands/media-projection.d.ts +7 -143
  63. package/build/lib/commands/media-projection.d.ts.map +1 -1
  64. package/build/lib/commands/media-projection.js +113 -140
  65. package/build/lib/commands/media-projection.js.map +1 -1
  66. package/build/lib/commands/mixins.d.ts +740 -0
  67. package/build/lib/commands/mixins.d.ts.map +1 -0
  68. package/build/lib/commands/mixins.js +19 -0
  69. package/build/lib/commands/mixins.js.map +1 -0
  70. package/build/lib/commands/network.d.ts +7 -138
  71. package/build/lib/commands/network.d.ts.map +1 -1
  72. package/build/lib/commands/network.js +212 -254
  73. package/build/lib/commands/network.js.map +1 -1
  74. package/build/lib/commands/performance.d.ts +24 -70
  75. package/build/lib/commands/performance.d.ts.map +1 -1
  76. package/build/lib/commands/performance.js +144 -100
  77. package/build/lib/commands/performance.js.map +1 -1
  78. package/build/lib/commands/permissions.d.ts +8 -92
  79. package/build/lib/commands/permissions.d.ts.map +1 -1
  80. package/build/lib/commands/permissions.js +75 -87
  81. package/build/lib/commands/permissions.js.map +1 -1
  82. package/build/lib/commands/recordscreen.d.ts +7 -193
  83. package/build/lib/commands/recordscreen.d.ts.map +1 -1
  84. package/build/lib/commands/recordscreen.js +151 -182
  85. package/build/lib/commands/recordscreen.js.map +1 -1
  86. package/build/lib/commands/shell.d.ts +7 -7
  87. package/build/lib/commands/shell.d.ts.map +1 -1
  88. package/build/lib/commands/shell.js +40 -33
  89. package/build/lib/commands/shell.js.map +1 -1
  90. package/build/lib/commands/streamscreen.d.ts +9 -103
  91. package/build/lib/commands/streamscreen.d.ts.map +1 -1
  92. package/build/lib/commands/streamscreen.js +261 -218
  93. package/build/lib/commands/streamscreen.js.map +1 -1
  94. package/build/lib/commands/system-bars.d.ts +22 -90
  95. package/build/lib/commands/system-bars.d.ts.map +1 -1
  96. package/build/lib/commands/system-bars.js +76 -74
  97. package/build/lib/commands/system-bars.js.map +1 -1
  98. package/build/lib/commands/touch.d.ts +10 -29
  99. package/build/lib/commands/touch.d.ts.map +1 -1
  100. package/build/lib/commands/touch.js +301 -285
  101. package/build/lib/commands/touch.js.map +1 -1
  102. package/build/lib/commands/types.d.ts +978 -0
  103. package/build/lib/commands/types.d.ts.map +1 -0
  104. package/build/lib/commands/types.js +3 -0
  105. package/build/lib/commands/types.js.map +1 -0
  106. package/build/lib/constraints.d.ts +291 -0
  107. package/build/lib/constraints.d.ts.map +1 -0
  108. package/build/lib/{desired-caps.js → constraints.js} +103 -102
  109. package/build/lib/constraints.js.map +1 -0
  110. package/build/lib/driver.d.ts +68 -37
  111. package/build/lib/driver.d.ts.map +1 -1
  112. package/build/lib/driver.js +123 -80
  113. package/build/lib/driver.js.map +1 -1
  114. package/build/lib/helpers/android.d.ts +164 -0
  115. package/build/lib/helpers/android.d.ts.map +1 -0
  116. package/build/lib/helpers/android.js +819 -0
  117. package/build/lib/helpers/android.js.map +1 -0
  118. package/build/lib/helpers/index.d.ts +7 -0
  119. package/build/lib/helpers/index.d.ts.map +1 -0
  120. package/build/lib/helpers/index.js +29 -0
  121. package/build/lib/helpers/index.js.map +1 -0
  122. package/build/lib/helpers/types.d.ts +121 -0
  123. package/build/lib/helpers/types.d.ts.map +1 -0
  124. package/build/lib/helpers/types.js +3 -0
  125. package/build/lib/helpers/types.js.map +1 -0
  126. package/build/lib/helpers/unlock.d.ts +32 -0
  127. package/build/lib/helpers/unlock.d.ts.map +1 -0
  128. package/build/lib/helpers/unlock.js +273 -0
  129. package/build/lib/helpers/unlock.js.map +1 -0
  130. package/build/lib/helpers/webview.d.ts +74 -0
  131. package/build/lib/helpers/webview.d.ts.map +1 -0
  132. package/build/lib/helpers/webview.js +421 -0
  133. package/build/lib/helpers/webview.js.map +1 -0
  134. package/build/lib/index.d.ts +9 -0
  135. package/build/lib/index.d.ts.map +1 -0
  136. package/build/lib/index.js +37 -0
  137. package/build/lib/index.js.map +1 -0
  138. package/build/lib/method-map.d.ts +0 -8
  139. package/build/lib/method-map.d.ts.map +1 -1
  140. package/build/lib/method-map.js +63 -74
  141. package/build/lib/method-map.js.map +1 -1
  142. package/build/lib/stubs.d.ts +0 -1
  143. package/build/lib/stubs.d.ts.map +1 -1
  144. package/build/lib/stubs.js +1 -0
  145. package/build/lib/stubs.js.map +1 -1
  146. package/build/lib/utils.d.ts +1 -1
  147. package/build/lib/utils.d.ts.map +1 -1
  148. package/lib/commands/actions.js +351 -464
  149. package/lib/commands/alert.js +27 -17
  150. package/lib/commands/app-management.js +156 -314
  151. package/lib/commands/context.js +457 -441
  152. package/lib/commands/element.js +201 -157
  153. package/lib/commands/emu-console.js +25 -45
  154. package/lib/commands/execute.js +106 -90
  155. package/lib/commands/file-actions.js +222 -240
  156. package/lib/commands/find.ts +103 -0
  157. package/lib/commands/general.js +327 -339
  158. package/lib/commands/ime.js +50 -34
  159. package/lib/commands/{index.js → index.ts} +20 -24
  160. package/lib/commands/intent.js +108 -249
  161. package/lib/commands/keyboard.js +20 -8
  162. package/lib/commands/log.js +172 -116
  163. package/lib/commands/media-projection.js +134 -161
  164. package/lib/commands/mixins.ts +966 -0
  165. package/lib/commands/network.js +252 -281
  166. package/lib/commands/performance.js +203 -132
  167. package/lib/commands/permissions.js +108 -109
  168. package/lib/commands/recordscreen.js +212 -209
  169. package/lib/commands/shell.js +51 -40
  170. package/lib/commands/streamscreen.js +355 -289
  171. package/lib/commands/system-bars.js +92 -83
  172. package/lib/commands/touch.js +357 -294
  173. package/lib/commands/types.ts +1097 -0
  174. package/lib/{desired-caps.js → constraints.ts} +106 -103
  175. package/lib/{driver.js → driver.ts} +278 -132
  176. package/lib/helpers/android.ts +1143 -0
  177. package/lib/helpers/index.ts +6 -0
  178. package/lib/helpers/types.ts +134 -0
  179. package/lib/helpers/unlock.ts +329 -0
  180. package/lib/helpers/webview.ts +582 -0
  181. package/lib/index.ts +18 -0
  182. package/lib/method-map.js +87 -98
  183. package/lib/stubs.ts +0 -1
  184. package/package.json +26 -19
  185. package/build/index.js +0 -51
  186. package/build/lib/android-helpers.d.ts +0 -136
  187. package/build/lib/android-helpers.d.ts.map +0 -1
  188. package/build/lib/android-helpers.js +0 -855
  189. package/build/lib/android-helpers.js.map +0 -1
  190. package/build/lib/commands/coverage.d.ts +0 -5
  191. package/build/lib/commands/coverage.d.ts.map +0 -1
  192. package/build/lib/commands/coverage.js +0 -19
  193. package/build/lib/commands/coverage.js.map +0 -1
  194. package/build/lib/desired-caps.d.ts +0 -353
  195. package/build/lib/desired-caps.d.ts.map +0 -1
  196. package/build/lib/desired-caps.js.map +0 -1
  197. package/build/lib/unlock-helpers.d.ts +0 -38
  198. package/build/lib/unlock-helpers.d.ts.map +0 -1
  199. package/build/lib/unlock-helpers.js +0 -266
  200. package/build/lib/unlock-helpers.js.map +0 -1
  201. package/build/lib/webview-helpers.d.ts +0 -224
  202. package/build/lib/webview-helpers.d.ts.map +0 -1
  203. package/build/lib/webview-helpers.js +0 -528
  204. package/build/lib/webview-helpers.js.map +0 -1
  205. package/index.js +0 -24
  206. package/lib/android-helpers.js +0 -983
  207. package/lib/commands/coverage.js +0 -18
  208. package/lib/commands/find.js +0 -82
  209. package/lib/unlock-helpers.js +0 -278
  210. package/lib/webview-helpers.js +0 -602
@@ -1,602 +0,0 @@
1
- import _ from 'lodash';
2
- import logger from './logger';
3
- import axios from 'axios';
4
- import {util} from '@appium/support';
5
- import {findAPortNotInUse} from 'portscanner';
6
- import LRU from 'lru-cache';
7
- import B from 'bluebird';
8
- import path from 'path';
9
- import os from 'os';
10
-
11
- const NATIVE_WIN = 'NATIVE_APP';
12
- const WEBVIEW_WIN = 'WEBVIEW';
13
- const CHROMIUM_WIN = 'CHROMIUM';
14
- const WEBVIEW_BASE = `${WEBVIEW_WIN}_`;
15
- const WEBVIEW_PID_PATTERN = new RegExp(`^${WEBVIEW_BASE}(\\d+)`);
16
- const WEBVIEW_PKG_PATTERN = new RegExp(`^${WEBVIEW_BASE}([^\\d\\s][\\w.]*)`);
17
- const DEVTOOLS_SOCKET_PATTERN = /@[\w.]+_devtools_remote_?([\w.]+_)?(\d+)?\b/;
18
- const CROSSWALK_SOCKET_PATTERN = /@([\w.]+)_devtools_remote\b/;
19
- const CHROMIUM_DEVTOOLS_SOCKET = 'chrome_devtools_remote';
20
- const CHROME_PACKAGE_NAME = 'com.android.chrome';
21
- const KNOWN_CHROME_PACKAGE_NAMES = [
22
- CHROME_PACKAGE_NAME,
23
- 'com.chrome.beta',
24
- 'com.chrome.dev',
25
- 'com.chrome.canary',
26
- ];
27
- const DEVTOOLS_PORTS_RANGE = [10900, 11000];
28
- const WEBVIEWS_DETAILS_CACHE = new LRU({
29
- max: 100,
30
- updateAgeOnGet: true,
31
- });
32
- const CDP_REQ_TIMEOUT = 2000; // ms
33
- const DEVTOOLS_PORT_ALLOCATION_GUARD = util.getLockFileGuard(
34
- path.resolve(os.tmpdir(), 'android_devtools_port_guard'),
35
- {timeout: 7, tryRecovery: true}
36
- );
37
-
38
- const helpers = {};
39
-
40
- function toDetailsCacheKey(adb, webview) {
41
- return `${adb?.curDeviceId}:${webview}`;
42
- }
43
-
44
- /**
45
- * This function gets a list of android system processes and returns ones
46
- * that look like webviews
47
- * See https://cs.chromium.org/chromium/src/chrome/browser/devtools/device/android_device_info_query.cc
48
- * for more details
49
- *
50
- * @param {object} adb - an ADB instance
51
- *
52
- * @return {Array.<string>} - a list of matching webview socket names (including the leading '@')
53
- */
54
- async function getPotentialWebviewProcs(adb) {
55
- const out = await adb.shell(['cat', '/proc/net/unix']);
56
- const names = [];
57
- const allMatches = [];
58
- for (const line of out.split('\n')) {
59
- // Num RefCount Protocol Flags Type St Inode Path
60
- const [, , , flags, , st, , sockPath] = line.trim().split(/\s+/);
61
- if (!sockPath) {
62
- continue;
63
- }
64
- if (sockPath.startsWith('@')) {
65
- allMatches.push(line.trim());
66
- }
67
- if (flags !== '00010000' || st !== '01') {
68
- continue;
69
- }
70
- if (!DEVTOOLS_SOCKET_PATTERN.test(sockPath)) {
71
- continue;
72
- }
73
-
74
- names.push(sockPath);
75
- }
76
- if (_.isEmpty(names)) {
77
- logger.debug('Found no active devtools sockets');
78
- if (!_.isEmpty(allMatches)) {
79
- logger.debug(`Other sockets are: ${JSON.stringify(allMatches, null, 2)}`);
80
- }
81
- } else {
82
- logger.debug(
83
- `Parsed ${names.length} active devtools ${util.pluralize('socket', names.length, false)}: ` +
84
- JSON.stringify(names)
85
- );
86
- }
87
- // sometimes the webview process shows up multiple times per app
88
- return _.uniq(names);
89
- }
90
-
91
- /**
92
- * @typedef {Object} WebviewProc
93
- * @property {string} proc - The webview process name (as returned by
94
- * getPotentialWebviewProcs
95
- * @property {string} webview - The actual webview context name
96
- */
97
- /**
98
- * This function retrieves a list of system processes that look like webviews,
99
- * and returns them along with the webview context name appropriate for it.
100
- * If we pass in a deviceSocket, we only attempt to find webviews which match
101
- * that socket name (this is for apps which embed Chromium, which isn't the
102
- * same as chrome-backed webviews).
103
- *
104
- * @param {object} adb - an ADB instance
105
- * @param {?string} deviceSocket - the explictly-named device socket to use
106
- *
107
- * @return {Array.<WebviewProc>}
108
- */
109
- async function webviewsFromProcs(adb, deviceSocket = null) {
110
- const socketNames = await getPotentialWebviewProcs(adb);
111
- const webviews = [];
112
- for (const socketName of socketNames) {
113
- if (deviceSocket === CHROMIUM_DEVTOOLS_SOCKET && socketName === `@${deviceSocket}`) {
114
- webviews.push({
115
- proc: socketName,
116
- webview: CHROMIUM_WIN,
117
- });
118
- continue;
119
- }
120
-
121
- const socketNameMatch = DEVTOOLS_SOCKET_PATTERN.exec(socketName);
122
- if (!socketNameMatch) {
123
- continue;
124
- }
125
- const matchedSocketName = socketNameMatch[2];
126
- const crosswalkMatch = CROSSWALK_SOCKET_PATTERN.exec(socketName);
127
- if (!matchedSocketName && !crosswalkMatch) {
128
- continue;
129
- }
130
-
131
- if ((deviceSocket && socketName === `@${deviceSocket}`) || !deviceSocket) {
132
- webviews.push({
133
- proc: socketName,
134
- webview: matchedSocketName
135
- ? `${WEBVIEW_BASE}${matchedSocketName}`
136
- : `${WEBVIEW_BASE}${crosswalkMatch[1]}`,
137
- });
138
- }
139
- }
140
- return webviews;
141
- }
142
-
143
- /**
144
- * Allocates a local port for devtools communication
145
- *
146
- * @param {ADB} adb ADB instance
147
- * @param {string} socketName The remote Unix socket name
148
- * @param {?number} webviewDevtoolsPort The local port number or null to apply
149
- * autodetection
150
- * @returns {number} The local port number if the remote socket has been forwarded
151
- * successfully or `null` otherwise
152
- * @throws {Error} If there was an error while allocating the local port
153
- */
154
- async function allocateDevtoolsPort(adb, socketName, webviewDevtoolsPort = null) {
155
- // socket names come with '@', but this should not be a part of the abstract
156
- // remote port, so remove it
157
- const remotePort = socketName.replace(/^@/, '');
158
- let [startPort, endPort] = DEVTOOLS_PORTS_RANGE;
159
- if (webviewDevtoolsPort) {
160
- endPort = webviewDevtoolsPort + (endPort - startPort);
161
- startPort = webviewDevtoolsPort;
162
- }
163
- logger.debug(
164
- `Forwarding remote port ${remotePort} to a local ` + `port in range ${startPort}..${endPort}`
165
- );
166
- if (!webviewDevtoolsPort) {
167
- logger.debug(
168
- `You could use the 'webviewDevtoolsPort' capability to customize ` +
169
- `the starting port number`
170
- );
171
- }
172
- return await DEVTOOLS_PORT_ALLOCATION_GUARD(async () => {
173
- let localPort;
174
- try {
175
- localPort = await findAPortNotInUse(startPort, endPort);
176
- } catch (e) {
177
- throw new Error(
178
- `Cannot find any free port to forward the Devtools socket ` +
179
- `in range ${startPort}..${endPort}. You could set the starting port number ` +
180
- `manually by providing the 'webviewDevtoolsPort' capability`
181
- );
182
- }
183
- await adb.adbExec(['forward', `tcp:${localPort}`, `localabstract:${remotePort}`]);
184
- return localPort;
185
- });
186
- }
187
-
188
- /**
189
- * @typedef {Object} WebviewProps
190
- * @property {string} proc The name of the Devtools Unix socket
191
- * @property {string} webview The web view alias. Looks like `WEBVIEW_`
192
- * prefix plus PID or package name
193
- * @property {?Object} info Webview information as it is retrieved by
194
- * /json/version CDP endpoint
195
- * @property {?Array<Object>} pages Webview pages list as it is retrieved by
196
- * /json/list CDP endpoint
197
- */
198
-
199
- /**
200
- * @typedef {Object} DetailCollectionOptions
201
- * @property {?string|number} webviewDevtoolsPort The starting port to use for webview page
202
- * presence check (if not the default of 9222).
203
- * @property {?boolean} ensureWebviewsHavePages Whether to check for webview
204
- * pages presence
205
- * @property {boolean} enableWebviewDetailsCollection Whether to collect
206
- * web view details and send them to Chromedriver constructor, so it could
207
- * select a binary more precisely based on this info.
208
- */
209
-
210
- /**
211
- * This is a wrapper for Chrome Debugger Protocol data collection.
212
- * No error is thrown if CDP request fails - in such case no data will be
213
- * recorded into the corresponding `webviewsMapping` item.
214
- *
215
- * @param {ADB} adb The ADB instance
216
- * @param {Array<WebviewProps>} webviewsMapping The current webviews mapping
217
- * !!! Each item of this array gets mutated (`info`/`pages` properties get added
218
- * based on the provided `opts`) if the requested details have been
219
- * successfully retrieved for it !!!
220
- * @param {DetailCollectionOptions} opts If both `ensureWebviewsHavePages` and
221
- * `enableWebviewDetailsCollection` properties are falsy then no details collection
222
- * is performed
223
- */
224
- async function collectWebviewsDetails(adb, webviewsMapping, opts = {}) {
225
- if (_.isEmpty(webviewsMapping)) {
226
- return;
227
- }
228
-
229
- const {
230
- webviewDevtoolsPort = null,
231
- ensureWebviewsHavePages = null,
232
- enableWebviewDetailsCollection = null,
233
- } = opts;
234
-
235
- if (!ensureWebviewsHavePages) {
236
- logger.info(
237
- `Not checking whether webviews have active pages; use the ` +
238
- `'ensureWebviewsHavePages' cap to turn this check on`
239
- );
240
- }
241
-
242
- if (!enableWebviewDetailsCollection) {
243
- logger.info(
244
- `Not collecting web view details. Details collection might help ` +
245
- `to make Chromedriver initialization more precise. Use the 'enableWebviewDetailsCollection' ` +
246
- `cap to turn it on`
247
- );
248
- }
249
-
250
- if (!ensureWebviewsHavePages && !enableWebviewDetailsCollection) {
251
- return;
252
- }
253
-
254
- // Connect to each devtools socket and retrieve web view details
255
- logger.debug(`Collecting CDP data of ${util.pluralize('webview', webviewsMapping.length, true)}`);
256
- const detailCollectors = [];
257
- for (const item of webviewsMapping) {
258
- detailCollectors.push(
259
- (async () => {
260
- let localPort;
261
- try {
262
- localPort = await allocateDevtoolsPort(adb, item.proc, webviewDevtoolsPort);
263
- if (enableWebviewDetailsCollection) {
264
- item.info = await cdpInfo(localPort);
265
- }
266
- if (ensureWebviewsHavePages) {
267
- item.pages = await cdpList(localPort);
268
- }
269
- } catch (e) {
270
- logger.debug(e);
271
- } finally {
272
- if (localPort) {
273
- try {
274
- await adb.removePortForward(localPort);
275
- } catch (e) {
276
- logger.debug(e);
277
- }
278
- }
279
- }
280
- })()
281
- );
282
- }
283
- await B.all(detailCollectors);
284
- logger.debug(`CDP data collection completed`);
285
- }
286
-
287
- // https://chromedevtools.github.io/devtools-protocol/
288
- async function cdpList(localPort) {
289
- return (
290
- await axios({
291
- url: `http://127.0.0.1:${localPort}/json/list`,
292
- timeout: CDP_REQ_TIMEOUT,
293
- })
294
- ).data;
295
- }
296
-
297
- // https://chromedevtools.github.io/devtools-protocol/
298
- async function cdpInfo(localPort) {
299
- return (
300
- await axios({
301
- url: `http://127.0.0.1:${localPort}/json/version`,
302
- timeout: CDP_REQ_TIMEOUT,
303
- })
304
- ).data;
305
- }
306
-
307
- /**
308
- * Take a webview name like WEBVIEW_4296 and use 'adb shell ps' to figure out
309
- * which app package is associated with that webview. One of the reasons we
310
- * want to do this is to make sure we're listing webviews for the actual AUT,
311
- * not some other running app
312
- *
313
- * @param {object} adb - an ADB instance
314
- * @param {string} webview - a webview process name
315
- *
316
- * @returns {string} - the package name of the app running the webview
317
- * @throws {Error} If there was a failure while retrieving the process name
318
- */
319
- helpers.procFromWebview = async function procFromWebview(adb, webview) {
320
- const pidMatch = WEBVIEW_PID_PATTERN.exec(webview);
321
- if (!pidMatch) {
322
- throw new Error(`Could not find PID for webview '${webview}'`);
323
- }
324
-
325
- const pid = pidMatch[1];
326
- logger.debug(`${webview} mapped to pid ${pid}`);
327
- logger.debug(`Getting process name for webview '${webview}'`);
328
- const pkg = await adb.getNameByPid(pid);
329
- logger.debug(`Got process name: '${pkg}'`);
330
- return pkg;
331
- };
332
-
333
- /**
334
- * Parse webview names for getContexts
335
- *
336
- * @param {Array<WebviewsMapping>} webviewsMapping See note on getWebViewsMapping
337
- * @param {GetWebviewsOpts} opts See note on getWebViewsMapping
338
- * @return {Array.<string>} - a list of webview names
339
- */
340
- helpers.parseWebviewNames = function parseWebviewNames(
341
- webviewsMapping,
342
- {ensureWebviewsHavePages = true, isChromeSession = false} = {}
343
- ) {
344
- if (isChromeSession) {
345
- return [CHROMIUM_WIN];
346
- }
347
-
348
- const result = [];
349
- for (const {webview, pages, proc, webviewName} of webviewsMapping) {
350
- if (ensureWebviewsHavePages && pages?.length === 0) {
351
- logger.info(
352
- `Skipping the webview '${webview}' at '${proc}' ` +
353
- `since it has reported having zero pages`
354
- );
355
- continue;
356
- }
357
- if (webviewName) {
358
- result.push(webviewName);
359
- }
360
- }
361
- logger.debug(
362
- `Found ${util.pluralize('webview', result.length, true)}: ${JSON.stringify(result)}`
363
- );
364
- return result;
365
- };
366
-
367
- /**
368
- * @typedef {Object} GetWebviewsOpts
369
- * @property {string} androidDeviceSocket [null] - device socket name
370
- * @property {boolean} ensureWebviewsHavePages [true] - whether to check for webview
371
- * page presence
372
- * @property {number} webviewDevtoolsPort [9222] - port to use for webview page
373
- * presence check.
374
- * @property {boolean} enableWebviewDetailsCollection [true] - whether to collect
375
- * web view details and send them to Chromedriver constructor, so it could
376
- * select a binary more precisely based on this info.
377
- */
378
-
379
- /**
380
- * @typedef {Object} WebviewsMapping
381
- * @property {string} proc See note on WebviewProps
382
- * @property {string} webview See note on WebviewProps
383
- * @property {?Object} info See note on WebviewProps
384
- * @property {?Array<Object>} pages See note on WebviewProps
385
- * @propery {?string} webviewName An actual webview name for switching context
386
- */
387
-
388
- /**
389
- * Get a list of available webviews mapping by introspecting processes with adb,
390
- * where webviews are listed. It's possible to pass in a 'deviceSocket' arg, which
391
- * limits the webview possibilities to the one running on the Chromium devtools
392
- * socket we're interested in (see note on webviewsFromProcs). We can also
393
- * direct this method to verify whether a particular webview process actually
394
- * has any pages (if a process exists but no pages are found, Chromedriver will
395
- * not actually be able to connect to it, so this serves as a guard for that
396
- * strange failure mode). The strategy for checking whether any pages are
397
- * active involves sending a request to the remote debug server on the device,
398
- * hence it is also possible to specify the port on the host machine which
399
- * should be used for this communication.
400
- *
401
- * @param {object} adb - an ADB instance
402
- * @param {GetWebviewsOpts} opts
403
- *
404
- * @return {Array<WebviewsMapping>} webviewsMapping
405
- */
406
- helpers.getWebViewsMapping = async function getWebViewsMapping(
407
- adb,
408
- {
409
- androidDeviceSocket = null,
410
- ensureWebviewsHavePages = true,
411
- webviewDevtoolsPort = null,
412
- enableWebviewDetailsCollection = true,
413
- } = {}
414
- ) {
415
- logger.debug('Getting a list of available webviews');
416
- const webviewsMapping = await webviewsFromProcs(adb, androidDeviceSocket);
417
-
418
- await collectWebviewsDetails(adb, webviewsMapping, {
419
- ensureWebviewsHavePages,
420
- enableWebviewDetailsCollection,
421
- webviewDevtoolsPort,
422
- });
423
-
424
- for (const webviewMapping of webviewsMapping) {
425
- const {webview, info} = webviewMapping;
426
- webviewMapping.webviewName = null;
427
-
428
- let wvName = webview;
429
- let process = undefined;
430
- if (!androidDeviceSocket) {
431
- const pkgMatch = WEBVIEW_PKG_PATTERN.exec(webview);
432
- try {
433
- // web view name could either be suffixed with PID or the package name
434
- // package names could not start with a digit
435
- const pkg = pkgMatch ? pkgMatch[1] : await helpers.procFromWebview(adb, webview);
436
- wvName = `${WEBVIEW_BASE}${pkg}`;
437
- const pidMatch = WEBVIEW_PID_PATTERN.exec(webview);
438
- process = {
439
- name: pkg,
440
- id: pidMatch ? pidMatch[1] : null,
441
- };
442
- } catch (e) {
443
- logger.warn(e.message);
444
- continue;
445
- }
446
- }
447
-
448
- webviewMapping.webviewName = wvName;
449
- const key = toDetailsCacheKey(adb, wvName);
450
- if (info || process) {
451
- WEBVIEWS_DETAILS_CACHE.set(key, {info, process});
452
- } else if (WEBVIEWS_DETAILS_CACHE.has(key)) {
453
- WEBVIEWS_DETAILS_CACHE.delete(key);
454
- }
455
- }
456
- return webviewsMapping;
457
- };
458
-
459
- /**
460
- * @typedef {Object} ProcessInfo
461
- * @property {string} name The process name
462
- * @property {?string} id The process id (if could be retrieved)
463
- */
464
-
465
- /**
466
- * @typedef {Object} WebViewDetails
467
- * @property {?ProcessInfo} process - Web view process details
468
- * @property {Object} info - Web view details as returned by /json/version CDP endpoint, for example:
469
- * {
470
- * "Browser": "Chrome/72.0.3601.0",
471
- * "Protocol-Version": "1.3",
472
- * "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3601.0 Safari/537.36",
473
- * "V8-Version": "7.2.233",
474
- * "WebKit-Version": "537.36 (@cfede9db1d154de0468cb0538479f34c0755a0f4)",
475
- * "webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/b0b8a4fb-bb17-4359-9533-a8d9f3908bd8"
476
- * }
477
- */
478
-
479
- /**
480
- * Retrieves web view details previously cached by `getWebviews` call
481
- *
482
- * @param {ADB} adb ADB instance
483
- * @param {string} webview The name of the web view
484
- * @returns {?WebViewDetails} Either `undefined` or the recent web view details
485
- */
486
- helpers.getWebviewDetails = function getWebviewDetails(adb, webview) {
487
- const key = toDetailsCacheKey(adb, webview);
488
- return WEBVIEWS_DETAILS_CACHE.get(key);
489
- };
490
-
491
- /**
492
- * Create Chrome driver capabilities based on the provided
493
- * Appium capabilities
494
- *
495
- * @param {Object} opts User-provided capabilities object
496
- * @param {string} deviceId The identifier of the Android device under test
497
- * @param {?WebViewDetails} webViewDetails
498
- * @returns {Object} The capabilities object.
499
- * See https://chromedriver.chromium.org/capabilities for more details.
500
- */
501
- helpers.createChromedriverCaps = function createChromedriverCaps(opts, deviceId, webViewDetails) {
502
- const caps = {chromeOptions: {}};
503
-
504
- const androidPackage =
505
- opts.chromeOptions?.androidPackage ||
506
- opts.appPackage ||
507
- webViewDetails?.info?.['Android-Package'];
508
- if (androidPackage) {
509
- // chromedriver raises an invalid argument error when androidPackage is 'null'
510
- caps.chromeOptions.androidPackage = androidPackage;
511
- }
512
- if (_.isBoolean(opts.chromeUseRunningApp)) {
513
- caps.chromeOptions.androidUseRunningApp = opts.chromeUseRunningApp;
514
- }
515
- if (opts.chromeAndroidPackage) {
516
- caps.chromeOptions.androidPackage = opts.chromeAndroidPackage;
517
- }
518
- if (opts.chromeAndroidActivity) {
519
- caps.chromeOptions.androidActivity = opts.chromeAndroidActivity;
520
- }
521
- if (opts.chromeAndroidProcess) {
522
- caps.chromeOptions.androidProcess = opts.chromeAndroidProcess;
523
- } else if (webViewDetails?.process?.name && webViewDetails?.process?.id) {
524
- caps.chromeOptions.androidProcess = webViewDetails.process.name;
525
- }
526
- if (_.toLower(opts.browserName) === 'chromium-webview') {
527
- caps.chromeOptions.androidActivity = opts.appActivity;
528
- }
529
- if (opts.pageLoadStrategy) {
530
- caps.pageLoadStrategy = opts.pageLoadStrategy;
531
- }
532
- const isChrome = _.toLower(caps.chromeOptions.androidPackage) === 'chrome';
533
- if (_.includes(KNOWN_CHROME_PACKAGE_NAMES, caps.chromeOptions.androidPackage) || isChrome) {
534
- // if we have extracted package from context name, it could come in as bare
535
- // "chrome", and so we should make sure the details are correct, including
536
- // not using an activity or process id
537
- if (isChrome) {
538
- caps.chromeOptions.androidPackage = CHROME_PACKAGE_NAME;
539
- }
540
- delete caps.chromeOptions.androidActivity;
541
- delete caps.chromeOptions.androidProcess;
542
- }
543
- // add device id from adb
544
- caps.chromeOptions.androidDeviceSerial = deviceId;
545
-
546
- if (_.isPlainObject(opts.loggingPrefs) || _.isPlainObject(opts.chromeLoggingPrefs)) {
547
- if (opts.loggingPrefs) {
548
- logger.warn(`The 'loggingPrefs' cap is deprecated; use the 'chromeLoggingPrefs' cap instead`);
549
- }
550
- caps.loggingPrefs = opts.chromeLoggingPrefs || opts.loggingPrefs;
551
- }
552
- if (opts.enablePerformanceLogging) {
553
- logger.warn(
554
- `The 'enablePerformanceLogging' cap is deprecated; simply use ` +
555
- `the 'chromeLoggingPrefs' cap instead, with a 'performance' key set to 'ALL'`
556
- );
557
- const newPref = {performance: 'ALL'};
558
- // don't overwrite other logging prefs that have been sent in if they exist
559
- caps.loggingPrefs = caps.loggingPrefs ? Object.assign({}, caps.loggingPrefs, newPref) : newPref;
560
- }
561
-
562
- if (opts.chromeOptions?.Arguments) {
563
- // merge `Arguments` and `args`
564
- opts.chromeOptions.args = [...(opts.chromeOptions.args || []), ...opts.chromeOptions.Arguments];
565
- delete opts.chromeOptions.Arguments;
566
- }
567
-
568
- logger.debug(
569
- 'Precalculated Chromedriver capabilities: ' + JSON.stringify(caps.chromeOptions, null, 2)
570
- );
571
-
572
- const protectedCapNames = [];
573
- for (const [opt, val] of _.toPairs(opts.chromeOptions)) {
574
- if (_.isUndefined(caps.chromeOptions[opt])) {
575
- caps.chromeOptions[opt] = val;
576
- } else {
577
- protectedCapNames.push(opt);
578
- }
579
- }
580
- if (!_.isEmpty(protectedCapNames)) {
581
- logger.info(
582
- 'The following Chromedriver capabilities cannot be overridden ' +
583
- 'by the provided chromeOptions:'
584
- );
585
- for (const optName of protectedCapNames) {
586
- logger.info(` ${optName} (${JSON.stringify(opts.chromeOptions[optName])})`);
587
- }
588
- }
589
-
590
- return caps;
591
- };
592
-
593
- export default helpers;
594
- export {
595
- helpers,
596
- NATIVE_WIN,
597
- WEBVIEW_WIN,
598
- WEBVIEW_BASE,
599
- CHROMIUM_WIN,
600
- KNOWN_CHROME_PACKAGE_NAMES,
601
- DEVTOOLS_SOCKET_PATTERN,
602
- };