|
| 1 | +const recorder = require('../recorder'); |
| 2 | +const store = require('../store'); |
| 3 | +const { debug } = require('../output'); |
| 4 | + |
| 5 | +const defaultConfig = { |
| 6 | + registerGlobal: true, |
| 7 | + pollInterval: 200, |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + * |
| 13 | + * Adds global `retryTo` which retries steps a few times before failing. |
| 14 | + * |
| 15 | + * Enable this plugin in `codecept.conf.js` (enabled by default for new setups): |
| 16 | + * |
| 17 | + * ```js |
| 18 | + * plugins: { |
| 19 | + * retryTo: { |
| 20 | + * enabled: true |
| 21 | + * } |
| 22 | + * } |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * Use it in your tests: |
| 26 | + * |
| 27 | + * ```js |
| 28 | + * // retry these steps 5 times before failing |
| 29 | + * await retryTo((tryNum) => { |
| 30 | + * I.click('Open'); |
| 31 | + * I.see('Opened') |
| 32 | + * }, 5); |
| 33 | + * ``` |
| 34 | + * Set polling interval as 3rd argument (200ms by default): |
| 35 | + * |
| 36 | + * ```js |
| 37 | + * // retry these steps 5 times before failing |
| 38 | + * await retryTo((tryNum) => { |
| 39 | + * I.click('Open'); |
| 40 | + * I.see('Opened') |
| 41 | + * }, 5, 100); |
| 42 | + * ``` |
| 43 | + * |
| 44 | + * Default polling interval can be changed in a config: |
| 45 | + * |
| 46 | + * ```js |
| 47 | + * plugins: { |
| 48 | + * retryTo: { |
| 49 | + * enabled: true, |
| 50 | + * pollingInterval: 500, |
| 51 | + * } |
| 52 | + * } |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * Disables retryFailedStep plugin for steps inside a block; |
| 56 | + * |
| 57 | + * Use this plugin if: |
| 58 | + * |
| 59 | + * * you need repeat a set of actions in flaky tests |
| 60 | + * * iframe was not rendered and you need to retry switching to it |
| 61 | + * |
| 62 | + * |
| 63 | + * #### Configuration |
| 64 | + * |
| 65 | + * * `pollingInterval` - default interval between retries in ms. 200 by default. |
| 66 | + * * `registerGlobal` - to register `retryTo` function globally, true by default |
| 67 | + * |
| 68 | + * If `registerGlobal` is false you can use retryTo from the plugin: |
| 69 | + * |
| 70 | + * ```js |
| 71 | + * const retryTo = codeceptjs.container.plugins('retryTo'); |
| 72 | + * ``` |
| 73 | + * |
| 74 | +*/ |
| 75 | +module.exports = function (config) { |
| 76 | + config = Object.assign(defaultConfig, config); |
| 77 | + |
| 78 | + if (config.registerGlobal) { |
| 79 | + global.retryTo = retryTo; |
| 80 | + } |
| 81 | + return retryTo; |
| 82 | + |
| 83 | + function retryTo(callback, maxTries, pollInterval = undefined) { |
| 84 | + const mode = store.debugMode; |
| 85 | + let tries = 1; |
| 86 | + if (!pollInterval) pollInterval = config.pollInterval; |
| 87 | + |
| 88 | + let err = null; |
| 89 | + |
| 90 | + return new Promise((done) => { |
| 91 | + const tryBlock = () => { |
| 92 | + recorder.session.start(`retryTo ${tries}`); |
| 93 | + callback(tries); |
| 94 | + recorder.add(() => { |
| 95 | + recorder.session.restore(`retryTo ${tries}`); |
| 96 | + done(null); |
| 97 | + }); |
| 98 | + recorder.session.catch((e) => { |
| 99 | + err = e; |
| 100 | + recorder.session.restore(`retryTo ${tries}`); |
| 101 | + tries++; |
| 102 | + // recorder.session.restore(`retryTo`); |
| 103 | + if (tries <= maxTries) { |
| 104 | + debug(`Error ${err}... Retrying`); |
| 105 | + err = null; |
| 106 | + |
| 107 | + recorder.add(`retryTo ${tries}`, () => { |
| 108 | + tryBlock(); |
| 109 | + // recorder.add(() => new Promise(done => setTimeout(done, pollInterval))); |
| 110 | + }); |
| 111 | + } else { |
| 112 | + // recorder.throw(err); |
| 113 | + done(null); |
| 114 | + } |
| 115 | + }); |
| 116 | + // return recorder.promise(); |
| 117 | + }; |
| 118 | + |
| 119 | + recorder.add('retryTo', async () => { |
| 120 | + store.debugMode = true; |
| 121 | + tryBlock(); |
| 122 | + // recorder.add(() => recorder.session.restore(`retryTo ${tries-1}`)); |
| 123 | + }); |
| 124 | + }).then(() => { |
| 125 | + if (err) recorder.throw(err); |
| 126 | + }); |
| 127 | + } |
| 128 | +}; |
0 commit comments