From 9d0b0046adcda3991848e01caaf66b476690cfae Mon Sep 17 00:00:00 2001 From: itbm <22393016+itbm@users.noreply.github.com> Date: Tue, 23 Feb 2021 22:08:40 +0000 Subject: [PATCH] Add dynamic wait to allow page content to render (#331) --- .../utils/visual_report/visualReportHelper.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kibana-reports/server/routes/utils/visual_report/visualReportHelper.ts b/kibana-reports/server/routes/utils/visual_report/visualReportHelper.ts index 036d544b..c62b845e 100644 --- a/kibana-reports/server/routes/utils/visual_report/visualReportHelper.ts +++ b/kibana-reports/server/routes/utils/visual_report/visualReportHelper.ts @@ -59,6 +59,24 @@ export const createVisualReport = async ( ? DOMPurify.sanitize(header) : DEFAULT_REPORT_HEADER; const reportFooter = footer ? DOMPurify.sanitize(footer) : ''; + + // add waitForDynamicContent function + const waitForDynamicContent = async(page, timeout = 30000, interval = 1000, checks = 5) => { + const maxChecks = timeout / interval; + let passedChecks = 0; + let previousLength = 0; + + let i=0; while(i++ <= maxChecks){ + let pageContent = await page.content(); + let currentLength = pageContent.length; + + (previousLength === 0 || previousLength != currentLength) ? passedChecks = 0 : passedChecks++; + if (passedChecks >= checks) { break; } + + previousLength = currentLength; + await page.waitFor(interval); + } + } // set up puppeteer const browser = await puppeteer.launch({ @@ -142,6 +160,9 @@ export const createVisualReport = async ( `report source can only be one of [Dashboard, Visualization]` ); } + + // wait for dynamic page content to render + await waitForDynamicContent(page); const screenshot = await page.screenshot({ fullPage: true });