Created
May 28, 2019 07:51
-
-
Save RyosukeCla/7d2bb11f37115db62a58747792038d47 to your computer and use it in GitHub Desktop.
cypress retry js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="Cypress" /> | |
export default async function retry(retryable, { times } = { times: 3 }) { | |
return await _retry(retryable, { times, wait: 1000, waitScale: 1 }); | |
} | |
async function _retry(retryable, { times, wait, waitScale }) { | |
try { | |
return await retryable() | |
} catch(e) { | |
const nextOption = { | |
times: times - 1, | |
wait, | |
waitScale: 2 * waitScale | |
}; | |
if (times < 0) throw new Error('failed all times'); | |
cy.log(`retry and wait for ${waitScale * wait}ms`); | |
cy.wait(waitScale * wait); | |
return await _retry(retryable, nextOption) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
難しいけど、 cypress の queue に積んでいくスレッドと、cypress の queue に積まれているタスクが実行されるスレッドの2つを意識しながらコーディングしなければならない。
使い方
retryable
が実行されると、 cypress queue にタスクが積まれる。そして、積まれたタスクが実行され終わると、 Promise によってretryable.then or catch
が実行される。そして、catch
の場合はもう一度retryable
を実行し、cypress queueにタスクを積む。またcy.wait
タスクを積むことでリトライ間隔を実装している。