-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathretryTo.js
129 lines (120 loc) · 2.92 KB
/
retryTo.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
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
const recorder = require('../recorder');
const { debug } = require('../output');
const defaultConfig = {
registerGlobal: true,
pollInterval: 200,
};
/**
*
*
* Adds global `retryTo` which retries steps a few times before failing.
*
* Enable this plugin in `codecept.conf.js` (enabled by default for new setups):
*
* ```js
* plugins: {
* retryTo: {
* enabled: true
* }
* }
* ```
*
* Use it in your tests:
*
* ```js
* // retry these steps 5 times before failing
* await retryTo((tryNum) => {
* I.switchTo('#editor frame');
* I.click('Open');
* I.see('Opened')
* }, 5);
* ```
* Set polling interval as 3rd argument (200ms by default):
*
* ```js
* // retry these steps 5 times before failing
* await retryTo((tryNum) => {
* I.switchTo('#editor frame');
* I.click('Open');
* I.see('Opened')
* }, 5, 100);
* ```
*
* Default polling interval can be changed in a config:
*
* ```js
* plugins: {
* retryTo: {
* enabled: true,
* pollInterval: 500,
* }
* }
* ```
*
* Disables retryFailedStep plugin for steps inside a block;
*
* Use this plugin if:
*
* * you need repeat a set of actions in flaky tests
* * iframe was not rendered and you need to retry switching to it
*
*
* #### Configuration
*
* * `pollInterval` - default interval between retries in ms. 200 by default.
* * `registerGlobal` - to register `retryTo` function globally, true by default
*
* If `registerGlobal` is false you can use retryTo from the plugin:
*
* ```js
* const retryTo = codeceptjs.container.plugins('retryTo');
* ```
*
*/
module.exports = function (config) {
config = Object.assign(defaultConfig, config);
function retryTo(callback, maxTries, pollInterval = config.pollInterval) {
return new Promise((done, reject) => {
let tries = 1;
function handleRetryException(err) {
recorder.throw(err);
reject(err);
}
const tryBlock = async () => {
tries++;
recorder.session.start(`retryTo ${tries}`);
try {
await callback(tries);
} catch (err) {
handleRetryException(err);
}
// Call done if no errors
recorder.add(() => {
recorder.session.restore(`retryTo ${tries}`);
done(null);
});
// Catch errors and retry
recorder.session.catch((err) => {
recorder.session.restore(`retryTo ${tries}`);
if (tries <= maxTries) {
debug(`Error ${err}... Retrying`);
recorder.add(`retryTo ${tries}`, () =>
setTimeout(tryBlock, pollInterval)
);
} else {
// if maxTries reached
handleRetryException(err);
}
});
};
recorder.add('retryTo', tryBlock).catch(err => {
console.error('An error occurred:', err);
done(null);
});
});
}
if (config.registerGlobal) {
global.retryTo = retryTo;
}
return retryTo;
};