-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathretryTo.js
121 lines (113 loc) · 2.71 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
const recorder = require('../recorder');
const store = require('../store');
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);
if (config.registerGlobal) {
global.retryTo = retryTo;
}
return retryTo;
function retryTo(callback, maxTries, pollInterval = undefined) {
let tries = 1;
if (!pollInterval) pollInterval = config.pollInterval;
let err = null;
return new Promise((done) => {
const tryBlock = () => {
recorder.session.start(`retryTo ${tries}`);
callback(tries);
recorder.add(() => {
recorder.session.restore(`retryTo ${tries}`);
done(null);
});
recorder.session.catch((e) => {
err = e;
recorder.session.restore(`retryTo ${tries}`);
tries++;
if (tries <= maxTries) {
debug(`Error ${err}... Retrying`);
err = null;
recorder.add(`retryTo ${tries}`, () => setTimeout(tryBlock, pollInterval));
} else {
done(null);
}
});
};
recorder.add('retryTo', async () => {
tryBlock();
});
}).then(() => {
if (err) recorder.throw(err);
});
}
};