@@ -301,6 +301,31 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
301301 throw new Error ( `for snapshot ${ snapshot . name } viewport ${ viewportString } , multiple elements found for selector ${ processedOptions . element } ` ) ;
302302 }
303303 } else if ( selectors . length ) {
304+ let height = 0 ;
305+ height = await page . evaluate ( ( ) => {
306+ const DEFAULT_HEIGHT = 16384 ;
307+ const body = document . body ;
308+ const html = document . documentElement ;
309+ if ( ! body || ! html ) {
310+ ctx . log . debug ( 'Document body or html element is missing, using default height' ) ;
311+ return DEFAULT_HEIGHT ;
312+ }
313+ const measurements = [
314+ body ?. scrollHeight || 0 ,
315+ body ?. offsetHeight || 0 ,
316+ html ?. clientHeight || 0 ,
317+ html ?. scrollHeight || 0 ,
318+ html ?. offsetHeight || 0
319+ ] ;
320+ const allMeasurementsInvalid = measurements . every ( measurement => ! measurement ) ;
321+ if ( allMeasurementsInvalid ) {
322+ ctx . log . debug ( 'All height measurements are invalid, using default height' ) ;
323+ return DEFAULT_HEIGHT ;
324+ }
325+ return Math . max ( ...measurements ) ;
326+ } ) ;
327+ ctx . log . debug ( `Calculated content height: ${ height } ` ) ;
328+
304329 let locators : Array < Locator > = [ ] ;
305330 if ( ! Array . isArray ( processedOptions [ ignoreOrSelectBoxes ] [ viewportString ] ) ) processedOptions [ ignoreOrSelectBoxes ] [ viewportString ] = [ ]
306331
@@ -314,12 +339,23 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
314339 }
315340 for ( const locator of locators ) {
316341 let bb = await locator . boundingBox ( ) ;
317- if ( bb ) processedOptions [ ignoreOrSelectBoxes ] [ viewportString ] . push ( {
318- left : bb . x ,
319- top : bb . y ,
320- right : bb . x + bb . width ,
321- bottom : bb . y + bb . height
322- } ) ;
342+ if ( bb ) {
343+ // Calculate top and bottom from the bounding box properties
344+ const top = bb . y ;
345+ const bottom = bb . y + bb . height ;
346+
347+ // Only push if top and bottom are within the calculated height
348+ if ( top <= height && bottom <= height ) {
349+ processedOptions [ ignoreOrSelectBoxes ] [ viewportString ] . push ( {
350+ left : bb . x ,
351+ top : top ,
352+ right : bb . x + bb . width ,
353+ bottom : bottom
354+ } ) ;
355+ } else {
356+ ctx . log . debug ( `Bounding box for selector skipped due to exceeding height: ${ JSON . stringify ( { top, bottom, height } ) } ` ) ;
357+ }
358+ }
323359 }
324360 }
325361 ctx . log . debug ( `Processed options: ${ JSON . stringify ( processedOptions ) } ` ) ;
0 commit comments