refactor: use generateNonUniqueSelector

This commit is contained in:
karishmas6
2024-09-03 03:34:17 +05:30
parent 7e7f6dac48
commit f491d1bf3d

View File

@@ -755,23 +755,23 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
try { try {
const selectors = await page.evaluate(({ x, y }: { x: number, y: number }) => { const selectors = await page.evaluate(({ x, y }: { x: number, y: number }) => {
function getNonUniqueSelector(element: HTMLElement): string { // function getNonUniqueSelector(element: HTMLElement): string {
let selector = element.tagName.toLowerCase(); // let selector = element.tagName.toLowerCase();
// Avoid using IDs to maintain non-uniqueness // // Avoid using IDs to maintain non-uniqueness
if (element.className) { // if (element.className) {
const classes = element.className.split(/\s+/).filter((cls: string) => Boolean(cls)); // const classes = element.className.split(/\s+/).filter((cls: string) => Boolean(cls));
if (classes.length > 0) { // if (classes.length > 0) {
// Exclude utility classes and escape special characters // // Exclude utility classes and escape special characters
const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':')); // const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
if (validClasses.length > 0) { // if (validClasses.length > 0) {
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.'); // selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
} // }
} // }
} // }
return selector; // return selector;
} // }
function getSelectorPath(element: HTMLElement | null): string { function getSelectorPath(element: HTMLElement | null): string {
const path: string[] = []; const path: string[] = [];
@@ -779,7 +779,7 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
const maxDepth = 2; // Limiting the depth of the selector path const maxDepth = 2; // Limiting the depth of the selector path
while (element && element !== document.body && depth < maxDepth) { while (element && element !== document.body && depth < maxDepth) {
const selector = getNonUniqueSelector(element); const selector = generateNonUniqueSelector(element);
path.unshift(selector); path.unshift(selector);
element = element.parentElement; element = element.parentElement;