/
webdriver.ts
73 lines (67 loc) · 2.02 KB
/
webdriver.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
/* eslint-disable max-classes-per-file */
import { ElementSelector, ElementRect } from '../lib/mugshot';
/**
* Webdriver compatible client.
*/
export interface Webdriver {
/**
* Take a viewport screenshot and return a base64 string.
*
* @see https://w3c.github.io/webdriver/#take-screenshot
*/
takeViewportScreenshot: () => Promise<string>;
/**
* Get the dimensions and coordinates of an element.
*
* @see https://w3c.github.io/webdriver/#get-element-rect
*
* If the selector matches more than 1 element then an array
* of {@link ElementRect} should be returned, otherwise a single
* {@link ElementRect} should be returned.
*
* Should return `null` if the selector doesn't match any element.
*
* Should return `0,0,0,0` for every element that is not visible.
*/
getElementRect: (
selector: ElementSelector
) => Promise<ElementRect | ElementRect[] | null>;
/**
* Set the size of the __viewport__ (meaning window minus chrome).
*
* This is unlike setWindowRect which doesn't take the chrome into account.
*
* @see https://w3c.github.io/webdriver/#set-window-rect
*/
setViewportSize: (width: number, height: number) => Promise<void>;
/**
* Execute a function in the current page context.
*
* @param func An asynchronous function returning a promise.
* Needs to be serializable.
* @param args Will be passed to the function.
* Needs to be serializable.
*
* @see https://w3c.github.io/webdriver/#execute-async-script
*/
execute: <R, A extends any[]>(
func: (...args: A) => R,
...args: A
) => Promise<R>;
}
/**
* Thrown when the given selector doesn't match anything.
*/
export class ElementNotFoundError extends Error {
constructor(selector: string) {
super(`Couldn't find element ${selector}`);
}
}
/**
* Thrown when the given selector matches an element that is not visible.
*/
export class ElementNotVisibleError extends Error {
constructor(selector: string) {
super(`Element ${selector} is not visible`);
}
}