From dd1a2ddec4d8137187c3ac7a529c235c2e0b0a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 12 Jul 2023 10:10:25 +0200 Subject: [PATCH] fix: wait aria-busy before images & fonts (#9) --- src/index.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6feeb5d..d9c7898 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,9 +37,9 @@ function waitForFontLoading() { // Check if the images are loaded function waitForImagesLoading() { - const allImages = Array.from(document.images); - allImages.forEach(img => img.loading = "eager") - return allImages.every((img)=>img.complete); + const allImages = Array.from(document.images); + allImages.forEach((img) => (img.loading = "eager")); + return allImages.every((img) => img.complete); } type LocatorOptions = Parameters[1]; @@ -72,9 +72,14 @@ export async function argosScreenshot( mkdir(screenshotFolder, { recursive: true }); + // Inject global styles + await page.addStyleTag({ content: GLOBAL_STYLES }); + + // Wait for all busy elements to be loaded + await page.waitForSelector('[aria-busy="true"]', { state: "hidden" }); + + // Wait for all images and fonts to be loaded await Promise.all([ - page.addStyleTag({ content: GLOBAL_STYLES }), - page.waitForSelector('[aria-busy="true"]', { state: "hidden" }), page.waitForFunction(waitForImagesLoading), page.waitForFunction(waitForFontLoading), ]);