appium-android-driver 8.4.0 → 9.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.
@@ -1,509 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
-
3
- import {util} from '@appium/support';
4
- import {errors, isErrorType} from 'appium/driver';
5
- import {asyncmap} from 'asyncbox';
6
- import B from 'bluebird';
7
- import _ from 'lodash';
8
-
9
- /**
10
- * @deprecated
11
- * @this {import('../driver').AndroidDriver}
12
- * @param {string?} [elementId=null]
13
- * @param {number?} [x=null]
14
- * @param {number?} [y=null]
15
- * @param {number} [count=1]
16
- * @returns {Promise<void>}
17
- */
18
- export async function tap(elementId = null, x = null, y = null, count = 1) {
19
- throw new errors.NotImplementedError('Not implemented');
20
- }
21
-
22
- /**
23
- * @deprecated
24
- * @this {import('../driver').AndroidDriver}
25
- * @param {string} elementId
26
- * @param {number} x
27
- * @param {number} y
28
- * @param {number} duration
29
- * @returns {Promise<void>}
30
- */
31
- export async function touchLongClick(elementId, x, y, duration) {
32
- throw new errors.NotImplementedError('Not implemented');
33
- }
34
-
35
- /**
36
- * @deprecated
37
- * @this {import('../driver').AndroidDriver}
38
- * @param {string} elementId
39
- * @param {number} x
40
- * @param {number} y
41
- * @returns {Promise<void>}
42
- */
43
- export async function touchDown(elementId, x, y) {
44
- throw new errors.NotImplementedError('Not implemented');
45
- }
46
-
47
- /**
48
- * @deprecated
49
- * @this {import('../driver').AndroidDriver}
50
- * @param {string} elementId
51
- * @param {number} x
52
- * @param {number} y
53
- * @returns {Promise<void>}
54
- */
55
- export async function touchUp(elementId, x, y) {
56
- throw new errors.NotImplementedError('Not implemented');
57
- }
58
-
59
- /**
60
- * @deprecated
61
- * @this {import('../driver').AndroidDriver}
62
- * @param {string} elementId
63
- * @param {number} x
64
- * @param {number} y
65
- * @returns {Promise<void>}
66
- */
67
- export async function touchMove(elementId, x, y) {
68
- throw new errors.NotImplementedError('Not implemented');
69
- }
70
-
71
- /**
72
- * @deprecated
73
- * @this {import('../driver').AndroidDriver}
74
- * @param {import('./types').SwipeOpts} opts
75
- * @returns {Promise<void>}
76
- */
77
- export async function doSwipe(opts) {
78
- throw new errors.NotImplementedError('Not implemented');
79
- }
80
-
81
- /**
82
- * @deprecated
83
- * @this {import('../driver').AndroidDriver}
84
- * @param {import('./types').TouchDragAction} opts
85
- * @returns {Promise<void>}
86
- */
87
- export async function doTouchDrag(opts) {
88
- throw new errors.NotImplementedError('Not implemented');
89
- }
90
-
91
- /**
92
- * @deprecated
93
- * @this {import('../driver').AndroidDriver}
94
- * @param {import('./types').TouchActionKind} action
95
- * @param {import('./types').TouchActionOpts} [opts={}]
96
- * @returns {Promise<void>}
97
- */
98
- export async function doTouchAction(action, opts = {}) {
99
- const {element, x, y, count, ms, duration} = opts;
100
- // parseTouch precalculates absolute element positions
101
- // so there is no need to pass `element` to the affected gestures
102
- switch (action) {
103
- case 'tap':
104
- return await this.tap('', x, y, count);
105
- case 'press':
106
- return await this.touchDown('', /** @type {number} */ (x), /** @type {number} */ (y));
107
- case 'release':
108
- return await this.touchUp(
109
- String(element),
110
- /** @type {number} */ (x),
111
- /** @type {number} */ (y),
112
- );
113
- case 'moveTo':
114
- return await this.touchMove('', /** @type {number} */ (x), /** @type {number} */ (y));
115
- case 'wait':
116
- return await B.delay(/** @type {number} */ (ms));
117
- case 'longPress':
118
- return await this.touchLongClick(
119
- '',
120
- /** @type {number} */ (x),
121
- /** @type {number} */ (y),
122
- duration ?? 1000,
123
- );
124
- case 'cancel':
125
- // TODO: clarify behavior of 'cancel' action and fix this
126
- this.log.warn('Cancel action currently has no effect');
127
- break;
128
- default:
129
- this.log.errorAndThrow(`unknown action ${action}`);
130
- }
131
- }
132
-
133
- /**
134
- * @deprecated
135
- * @this {import('../driver').AndroidDriver}
136
- * @param {import('./types').TouchAction[]} gestures
137
- * @returns {Promise<void>}
138
- */
139
- export async function performTouch(gestures) {
140
- // press-wait-moveTo-release is `swipe`, so use native method
141
- if (
142
- gestures.length === 4 &&
143
- gestures[0].action === 'press' &&
144
- gestures[1].action === 'wait' &&
145
- gestures[2].action === 'moveTo' &&
146
- gestures[3].action === 'release'
147
- ) {
148
- let swipeOpts = await getSwipeOptions.bind(this)(
149
- /** @type {import('./types').SwipeAction} */ (gestures),
150
- );
151
- return await this.doSwipe(swipeOpts);
152
- }
153
- let actions = /** @type {(import('./types').TouchActionKind|TouchAction)[]} */ (
154
- _.map(gestures, 'action')
155
- );
156
-
157
- if (actions[0] === 'longPress' && actions[1] === 'moveTo' && actions[2] === 'release') {
158
- // some things are special
159
- return await this.doTouchDrag(/** @type {import('./types').TouchDragAction} */ (gestures));
160
- } else {
161
- if (actions.length === 2) {
162
- // `press` without a wait is too slow and gets interpretted as a `longPress`
163
- if (_.head(actions) === 'press' && _.last(actions) === 'release') {
164
- actions[0] = 'tap';
165
- gestures[0].action = 'tap';
166
- }
167
-
168
- // the `longPress` and `tap` methods release on their own
169
- if (
170
- (_.head(actions) === 'tap' || _.head(actions) === 'longPress') &&
171
- _.last(actions) === 'release'
172
- ) {
173
- gestures.pop();
174
- actions.pop();
175
- }
176
- } else {
177
- // longpress followed by anything other than release should become a press and wait
178
- if (actions[0] === 'longPress') {
179
- actions = ['press', 'wait', ...actions.slice(1)];
180
-
181
- let press = /** @type {NonReleaseTouchAction} */ (gestures.shift());
182
- press.action = 'press';
183
- /** @type {NonReleaseTouchAction} */
184
- let wait = {
185
- action: 'wait',
186
- options: {ms: press.options.duration || 1000},
187
- };
188
- delete press.options.duration;
189
- gestures = [press, wait, ...gestures];
190
- }
191
- }
192
-
193
- let fixedGestures = await parseTouch.bind(this)(gestures, false);
194
- // fix release action then perform all actions
195
- if (actions[actions.length - 1] === 'release') {
196
- actions[actions.length - 1] = /** @type {TouchAction} */ (
197
- await fixRelease.bind(this)(gestures)
198
- );
199
- }
200
- for (let g of fixedGestures) {
201
- await performGesture.bind(this)(g);
202
- }
203
- }
204
- }
205
-
206
- /**
207
- * @deprecated
208
- * @this {import('../driver').AndroidDriver}
209
- * @param {import('./types').TouchAction[]} actions
210
- * @param {string} elementId
211
- * @returns {Promise<void>}
212
- */
213
- export async function performMultiAction(actions, elementId) {
214
- // Android needs at least two actions to be able to perform a multi pointer gesture
215
- if (actions.length === 1) {
216
- throw new Error(
217
- 'Multi Pointer Gestures need at least two actions. ' +
218
- 'Use Touch Actions for a single action.',
219
- );
220
- }
221
-
222
- const states = await asyncmap(
223
- actions,
224
- async (action) => await parseTouch.bind(this)(action, true),
225
- false,
226
- );
227
-
228
- return await this.doPerformMultiAction(elementId, states);
229
- }
230
-
231
- /**
232
- * @deprecated
233
- * @this {import('../driver').AndroidDriver}
234
- * @param {string} elementId
235
- * @param {import('./types').TouchState[]} states
236
- * @returns {Promise<void>}
237
- */
238
- export async function doPerformMultiAction(elementId, states) {
239
- throw new errors.NotImplementedError('Not implemented');
240
- }
241
-
242
- // #region Internal helpers
243
-
244
- /**
245
- * @deprecated
246
- * @param {number|null} [val]
247
- * @returns {number}
248
- */
249
- function getCoordDefault(val) {
250
- // going the long way and checking for undefined and null since
251
- // we can't be assured `elId` is a string and not an int. Same
252
- // thing with destElement below.
253
- return util.hasValue(val) ? val : 0.5;
254
- }
255
-
256
- /**
257
- * @deprecated
258
- * @param {number} number
259
- * @param {number} digits
260
- * @returns {number}
261
- */
262
- export function truncateDecimals(number, digits) {
263
- const multiplier = Math.pow(10, digits),
264
- adjustedNum = number * multiplier,
265
- truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);
266
-
267
- return truncatedNum / multiplier;
268
- }
269
-
270
- /**
271
- * @deprecated
272
- * @param {NonReleaseTouchAction} waitGesture
273
- * @returns {number}
274
- */
275
- function getSwipeTouchDuration(waitGesture) {
276
- // the touch action api uses ms, we want seconds
277
- // 0.8 is the default time for the operation
278
- let duration = 0.8;
279
- if (typeof waitGesture.options.ms !== 'undefined' && waitGesture.options.ms) {
280
- duration = waitGesture.options.ms / 1000;
281
- if (duration === 0) {
282
- // set to a very low number, since they wanted it fast
283
- // but below 0.1 becomes 0 steps, which causes errors
284
- duration = 0.1;
285
- }
286
- }
287
- return duration;
288
- }
289
-
290
- /**
291
- * @deprecated
292
- * @this {import('../driver').AndroidDriver}
293
- * @param {import('./types').SwipeAction} gestures
294
- * @param {number} [touchCount=1]
295
- * @returns {Promise<import('./types').TouchSwipeOpts>}
296
- */
297
- async function getSwipeOptions(gestures, touchCount = 1) {
298
- let startX = getCoordDefault(gestures[0].options.x),
299
- startY = getCoordDefault(gestures[0].options.y),
300
- endX = getCoordDefault(gestures[2].options.x),
301
- endY = getCoordDefault(gestures[2].options.y),
302
- duration = getSwipeTouchDuration(gestures[1]),
303
- element = /** @type {string} */ (gestures[0].options.element),
304
- destElement = gestures[2].options.element || gestures[0].options.element;
305
-
306
- // there's no destination element handling in bootstrap and since it applies to all platforms, we handle it here
307
- if (util.hasValue(destElement)) {
308
- let locResult = await this.getLocationInView(destElement);
309
- let sizeResult = await this.getSize(destElement);
310
- let offsetX = Math.abs(endX) < 1 && Math.abs(endX) > 0 ? sizeResult.width * endX : endX;
311
- let offsetY = Math.abs(endY) < 1 && Math.abs(endY) > 0 ? sizeResult.height * endY : endY;
312
- endX = locResult.x + offsetX;
313
- endY = locResult.y + offsetY;
314
- // if the target element was provided, the coordinates for the destination need to be relative to it.
315
- if (util.hasValue(element)) {
316
- let firstElLocation = await this.getLocationInView(element);
317
- endX -= firstElLocation.x;
318
- endY -= firstElLocation.y;
319
- }
320
- }
321
- // clients are responsible to use these options correctly
322
- return {startX, startY, endX, endY, duration, touchCount, element};
323
- }
324
-
325
- /**
326
- * @deprecated
327
- * @this {import('../driver').AndroidDriver}
328
- * @param {import('./types').TouchAction[]} gestures
329
- * @param {boolean} [multi]
330
- * @returns {Promise<import('./types').TouchState[]>}
331
- */
332
- async function parseTouch(gestures, multi) {
333
- // because multi-touch releases at the end by default
334
- if (multi && /** @type {TouchAction} */ (_.last(gestures)).action === 'release') {
335
- gestures.pop();
336
- }
337
-
338
- let touchStateObjects = await asyncmap(
339
- gestures,
340
- async (gesture) => {
341
- let options = gesture.options || {};
342
- if (_.includes(['press', 'moveTo', 'tap', 'longPress'], gesture.action)) {
343
- options.offset = false;
344
- let elementId = gesture.options.element;
345
- if (elementId) {
346
- let pos = await this.getLocationInView(elementId);
347
- if (gesture.options.x || gesture.options.y) {
348
- options.x = pos.x + (gesture.options.x || 0);
349
- options.y = pos.y + (gesture.options.y || 0);
350
- } else {
351
- const {width, height} = await this.getSize(elementId);
352
- options.x = pos.x + width / 2;
353
- options.y = pos.y + height / 2;
354
- }
355
- let touchStateObject = {
356
- action: gesture.action,
357
- options,
358
- timeOffset: 0.005,
359
- };
360
- return touchStateObject;
361
- } else {
362
- options.x = gesture.options.x || 0;
363
- options.y = gesture.options.y || 0;
364
-
365
- let touchStateObject = {
366
- action: gesture.action,
367
- options,
368
- timeOffset: 0.005,
369
- };
370
- return touchStateObject;
371
- }
372
- } else {
373
- let offset = 0.005;
374
- if (gesture.action === 'wait') {
375
- options = gesture.options;
376
- offset = parseInt(gesture.options.ms, 10) / 1000;
377
- }
378
- let touchStateObject = {
379
- action: gesture.action,
380
- options,
381
- timeOffset: offset,
382
- };
383
- return touchStateObject;
384
- }
385
- },
386
- false,
387
- );
388
- // we need to change the time (which is now an offset)
389
- // and the position (which may be an offset)
390
- let prevPos = null,
391
- time = 0;
392
- for (let state of touchStateObjects) {
393
- if (_.isUndefined(state.options.x) && _.isUndefined(state.options.y) && prevPos !== null) {
394
- // this happens with wait
395
- state.options.x = prevPos.x;
396
- state.options.y = prevPos.y;
397
- }
398
- if (state.options.offset && prevPos) {
399
- // the current position is an offset
400
- state.options.x += prevPos.x;
401
- state.options.y += prevPos.y;
402
- }
403
- delete state.options.offset;
404
- if (!_.isUndefined(state.options.x) && !_.isUndefined(state.options.y)) {
405
- prevPos = state.options;
406
- }
407
-
408
- if (multi) {
409
- let timeOffset = state.timeOffset;
410
- time += timeOffset;
411
- state.time = truncateDecimals(time, 3);
412
-
413
- // multi gestures require 'touch' rather than 'options'
414
- if (!_.isUndefined(state.options.x) && !_.isUndefined(state.options.y)) {
415
- state.touch = {
416
- x: state.options.x,
417
- y: state.options.y,
418
- };
419
- }
420
- delete state.options;
421
- }
422
- delete state.timeOffset;
423
- }
424
- return touchStateObjects;
425
- }
426
-
427
- /**
428
- * @deprecated
429
- * @this {import('../driver').AndroidDriver}
430
- * @param {import('./types').TouchAction[]} gestures
431
- * @returns {Promise<import('./types').ReleaseTouchAction|undefined>}
432
- */
433
- async function fixRelease(gestures) {
434
- let release = /** @type {import('./types').ReleaseTouchAction} */ (_.last(gestures));
435
- // sometimes there are no options
436
- release.options = release.options || {};
437
- // nothing to do if release options are already set
438
- if (release.options.element || (release.options.x && release.options.y)) {
439
- return;
440
- }
441
- // without coordinates, `release` uses the center of the screen, which,
442
- // generally speaking, is not what we want
443
- // therefore: loop backwards and use the last command with an element and/or
444
- // offset coordinates
445
- gestures = _.clone(gestures);
446
- let ref = null;
447
- for (let gesture of /** @type {NonReleaseTouchAction[]} */ (gestures.reverse())) {
448
- let opts = gesture.options;
449
- if (opts.element || (opts.x && opts.y)) {
450
- ref = gesture;
451
- break;
452
- }
453
- }
454
- if (ref) {
455
- let opts = ref.options;
456
- if (opts.element) {
457
- let loc = await this.getLocationInView(opts.element);
458
- if (opts.x && opts.y) {
459
- // this is an offset from the element
460
- release.options = {
461
- x: loc.x + opts.x,
462
- y: loc.y + opts.y,
463
- };
464
- } else {
465
- // this is the center of the element
466
- let size = await this.getSize(opts.element);
467
- release.options = {
468
- x: loc.x + size.width / 2,
469
- y: loc.y + size.height / 2,
470
- };
471
- }
472
- } else {
473
- release.options = _.pick(opts, 'x', 'y');
474
- }
475
- }
476
- return release;
477
- }
478
-
479
- /**
480
- * @deprecated
481
- * @this {import('../driver').AndroidDriver}
482
- * @param {import('./types').TouchAction} gesture
483
- * @returns {Promise<void>}
484
- */
485
- async function performGesture(gesture) {
486
- try {
487
- return await this.doTouchAction(gesture.action, gesture.options || {});
488
- } catch (e) {
489
- // sometime the element is not available when releasing, retry without it
490
- if (
491
- isErrorType(e, errors.NoSuchElementError) &&
492
- gesture.action === 'release' &&
493
- gesture.options?.element
494
- ) {
495
- delete gesture.options.element;
496
- this.log.debug(`retrying release without element opts: ${gesture.options}.`);
497
- return await this.doTouchAction(gesture.action, gesture.options || {});
498
- }
499
- throw e;
500
- }
501
- }
502
-
503
- // #endregion
504
-
505
- /**
506
- * @typedef {import('appium-adb').ADB} ADB
507
- * @typedef {import('./types').TouchAction} TouchAction
508
- * @typedef {import('./types').NonReleaseTouchAction} NonReleaseTouchAction
509
- */