feat: set depth for child selectors

This commit is contained in:
karishmas6
2024-09-03 03:30:53 +05:30
parent c2472c2251
commit 00e28c9741

View File

@@ -789,14 +789,16 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
} }
}; };
export const getChildSelectors = async (page: Page, parentSelector: string): Promise<string[]> => { export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise<string[]> => {
try { try {
const childSelectors = await page.evaluate((parentSelector: string) => { const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => {
const parentElement = document.querySelector(parentSelector); function getSelectors(element: HTMLElement, currentDepth: number): string[] {
if (!parentElement) return []; if (currentDepth > maxDepth) return [];
const childElements = Array.from(parentElement.children); const selectors: string[] = [];
return childElements.map(child => { const childElements = Array.from(element.children);
for (const child of childElements) {
let selector = child.tagName.toLowerCase(); let selector = child.tagName.toLowerCase();
if (child.className) { if (child.className) {
const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls)); const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls));
@@ -807,9 +809,20 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
} }
} }
} }
return selector; selectors.push(selector);
});
}, parentSelector); // Recursively get selectors for deeper levels
selectors.push(...getSelectors(child as HTMLElement, currentDepth + 1));
}
return selectors;
}
const parentElement = document.querySelector(parentSelector) as HTMLElement;
if (!parentElement) return [];
return getSelectors(parentElement, 0);
}, { parentSelector, maxDepth });
return childSelectors || []; return childSelectors || [];
} catch (error) { } catch (error) {
@@ -818,6 +831,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
} }
}; };
/** /**
* Returns the first pair from the given workflow that contains the given selector * Returns the first pair from the given workflow that contains the given selector
* inside the where condition, and it is the only selector there. * inside the where condition, and it is the only selector there.