Created
November 10, 2019 09:55
-
-
Save TETRA2000/757378a047fdb17fe8a441163945c318 to your computer and use it in GitHub Desktop.
Wait till page stopped with puppeteer
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
// tslint:disable: no-console | |
import * as BlinkDiff from "blink-diff"; | |
import { Page } from "puppeteer"; | |
export function sleep(ms: number) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
export async function waitTillPageStoped( | |
page: Page, | |
interval: number = 200, | |
timeout: number = 3000, | |
fullPage: boolean = true, | |
): Promise<boolean> { | |
const t0 = new Date().getTime(); | |
let previousBuffer: Buffer; | |
while (new Date().getTime() - t0 < timeout) { | |
await sleep(interval); | |
const currentBuffer: Buffer = Buffer.from(await page.screenshot({ | |
encoding: "base64", | |
fullPage, | |
}), "base64"); | |
if (previousBuffer == null) { | |
previousBuffer = currentBuffer; | |
continue; | |
} | |
const diff = new BlinkDiff({ imageA: previousBuffer, imageB: currentBuffer }); | |
const result = await diff.runWithPromise(); | |
if (result.differences === 0) { | |
return true; | |
} | |
previousBuffer = currentBuffer; | |
} | |
throw new Error("Timeouted!!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment