Skip to content

Commit

Permalink
Add dynamic wait to allow page content to render (opendistro-for-elas…
Browse files Browse the repository at this point in the history
  • Loading branch information
itbm authored and zhongnansu committed Jul 1, 2021
1 parent fc5f050 commit 9d0b004
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 });

Expand Down

0 comments on commit 9d0b004

Please sign in to comment.