-
Notifications
You must be signed in to change notification settings - Fork 411
/
functional_test_case.ts
183 lines (141 loc) · 5.15 KB
/
functional_test_case.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { InternTestCase } from "./intern_test_case"
import { Element } from "@theintern/leadfoot"
export class FunctionalTestCase extends InternTestCase {
get remote() {
return this.internTest.remote
}
async goToLocation(location: string): Promise<void> {
const processedLocation = location.match(/^\//) ? location.slice(1) : location
return this.remote.get(processedLocation)
}
async goBack(): Promise<void> {
return this.remote.goBack()
}
async goForward(): Promise<void> {
return this.remote.goForward()
}
async reload(): Promise<void> {
await this.evaluate(() => location.reload())
return this.nextBeat
}
async hasSelector(selector: string) {
return (await this.remote.findAllByCssSelector(selector)).length > 0
}
async selectorHasFocus(selector: string) {
const activeElement = await this.remote.getActiveElement()
return activeElement.equals(await this.querySelector(selector))
}
async querySelector(selector: string) {
return this.remote.findByCssSelector(selector)
}
async waitUntilSelector(selector: string): Promise<void> {
return (async () => {
let hasSelector = false
do hasSelector = await this.hasSelector(selector)
while (!hasSelector)
})()
}
async waitUntilNoSelector(selector: string): Promise<void> {
return (async () => {
let hasSelector = true
do hasSelector = await this.hasSelector(selector)
while (hasSelector)
})()
}
async querySelectorAll(selector: string) {
return this.remote.findAllByCssSelector(selector)
}
async clickSelector(selector: string): Promise<void> {
return (await this.remote.findByCssSelector(selector)).click()
}
async scrollToSelector(selector: string): Promise<void> {
const element = await this.remote.findByCssSelector(selector)
return this.evaluate(element => element.scrollIntoView(), element)
}
async pressTab(): Promise<void> {
return this.remote.getActiveElement().then(activeElement => activeElement.type(('\uE004'))) // TAB
}
async pressEnter(): Promise<void> {
return this.remote.getActiveElement().then(activeElement => activeElement.type(('\uE006'))) // ENTER
}
async getVisibleTextForSelector(selector: string): Promise<string> {
return this.querySelector(selector).then(element => element.getVisibleText())
}
async outerHTMLForSelector(selector: string): Promise<string> {
const element = await this.remote.findByCssSelector(selector)
return this.evaluate(element => element.outerHTML, element)
}
async innerHTMLForSelector(selector: string): Promise<string> {
const element = await this.remote.findAllByCssSelector(selector)
return this.evaluate(element => element.innerHTML, element)
}
async attributeForSelector(selector: string, attributeName: string) {
const element = await this.querySelector(selector)
return await element.getAttribute(attributeName)
}
async propertyForSelector(selector: string, attributeName: string) {
const element = await this.querySelector(selector)
return await element.getProperty(attributeName)
}
get scrollPosition(): Promise<{ x: number, y: number }> {
return this.evaluate(() => ({ x: window.scrollX, y: window.scrollY }))
}
async isScrolledToTop(): Promise<boolean> {
const { y: pageY } = await this.scrollPosition
return pageY === 0
}
async isScrolledToSelector(selector: string): Promise<boolean> {
const { y: pageY } = await this.scrollPosition
const { y: elementY } = await this.remote.findByCssSelector(selector).getPosition()
const offset = pageY - elementY
return Math.abs(offset) < 2
}
get nextBeat(): Promise<void> {
return this.sleep(100)
}
async sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
async evaluate<T>(callback: (...args: any[]) => T, ...args: any[]): Promise<T> {
return await this.remote.execute(callback, args)
}
get head(): Promise<Element> {
return this.evaluate(() => document.head as any)
}
get body(): Promise<Element> {
return this.evaluate(() => document.body as any)
}
get location(): Promise<string> {
return this.evaluate(() => location.toString())
}
get origin(): Promise<string> {
return this.evaluate(() => location.origin.toString())
}
get pathname(): Promise<string> {
return this.evaluate(() => location.pathname)
}
get search(): Promise<string> {
return this.evaluate(() => location.search)
}
get searchParams(): Promise<URLSearchParams> {
return this.evaluate(() => location.search).then(search => new URLSearchParams(search))
}
async getSearchParam(key: string): Promise<string> {
return (await this.searchParams).get(key) || ""
}
async getAllSearchParams(key: string): Promise<string[]> {
return (await this.searchParams).getAll(key) || []
}
get hash(): Promise<string> {
return this.evaluate(() => location.hash)
}
async acceptAlert(): Promise<void> {
return this.remote.acceptAlert()
}
async dismissAlert(): Promise<void> {
return this.remote.dismissAlert()
}
async getAlertText(): Promise<string> {
return this.remote.getAlertText()
}
}