-
Notifications
You must be signed in to change notification settings - Fork 582
/
index.js
65 lines (60 loc) · 2.53 KB
/
index.js
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
// This will ensure the page is tested for 10 minutes, until the test can be considered as failed.
const MAX_RETRIES = 200;
const WAIT_BETWEEN_RETRIES = 3000;
const REPEAT_WAIT_BETWEEN_RETRIES = 2000;
const sleep = (ms = WAIT_BETWEEN_RETRIES) =>
new Promise(resolve => {
setTimeout(resolve, ms);
});
Cypress.Commands.add("reloadUntil", (callback, options = {}) => {
return cy.log(`Reloading until a condition is met...`).then(() => {
let retries = -1;
let repeat = 0;
function check() {
retries++;
return cy.then(async response => {
await sleep(REPEAT_WAIT_BETWEEN_RETRIES);
const result = await callback(response);
try {
if (!result) {
throw Error();
}
// Sometimes, reloading the page can still return previous, not-wanted result. To avoid this,
// users can pass `options.repeat`, which will reload the page and make the extra assertions
// `options.repeat` times.
if (options.repeat > 0) {
repeat++;
if (repeat <= options.repeat) {
if (repeat === 1) {
cy.log(
`Success, but repeating the assertion ${options.repeat} times, to be extra sure.`
);
}
cy.log(`Assertion repeat ${repeat} / ${options.repeat}.`);
return cy
.log(`Reloading (attempt #${retries + 1})...`)
.reload()
.then(() => {
return check();
});
}
}
return cy.log("Condition met, moving on...");
} catch (err) {
if (retries > MAX_RETRIES) {
throw new Error(`retried too many times (${--retries})`);
}
await sleep();
return cy
.log(`Reloading (attempt #${retries + 1})...`)
.reload()
.then(() => {
return check();
});
}
return response;
});
}
return check();
});
});