This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwaits.ts
388 lines (372 loc) · 12.5 KB
/
waits.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Copyright (c) 2018 Hetzner Cloud GmbH
*
* SPDX-License-Identifier: MIT
*/
import { browser, ElementArrayFinder, ElementFinder } from 'protractor';
import { Locator } from 'protractor/built/locators';
import * as webdriver from 'selenium-webdriver';
import { getElementArrayFinder, getElementFinder, log } from './utils';
import { DEFAULT_TIMEOUT } from './config';
/**
* Wait for an element not to be present. Not present means that this element
* does not exist in the DOM.
*
* @param {ElementFinder | Locator | string} target
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitToBeNotPresent(
target: ElementFinder | Locator | string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const e: ElementFinder = getElementFinder(target);
// Don't use EC.not(EC.presenceOf(e)) because it doesn't return a promise which we can catch
return browser.wait(
() => {
return getElementFinder(target)
.isPresent()
.then(
(value: boolean) => !value,
() => false
);
},
timeout,
`Element ${e.locator()} is still present`
);
}
/**
* Wait for an element to be not displayed. An element which is not displayed
* could still be part of the DOM, but is hidden by a css rule.
*
* @param {ElementFinder | Locator | string} target Target element
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitToBeNotDisplayed(
target: ElementFinder | Locator | string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const e: ElementFinder = getElementFinder(target);
// Don't use EC.invisibilityOf(e) because it doesn't return a promise which we can catch
return browser.wait(
() => {
return getElementFinder(target)
.isPresent()
.then(result => {
if (!result) {
return false;
}
return e.isDisplayed();
})
.then(
(value: boolean) => !value,
() => false
);
},
timeout,
`Element ${e.locator()} is still displayed`
);
}
/**
* Wait for an element to be present. Present means the element is part of the
* DOM, but still might be hidden by CSS rules.
*
* @param {ElementFinder | Locator | string} target Target element
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitToBePresent(
target: ElementFinder | Locator | string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
let e: ElementFinder = getElementFinder(target);
// don't use EC.presenceOf(e) because it doesn't return a promise which we can catch
return browser.wait(
(): webdriver.promise.Promise<boolean> => {
e = getElementFinder(target);
log(`Element ${e.locator()} waitToBePresent`);
return e.isPresent().then(
(value: boolean) => value,
() => false
);
},
timeout,
`Element ${e.locator()} is not present`
);
}
/**
* Wait for an element to be displayed. Displayed means that it is part of the
* DOM **and** visible.
*
* @param {ElementFinder | Locator | string} target
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitToBeDisplayed(
target: ElementFinder | Locator | string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
let e: ElementFinder = getElementFinder(target);
// Don't use EC.visibilityOf(e), here because it doesn't return a promise which we can catch
return browser.wait(
(): webdriver.promise.Promise<boolean> => {
e = getElementFinder(target);
log(`Element ${e.locator()} waitToBeDisplayed`);
return e
.isPresent()
.then(
(value: boolean) => {
if (!value) {
return false;
}
return e.isDisplayed();
},
() => false
)
.then(
(value: boolean) => value,
() => false
);
},
timeout,
`Element ${e.locator()} is not present nor displayed`
);
}
/**
* Wait for an element's text content to equal the given value.
*
* @param {ElementFinder | Locator | string} target Target element
* @param {string} value The string we are waiting for
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForTextToBe(
target: ElementFinder | Locator | string,
value: string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const e: ElementFinder = getElementFinder(target);
// Don't use EC.textToBePresentInElement because it doesn't return a promise which we can catch
return browser.wait(
() => {
return waitToBeDisplayed(e, timeout)
.then(() => {
return getElementFinder(target).getText();
})
.then(
(text: string) => text === value,
() => false
);
},
timeout,
`Error waiting for text in ${e.locator()} to be ${value}`
);
}
/**
* Wait for an element's text content to match a regular expression.
*
* @param {ElementFinder | Locator | string} target
* @param {RegExp} value The RegExp which the content of the target should match
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForTextMatch(
target: ElementFinder | Locator | string,
value: RegExp,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
return browser.wait(
() => {
return waitToBeDisplayed(target, timeout)
.then(() => getElementFinder(target).getText())
.then(
(text: string) => !!text.match(value),
() => false
);
},
timeout,
`Error waiting for text to match ${value}`
);
}
/**
* Wait for an element's attribute to have the given value.
*
* @param {ElementFinder | Locator | string} target Target element
* @param {string} attr Attribute name
* @param {string} value Value which the attribute should have
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForAttributeToBe(
target: ElementFinder | Locator | string,
attr: string,
value: string,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
return browser.wait(
() => {
return waitToBeDisplayed(target, timeout)
.then(() => getElementFinder(target).getAttribute(attr))
.then(
(text: string) => text === value,
() => false
);
},
timeout,
`Error waiting for attribute ${attr} value to be ${value}`
);
}
/**
* Wait for an element's attribute value to match a regular expression.
*
* @param {ElementFinder | Locator | string} target Target element
* @param {string} attr Attribute name
* @param {RegExp} value RegExp which the attribute's value should match
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForAttributeMatch(
target: ElementFinder | Locator | string,
attr: string,
value: RegExp,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
return browser.wait(
() => {
return waitToBeDisplayed(target, timeout)
.then(() => getElementFinder(target).getAttribute(attr))
.then(
(text: string) => !!text.match(value),
() => false
);
},
timeout,
`Error waiting for attribute ${attr} value to match ${value}`
);
}
/**
* Wait for the browser's URL to match a regular expression.
*
* @param {RegExp} value RegExp which the URL should match
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForUrlMatch(
value: RegExp,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
return browser.wait(
() => {
return browser.getCurrentUrl().then(
(url: string) => !!url.match(value),
() => false
);
},
timeout,
`URL has not changed to match ${value}`
);
}
/**
* Waits that a selector resolves to the expected number of elements. Useful
* e.g. to verify that the expected number of items have been added to a list.
*
* @param {ElementFinder | Locator | string} target Target selector or ElementArryFinder
* @param {number} expected Number of the expected elements
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForElementCountToBe(
target: ElementArrayFinder | Locator | string,
expected: number,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const es: ElementArrayFinder = getElementArrayFinder(target);
return browser.wait(
() => {
return es.count().then(
(count: number) => count === expected,
() => false
);
},
timeout,
`Count of element list ${es.locator()} does not equal expected value ${expected}.`
);
}
/**
* Waits that a selector resolves to more than the expected count of elements.
* Useful e.g. to verify that at least some number of items have been added to
* a list.
*
* @param {ElementFinder | Locator | string} target Target selector or ElementArrayFinder
* @param {number} expected Expected number of elements
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForElementCountToBeGreaterThan(
target: ElementArrayFinder | Locator | string,
expected: number,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const es: ElementArrayFinder = getElementArrayFinder(target);
return browser.wait(
() => {
return es.count().then(
(count: number) => count > expected,
() => false
);
},
timeout,
`Count of element list ${es.locator()} is not greather than expected value ${expected}.`
);
}
/**
* Waits that a selector resolves to less than the expected count of elements.
* Useful e.g. to verify that at least some elements have been removed from
* a list.
*
* @param {ElementFinder | Locator | string} target Target selector or ElementArrayFinder
* @param {number} expected Should be less than the expected number of elements
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForElementCountToBeLessThan(
target: ElementArrayFinder | Locator | string,
expected: number,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
const es: ElementArrayFinder = getElementArrayFinder(target);
return browser.wait(
() => {
return es.count().then(
(count: number) => count < expected,
() => false
);
},
timeout,
`Count of element list ${es.locator()} is not less than expected value ${expected}.`
);
}
/**
* Waits for a window count. Useful e.g. for confirming that a popup window was opened.
*
* @param {number} count Expected number of windows
* @param {number} timeout Timeout in milliseconds
* @returns {promise.Promise<boolean>}
*/
export function waitForWindowCount(
count: number,
timeout: number = DEFAULT_TIMEOUT
): webdriver.promise.Promise<boolean> {
return browser.wait(() => {
return browser
.getAllWindowHandles()
.then((handels: string[]) => {
return handels.length;
})
.then(
(windows: number) => windows === count,
() => false
);
}, timeout);
}