This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageObject.ts
262 lines (210 loc) · 6.85 KB
/
PageObject.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
export interface Adapter<TElement> {
click(element: TElement): Promise<void>;
/* tslint:disable no-any */
evaluate<TResult>(
script: (...args: any[]) => TResult,
...args: any[]
): Promise<TResult>;
/* tslint:enable no-any */
findElements(selector: string, parent?: TElement): Promise<TElement[]>;
type(element: TElement, text: string, delay: number): Promise<void>;
}
export type Action<TComponent extends PageObject<TComponent>> = (
component: TComponent
) => Promise<void>;
export type Predicate<TComponent extends PageObject<TComponent>> = (
component: TComponent,
index: number,
components: TComponent[]
) => Promise<boolean>;
export interface Options<TComponent extends PageObject<TComponent>> {
readonly element?: object;
readonly parent?: PageObject<any> /* tslint:disable-line no-any */;
readonly predicate?: Predicate<TComponent>;
}
export interface ComponentClass<TComponent extends PageObject<TComponent>> {
readonly selector: string;
new (adapter: Adapter<object>, options?: Options<TComponent>): TComponent;
}
export class PageObject<T extends PageObject<T>> {
public static selectRoot<TComponent extends PageObject<TComponent>>(
Component: ComponentClass<TComponent>,
adapter: Adapter<object>,
predicate?: Predicate<TComponent>
): TComponent {
return new Component(adapter, {predicate});
}
private readonly _Component: ComponentClass<T>;
private readonly _adapter: Adapter<object>;
private readonly _options: Options<T>;
public constructor(adapter: Adapter<object>, options: Options<T> = {}) {
/* tslint:disable-next-line no-any */
const {constructor} = this as any;
if (typeof constructor.selector !== 'string') {
throw new Error('Missing selector');
}
this._Component = constructor;
this._adapter = adapter;
this._options = options;
}
public async click(): Promise<void> {
await this._adapter.click(await this._findElement());
}
public async focus(): Promise<void> {
await this._adapter.evaluate(
(_element: HTMLElement) => _element.focus(),
await this._findElement()
);
}
public async scrollIntoView(): Promise<void> {
await this._adapter.evaluate(
(_element: HTMLElement) =>
_element.scrollIntoView({
behavior: 'instant',
block: 'center',
inline: 'center'
}),
await this._findElement()
);
await new Promise<void>(resolve => setTimeout(resolve, 100));
}
public async type(text: string, delay: number = 100): Promise<void> {
await this._adapter.type(await this._findElement(), text, delay);
}
public async getAttribute(name: string): Promise<string> {
return this._adapter.evaluate(
(_element: HTMLElement, _name: string) =>
(_element.getAttribute(_name) || '').trim(),
await this._findElement(),
name
);
}
public async getHtml(): Promise<string> {
return this._adapter.evaluate(
(_element: HTMLElement) => _element.innerHTML.trim(),
await this._findElement()
);
}
public async getProperty<TValue>(name: string): Promise<TValue> {
return this._adapter.evaluate(
(_element, _name) => _element[_name],
await this._findElement(),
name
);
}
public async setProperty<TValue>(name: string, value: TValue): Promise<void> {
await this._adapter.evaluate(
(_element, _name, _value) => (_element[_name] = _value),
await this._findElement(),
name,
value
);
}
public async getTagName(): Promise<string> {
return this._adapter.evaluate(
(_element: HTMLElement) => _element.tagName.trim(),
await this._findElement()
);
}
public async getText(): Promise<string> {
return this._adapter.evaluate(
(_element: HTMLElement) => _element.innerText.trim(),
await this._findElement()
);
}
public async getUrl(): Promise<string> {
return this._adapter.evaluate(() => window.location.href.trim());
}
/* https://stackoverflow.com/a/36737835 */
public async isVisible(): Promise<boolean> {
return this._adapter.evaluate((_element: HTMLElement) => {
if (!_element.offsetHeight && !_element.offsetWidth) {
return false;
}
if (getComputedStyle(_element).visibility === 'hidden') {
return false;
}
return true;
}, await this._findElement());
}
public async waitUntil(action: Action<T>): Promise<this> {
const maybeTimeout = process.env.WAIT_TIMEOUT;
const timeout = maybeTimeout ? parseInt(maybeTimeout, 10) : 10000;
let error = new Error(`Timeout after ${timeout} milliseconds`);
let expired = false;
/* tslint:disable no-any */
let timeoutId1: any;
let timeoutId2: any;
/* tslint:enable no-any */
return Promise.race([
(async () => {
while (!expired) {
try {
await action(this as any) /* tslint:disable-line no-any */;
clearTimeout(timeoutId2);
return this;
} catch (e) {
error = e;
}
await new Promise<void>(resolve => {
timeoutId1 = setTimeout(resolve, 40);
});
}
/* istanbul ignore next */
throw error;
})(),
(async () => {
await new Promise<void>(resolve => {
timeoutId2 = setTimeout(resolve, timeout);
});
expired = true;
clearTimeout(timeoutId1);
throw error;
})()
]);
}
public toString(): string {
const {parent} = this._options;
const name = `${this.constructor.name}[${this._Component.selector}]`;
return parent ? `${parent.toString()} > ${name}` : name;
}
protected selectDescendant<TComponent extends PageObject<TComponent>>(
Component: ComponentClass<TComponent>,
predicate?: Predicate<TComponent>
): TComponent {
return new Component(this._adapter, {parent: this, predicate});
}
private async _findElement(): Promise<object> {
const {element} = this._options;
if (element) {
return element;
}
const elements = await this._findElements();
if (elements.length === 0) {
throw new Error(`Element not found: ${this.toString()}`);
}
if (elements.length > 1) {
throw new Error(`Element not unique: ${this.toString()}`);
}
return elements[0];
}
private async _findElements(): Promise<object[]> {
const Component = this._Component;
const adapter = this._adapter;
const options = this._options;
const {parent, predicate} = options;
const elements = await adapter.findElements(
Component.selector,
parent && (await parent._findElement())
);
if (!predicate) {
return elements;
}
const results = await Promise.all(
elements
.map(element => new Component(adapter, {...options, element}))
.map(predicate)
);
return elements.filter((element, index) => results[index]);
}
}