-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
const WAIT_BETWEEN_SCREENSHOTS_MS = 100; | ||
|
||
export async function waitUntilScreenStable() { | ||
const RETRY_TIMES = 5; | ||
/** | ||
* @param {function} assert Function that calls `page.screenshot()` and `expect(image).toMatchImageSnapshot()` | ||
* @param {object} options | ||
* @param options.retries The number of retries | ||
* @param options.timeoutMS The max timeout before failing the test in milliseconds | ||
* @param options.pollingMS The polling frequency in milliseconds | ||
*/ | ||
export async function toMatchImageSnapshot(assert, options = {}) { | ||
const DEFAULT_RETRY_TIMES = 5; | ||
const DEFAULT_TIMEOUT_MS = 10 * 1000; | ||
const DEFAULT_POLLING_MS = 100; | ||
|
||
const { | ||
retries = DEFAULT_RETRY_TIMES, | ||
timeoutMS = DEFAULT_TIMEOUT_MS, | ||
pollingMS = DEFAULT_POLLING_MS | ||
} = options; | ||
|
||
const startTime = Date.now(); | ||
|
||
let attempt = 0; | ||
|
||
/** | ||
* (tihuan): We want `await` to complete | ||
* before the next iteration | ||
* (tihuan): We want `await` to complete before the next iteration | ||
*/ | ||
/* eslint-disable no-await-in-loop */ | ||
while (attempt < RETRY_TIMES) { | ||
while (!isTimeoutExceeded()) { | ||
attempt++; | ||
|
||
const imageA = await page.screenshot(); | ||
|
||
await page.waitFor(WAIT_BETWEEN_SCREENSHOTS_MS); | ||
|
||
const imageB = await page.screenshot(); | ||
|
||
if (imageA.equals(imageB)) { | ||
try { | ||
await assert(); | ||
break; | ||
} else { | ||
if (attempt === RETRY_TIMES) { | ||
throw new Error(`Screen not stable after ${RETRY_TIMES} tries`); | ||
} catch (error) { | ||
if (attempt === retries) { | ||
throw error; | ||
} | ||
|
||
page.waitFor(pollingMS); | ||
|
||
continue; | ||
} | ||
} | ||
/* eslint-enable no-await-in-loop */ | ||
|
||
if (isTimeoutExceeded()) { | ||
throw new Error(`Timeout exceeded!`); | ||
} | ||
|
||
function isTimeoutExceeded() { | ||
return Date.now() - startTime > timeoutMS; | ||
} | ||
} |
This file contains 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