Skip to content

Instantly share code, notes, and snippets.

@TETRA2000
Created November 10, 2019 09:55
Show Gist options
  • Save TETRA2000/757378a047fdb17fe8a441163945c318 to your computer and use it in GitHub Desktop.
Save TETRA2000/757378a047fdb17fe8a441163945c318 to your computer and use it in GitHub Desktop.
Wait till page stopped with puppeteer
// 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