forked from lsphillips/wait-for-the-element
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitForTheElement.d.ts
More file actions
75 lines (70 loc) · 2.33 KB
/
Copy pathwaitForTheElement.d.ts
File metadata and controls
75 lines (70 loc) · 2.33 KB
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
/**
* Options that define how an element should be searched for.
*/
export interface ElementSearchOptions
{
/**
* The maximum amount of time (in milliseconds) to wait for a matching element to exist.
*/
timeout? : number;
/**
* The root element to start searching from.
*/
scope? : Element;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Fetches an element by waiting for it to exist.
*
* Example usage:
*
* ``` js
* try
* {
* let element = await waitForTheElement('.selector-for-an-element-that-may-appear-later', {
* timeout : 5000
* });
* }
* catch (error)
* {
* throw new Error('Took more than 5 seconds to find the element.');
* }
* ```
*
* By default it will search the entire document and wait for 2.5 seconds.
*
* @param selector The selector of the element to fetch. This can be any selector that `document.querySelector()` supports.
* @param options Options that define how the element should be searched for.
*
* @typeParam E The type of the element you expect to be found.
*
* @returns A promise that will resolve with an element matching `selector`, or reject with an error if an element isn't found in time.
*/
export function waitForTheElement<E extends Element> (selector : string, options? : ElementSearchOptions) : Promise<E>;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Fetches an element by waiting for it to exist, but returns `null` instead of throwing an error.
*
* Example usage:
*
* ``` js
* let element = await tryAndWaitForTheElement('.selector-for-an-element-that-may-appear-later', {
* timeout : 5000
* });
*
* if (element === null)
* {
* console.log('Took more than 5 seconds to find the element.');
* }
* ```
*
* By default it will search the entire document and wait for 2.5 seconds.
*
* @param selector The selector of the element to fetch. This can be any selector that `document.querySelector()` supports.
* @param options Options that define how the element should be searched for.
*
* @typeParam E The type of the element you expect to be found.
*
* @returns A promise that will resolve with an element matching `selector`, or `null` if an element isn't found in time.
*/
export function tryAndWaitForTheElement<E extends Element> (selector : string, options? : ElementSearchOptions) : Promise<E | null>;