Skip to content

Commit 4403f09

Browse files
committed
changes for page height and throwing warning for more than one viewport size
1 parent 40921d0 commit 4403f09

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/lib/processSnapshot.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,21 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
671671
for (const selector of selectors) {
672672
if (selector.startsWith('coordinates=')) {
673673
const coordString = selector.replace('coordinates=', '');
674-
const viewportSize = await page.viewportSize();
675-
676-
if (!viewportSize) {
677-
optionWarnings.add(`for snapshot ${snapshot.name} viewport ${viewportString}, unable to get viewport size for coordinate validation`);
678-
continue;
674+
let pageHeight = height;
675+
if (viewport.height) {
676+
pageHeight = viewport.height;
677+
}
678+
const validation = validateCoordinates(
679+
coordString,
680+
pageHeight,
681+
viewport.width,
682+
snapshot.name
683+
);
684+
685+
686+
if(renderViewports.length > 1){
687+
optionWarnings.add(`for snapshot ${snapshot.name}, coordinates may not be accurate for multiple viewports`);
679688
}
680-
681-
const validation = validateCoordinates(coordString, viewportSize, snapshot.name, viewportString);
682689

683690

684691
if (!validation.valid) {

src/lib/utils.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -669,17 +669,17 @@ function getPageNumber(screenshotName: string): string {
669669

670670
export function validateCoordinates(
671671
coordString: string,
672-
viewportSize: { width: number, height: number },
673-
snapshotName: string,
674-
viewportString: string
672+
pageHeight: number,
673+
pageWidth: number,
674+
snapshotName: string
675675
): { valid: boolean, error?: string, coords?: { top: number, bottom: number, left: number, right: number } } {
676676

677677
const coords = coordString.split(',').map(Number);
678678

679679
if (coords.length !== 4) {
680680
return {
681681
valid: false,
682-
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinates format: ${coordString}. Expected: top,bottom,left,right`
682+
error: `for snapshot ${snapshotName}, invalid coordinates format: ${coordString}. Expected: top,bottom,left,right`
683683
};
684684
}
685685

@@ -688,42 +688,42 @@ export function validateCoordinates(
688688
if (coords.some(isNaN)) {
689689
return {
690690
valid: false,
691-
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate values: ${coordString}. All values must be numbers`
691+
error: `for snapshot ${snapshotName}, invalid coordinate values: ${coordString}. All values must be numbers`
692692
};
693693
}
694694

695695
if (top < 0 || left < 0 || bottom < 0 || right < 0) {
696696
return {
697697
valid: false,
698-
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. top,left,bottom,right must be >= 0`
698+
error: `for snapshot ${snapshotName}, invalid coordinate bounds: ${coordString}. top,left,bottom,right must be >= 0`
699699
};
700700
}
701701

702702
if (top >= bottom) {
703703
return {
704704
valid: false,
705-
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. top must be < bottom`
705+
error: `for snapshot ${snapshotName}, invalid coordinate bounds: ${coordString}. top must be < bottom`
706706
};
707707
}
708708

709709
if (left >= right) {
710710
return {
711711
valid: false,
712-
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. left must be < right`
712+
error: `for snapshot ${snapshotName}, invalid coordinate bounds: ${coordString}. left must be < right`
713713
};
714714
}
715715

716-
if (bottom > viewportSize.height) {
716+
if (bottom > pageHeight) {
717717
return {
718718
valid: false,
719-
error: `for snapshot ${snapshotName} viewport ${viewportString}, coordinates exceed viewport bounds: ${coordString}. bottom (${bottom}) exceeds viewport height (${viewportSize.height})`
719+
error: `for snapshot ${snapshotName}, coordinates exceed viewport bounds: ${coordString}. bottom (${bottom}) exceeds viewport height (${pageHeight})`
720720
};
721721
}
722722

723-
if (right > viewportSize.width) {
723+
if (right > pageWidth) {
724724
return {
725725
valid: false,
726-
error: `for snapshot ${snapshotName} viewport ${viewportString}, coordinates exceed viewport bounds: ${coordString}. right (${right}) exceeds viewport width (${viewportSize.width})`
726+
error: `for snapshot ${snapshotName}, coordinates exceed viewport bounds: ${coordString}. right (${right}) exceeds viewport width (${pageWidth})`
727727
};
728728
}
729729

0 commit comments

Comments
 (0)