-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
82 lines (69 loc) 路 2.03 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";
const logCommand = ({ options, originalOptions }) => {
if (options.log) {
options.logger({
name: options.description,
message: options.customMessage,
consoleProps: () => originalOptions
});
}
};
const logCommandCheck = ({ result, options, originalOptions }) => {
if (!options.log || !options.verbose) return;
const message = [result];
if (options.customCheckMessage) {
message.unshift(options.customCheckMessage);
}
options.logger({
name: options.description,
message,
consoleProps: () => originalOptions
});
};
const waitUntil = (subject, checkFunction, originalOptions = {}) => {
if (!(checkFunction instanceof Function)) {
throw new Error("`checkFunction` parameter should be a function. Found: " + checkFunction);
}
const defaultOptions = {
// base options
interval: 200,
timeout: 5000,
errorMsg: "Timed out retrying",
// log options
description: "waitUntil",
log: true,
customMessage: undefined,
logger: Cypress.log,
verbose: false,
customCheckMessage: undefined
};
const options = { ...defaultOptions, ...originalOptions };
// filter out a falsy passed "customMessage" value
options.customMessage = [options.customMessage, originalOptions].filter(Boolean);
let retries = Math.floor(options.timeout / options.interval);
logCommand({ options, originalOptions });
const check = result => {
logCommandCheck({ result, options, originalOptions });
if (result) {
return result;
}
if (retries < 1) {
throw new Error(options.errorMsg);
}
cy.wait(options.interval, { log: false }).then(() => {
retries--;
return resolveValue();
});
};
const resolveValue = () => {
const result = checkFunction(subject);
const isAPromise = Boolean(result && result.then);
if (isAPromise) {
return result.then(check);
} else {
return check(result);
}
};
return resolveValue();
};
Cypress.Commands.add("waitUntil", { prevSubject: "optional" }, waitUntil);