-
Notifications
You must be signed in to change notification settings - Fork 0
/
findElOrEls.ts
74 lines (65 loc) · 2.1 KB
/
findElOrEls.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
import { errors } from '@appium/base-driver';
import type { Element } from '@appium/types';
import type { Driver } from '../Driver.ts';
import * as appiumUtils from '../helpers/appium.js';
export function findElOrEls(
strategy: string,
selector: string,
mult: true,
context?: any
): Promise<Element[]>;
export function findElOrEls(
strategy: string,
selector: string,
mult: false,
context?: any
): Promise<Element>;
export async function findElOrEls(
this: Driver,
strategy: string,
selector: string,
mult: boolean,
context: string
): Promise<Element | Element[]> {
const selectors = context ? appiumUtils.fromWebDriverElement(context) : [];
const attributes = appiumUtils.getSelectorFields(strategy, selector);
const fields = attributes ? { '*': attributes } : undefined;
if (mult) {
const elements = await appiumUtils.retrying({
timeout: this.implicitWaitMs,
command: async () => {
const document = await appiumUtils.getSource.call(this, fields);
const parent = context
? appiumUtils.getElement({ elementId: context, document })
: document.documentElement;
return appiumUtils.getElements({ strategy, selector, parent });
},
validate: (elements, error) => {
return !!error || (Array.isArray(elements) && elements.length > 0);
},
});
return elements.map((_, index) =>
appiumUtils.toWebDriverElement([
...selectors,
{ using: strategy, value: selector, index },
])
);
}
await appiumUtils.retrying({
timeout: this.implicitWaitMs || 0,
command: async () => {
const document = await appiumUtils.getSource.call(this, fields);
const parent = context
? appiumUtils.getElement({ elementId: context, document })
: document.documentElement;
return appiumUtils.getElement({ strategy, selector, parent });
},
validate: (element, error) => {
return !!element || !(error instanceof errors.NoSuchElementError);
},
});
return appiumUtils.toWebDriverElement([
...selectors,
{ using: strategy, value: selector },
]);
}