-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
base.ts
143 lines (137 loc) · 3.74 KB
/
base.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
export type Event = 'request'
/**
* This is the base Browser interface all browser
* classes should build off of, it is the bare
* methods we aim to support across tests
*
* They will always await last executed command.
* The interface is mutable - it doesn't have to be in sequece.
*
* You can manually await this interface to wait for completion of the last scheduled command.
*/
export class BrowserInterface implements PromiseLike<any> {
private promise: any
then: PromiseLike<any>['then']
private catch: any
protected chain(nextCall: any): BrowserInterface {
if (!this.promise) {
this.promise = Promise.resolve(this)
}
this.promise = this.promise.then(nextCall)
this.then = (...args) => this.promise.then(...args)
this.catch = (...args) => this.promise.catch(...args)
return this
}
/**
* This function will run in chain - it will wait for previous commands.
* But it won't have an effect on chain value and chain will still be green if this throws.
*/
protected chainWithReturnValue<T>(
callback: (...args: any[]) => Promise<T>
): Promise<T> {
return new Promise<T>((resolve, reject) => {
this.chain(async (...args: any[]) => {
try {
resolve(await callback(...args))
} catch (error) {
reject(error)
}
})
})
}
async setup(browserName: string, locale?: string): Promise<void> {}
async close(): Promise<void> {}
async quit(): Promise<void> {}
elementsByCss(selector: string): BrowserInterface[] {
return [this]
}
elementByCss(selector: string): BrowserInterface {
return this
}
elementById(selector: string): BrowserInterface {
return this
}
touchStart(): BrowserInterface {
return this
}
click(opts?: { modifierKey?: boolean }): BrowserInterface {
return this
}
keydown(key: string): BrowserInterface {
return this
}
keyup(key: string): BrowserInterface {
return this
}
focusPage(): BrowserInterface {
return this
}
type(text: string): BrowserInterface {
return this
}
moveTo(): BrowserInterface {
return this
}
// TODO(NEXT-290): type this correctly as awaitable
waitForElementByCss(selector: string, timeout?: number): BrowserInterface {
return this
}
waitForCondition(snippet: string, timeout?: number): BrowserInterface {
return this
}
/**
* Use browsers `go back` functionality.
*/
back(): BrowserInterface {
return this
}
/**
* Use browsers `go forward` functionality. Inverse of back.
*/
forward(): BrowserInterface {
return this
}
refresh(): BrowserInterface {
return this
}
setDimensions(opts: { height: number; width: number }): BrowserInterface {
return this
}
addCookie(opts: { name: string; value: string }): BrowserInterface {
return this
}
deleteCookies(): BrowserInterface {
return this
}
on(event: Event, cb: (...args: any[]) => void) {}
off(event: Event, cb: (...args: any[]) => void) {}
async loadPage(
url: string,
{ disableCache: boolean, beforePageLoad: Function }
): Promise<void> {}
async get(url: string): Promise<void> {}
async getValue(): Promise<any> {}
async getAttribute(name: string): Promise<any> {}
async eval(snippet: string | Function): Promise<any> {}
async evalAsync(snippet: string | Function): Promise<any> {}
async text(): Promise<string> {
return ''
}
async getComputedCss(prop: string): Promise<string> {
return ''
}
async hasElementByCssSelector(selector: string): Promise<boolean> {
return false
}
async log(): Promise<
{ source: 'error' | 'info' | 'log'; message: string }[]
> {
return []
}
async websocketFrames(): Promise<any[]> {
return []
}
async url(): Promise<string> {
return ''
}
}