Skip to content

Commit 77c2b32

Browse files
evaluate height if required
1 parent fae06c0 commit 77c2b32

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/lib/processSnapshot.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
5656
}
5757
}
5858
const page = await context.newPage();
59-
let height = 0;
6059

6160
// populate cache with already captured resources
6261
let cache: Record<string, any> = {};
@@ -285,15 +284,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
285284

286285
}
287286
if (ctx.config.cliEnableJavaScript && fullPage) await page.evaluate(scrollToBottomAndBackToTop, { frequency: 100, timing: ctx.config.scrollTime });
288-
// Calculate the height of the content
289-
height = await page.evaluate(() => {
290-
const body = document.body;
291-
const html = document.documentElement;
292-
return Math.max(
293-
body.scrollHeight, body.offsetHeight,
294-
html.clientHeight, html.scrollHeight, html.offsetHeight
295-
);
296-
});
297287

298288
ctx.log.debug(`Calculated content height: ${height}`);
299289

@@ -313,6 +303,30 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
313303
throw new Error(`for snapshot ${snapshot.name} viewport ${viewportString}, multiple elements found for selector ${processedOptions.element}`);
314304
}
315305
} else if (selectors.length) {
306+
let height = 0;
307+
height = await page.evaluate(() => {
308+
const DEFAULT_HEIGHT = 16384;
309+
const body = document.body;
310+
const html = document.documentElement;
311+
if (!body || !html) {
312+
ctx.log.debug('Document body or html element is missing, using default height');
313+
return DEFAULT_HEIGHT;
314+
}
315+
const measurements = [
316+
body?.scrollHeight || 0,
317+
body?.offsetHeight || 0,
318+
html?.clientHeight || 0,
319+
html?.scrollHeight || 0,
320+
html?.offsetHeight || 0
321+
];
322+
const allMeasurementsInvalid = measurements.every(measurement => !measurement);
323+
if (allMeasurementsInvalid) {
324+
ctx.log.debug('All height measurements are invalid, using default height');
325+
return DEFAULT_HEIGHT;
326+
}
327+
return Math.max(...measurements);
328+
});
329+
316330
let locators: Array<Locator> = [];
317331
if (!Array.isArray(processedOptions[ignoreOrSelectBoxes][viewportString])) processedOptions[ignoreOrSelectBoxes][viewportString] = []
318332

0 commit comments

Comments
 (0)