Skip to content

Commit c6a154a

Browse files
committed
changes for passing coordinates in ignore select region
1 parent 137599b commit c6a154a

File tree

3 files changed

+128
-4
lines changed

3 files changed

+128
-4
lines changed

src/lib/processSnapshot.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Snapshot, Context, DiscoveryErrors } from "../types.js";
2-
import { scrollToBottomAndBackToTop, getRenderViewports, getRenderViewportsForOptions } from "./utils.js"
2+
import { scrollToBottomAndBackToTop, getRenderViewports, getRenderViewportsForOptions, validateCoordinates } from "./utils.js"
33
import { chromium, Locator } from "@playwright/test"
44
import constants from "./constants.js";
55
import { updateLogContext } from '../lib/logger.js'
@@ -126,6 +126,9 @@ export async function prepareSnapshot(snapshot: Snapshot, ctx: Context): Promise
126126
case 'cssSelector':
127127
selectors.push(...value);
128128
break;
129+
case 'coordinates':
130+
selectors.push(...value.map(e => `coordinates=${e}`));
131+
break;
129132
}
130133
}
131134
}
@@ -500,6 +503,9 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
500503
case 'cssSelector':
501504
selectors.push(...value);
502505
break;
506+
case 'coordinates':
507+
selectors.push(...value.map(e => `coordinates=${e}`));
508+
break;
503509
}
504510
}
505511
}
@@ -663,14 +669,56 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
663669
if (!Array.isArray(processedOptions[ignoreOrSelectBoxes][viewportString])) processedOptions[ignoreOrSelectBoxes][viewportString] = []
664670

665671
for (const selector of selectors) {
672+
// Handle coordinates validation
673+
if (selector.startsWith('coordinates=')) {
674+
const coordString = selector.replace('coordinates=', '');
675+
const viewportSize = await page.viewportSize();
676+
677+
if (!viewportSize) {
678+
optionWarnings.add(`for snapshot ${snapshot.name} viewport ${viewportString}, unable to get viewport size for coordinate validation`);
679+
continue;
680+
}
681+
682+
const validation = validateCoordinates(coordString, viewportSize, snapshot.name, viewportString);
683+
684+
685+
if (!validation.valid) {
686+
optionWarnings.add(validation.error!);
687+
continue;
688+
}
689+
690+
// Coordinates are valid - create a coordinate element
691+
const coordinateElement = {
692+
type: 'coordinates',
693+
...validation.coords
694+
};
695+
locators.push(coordinateElement as any);
696+
continue;
697+
}
698+
666699
let l = await page.locator(selector).all()
667700
if (l.length === 0) {
668701
optionWarnings.add(`for snapshot ${snapshot.name} viewport ${viewportString}, no element found for selector ${selector}`);
669702
continue;
670703
}
671704
locators.push(...l);
672705
}
706+
673707
for (const locator of locators) {
708+
if (locator && typeof locator === 'object' && locator.hasOwnProperty('type') && (locator as any).type === 'coordinates') {
709+
const coordLocator = locator as any;
710+
const { top, bottom, left, right } = coordLocator;
711+
console.log(`locator: ${JSON.stringify(locator)}`);
712+
// Coordinates already validated, push directly
713+
processedOptions[ignoreOrSelectBoxes][viewportString].push({
714+
left: left,
715+
top: top,
716+
right: right,
717+
bottom: bottom
718+
});
719+
continue;
720+
}
721+
674722
let bb = await locator.boundingBox();
675723
if (bb) {
676724
// Calculate top and bottom from the bounding box properties
@@ -738,3 +786,5 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
738786
discoveryErrors: discoveryErrors
739787
}
740788
}
789+
790+

src/lib/utils.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,76 @@ export function calculateVariantCountFromSnapshot(snapshot: any, globalConfig?:
509509
}
510510

511511
return variantCount;
512-
}
512+
}
513+
514+
export function validateCoordinates(
515+
coordString: string,
516+
viewportSize: { width: number, height: number },
517+
snapshotName: string,
518+
viewportString: string
519+
): { valid: boolean, error?: string, coords?: { top: number, bottom: number, left: number, right: number } } {
520+
521+
// Parse coordinates
522+
const coords = coordString.split(',').map(Number);
523+
524+
// Check format
525+
if (coords.length !== 4) {
526+
return {
527+
valid: false,
528+
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinates format: ${coordString}. Expected: top,bottom,left,right`
529+
};
530+
}
531+
532+
const [top, bottom, left, right] = coords;
533+
534+
// Check if all values are numbers
535+
if (coords.some(isNaN)) {
536+
return {
537+
valid: false,
538+
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate values: ${coordString}. All values must be numbers`
539+
};
540+
}
541+
542+
// Check coordinate bounds
543+
if (top < 0 || left < 0 || bottom < 0 || right < 0) {
544+
return {
545+
valid: false,
546+
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. top,left,bottom,right must be >= 0`
547+
};
548+
}
549+
550+
if (top >= bottom) {
551+
return {
552+
valid: false,
553+
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. top must be < bottom`
554+
};
555+
}
556+
557+
if (left >= right) {
558+
return {
559+
valid: false,
560+
error: `for snapshot ${snapshotName} viewport ${viewportString}, invalid coordinate bounds: ${coordString}. left must be < right`
561+
};
562+
}
563+
564+
// Check viewport bounds
565+
if (bottom > viewportSize.height) {
566+
return {
567+
valid: false,
568+
error: `for snapshot ${snapshotName} viewport ${viewportString}, coordinates exceed viewport bounds: ${coordString}. bottom (${bottom}) exceeds viewport height (${viewportSize.height})`
569+
};
570+
}
571+
572+
if (right > viewportSize.width) {
573+
return {
574+
valid: false,
575+
error: `for snapshot ${snapshotName} viewport ${viewportString}, coordinates exceed viewport bounds: ${coordString}. right (${right}) exceeds viewport width (${viewportSize.width})`
576+
};
577+
}
578+
579+
// All validations passed
580+
return {
581+
valid: true,
582+
coords: { top, bottom, left, right }
583+
};
584+
}

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,15 @@ export interface Snapshot {
122122
id?: Array<string>,
123123
class?: Array<string>,
124124
cssSelector?: Array<string>,
125-
xpath?: Array<string>
125+
xpath?: Array<string>,
126+
coordinates?: Array<string>
126127
},
127128
selectDOM?: {
128129
id?: Array<string>,
129130
class?: Array<string>,
130131
cssSelector?: Array<string>,
131-
xpath?: Array<string>
132+
xpath?: Array<string>,
133+
coordinates?: Array<string>
132134
},
133135
element?: {
134136
id?: string,

0 commit comments

Comments
 (0)