-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathfind.ts
96 lines (87 loc) · 2.43 KB
/
find.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* @module
*/
import _ from 'lodash';
import {errors, isErrorType} from 'appium/driver';
import type {AndroidDriver} from '../driver';
import type {Element} from '@appium/types';
import type {FindElementOpts} from './types';
export async function findElOrEls(
this: AndroidDriver,
strategy: string,
selector: string,
mult: true,
context?: string,
): Promise<Element[]>;
export async function findElOrEls(
this: AndroidDriver,
strategy: string,
selector: string,
mult: false,
context?: string,
): Promise<Element>;
export async function findElOrEls(
this: AndroidDriver,
strategy: string,
selector: string,
mult: boolean,
context = '',
) {
if (!selector) {
throw new Error('Must provide a selector when finding elements');
}
const params: FindElementOpts = {
strategy,
selector,
context,
multiple: mult,
};
let element: Element | Element[] | undefined;
const doFind = async () => {
try {
element = await this.doFindElementOrEls(params);
} catch (err) {
// if the error that comes back is from a proxied request, we need to
// unwrap it to its actual protocol error first
if (isErrorType(err, errors.ProxyRequestError)) {
err = err.getActualError(); // eslint-disable-line no-ex-assign
}
// now we have to inspect the error to determine if it is a no such
// element error, based on the shape of the error object from
// appium/driver
if (isErrorType(err, errors.NoSuchElementError)) {
// we are fine with this, just indicate a retry
return false;
}
throw err;
}
// we want to return false if we want to potentially try again
return !_.isEmpty(element);
};
try {
await this.implicitWaitForCondition(doFind);
} catch (e) {
const err = e as Error;
if (err.message && err.message.match(/Condition unmet/)) {
// only get here if we are looking for multiple elements
// condition was not met setting res to empty array
element = [];
} else {
throw err;
}
}
if (mult) {
return element as Element[];
}
if (_.isEmpty(element)) {
throw new errors.NoSuchElementError();
}
return element as Element;
}
export async function doFindElementOrEls(
this: AndroidDriver,
params: FindElementOpts,
): Promise<Element | Element[]> {
throw new errors.NotImplementedError('Not implemented');
}