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,27 +789,40 @@ 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);
let selector = child.tagName.toLowerCase();
if (child.className) { for (const child of childElements) {
const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls)); let selector = child.tagName.toLowerCase();
if (classes.length > 0) { if (child.className) {
const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':')); const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls));
if (validClasses.length > 0) { if (classes.length > 0) {
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.'); const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
if (validClasses.length > 0) {
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
}
} }
} }
selectors.push(selector);
// Recursively get selectors for deeper levels
selectors.push(...getSelectors(child as HTMLElement, currentDepth + 1));
} }
return selector;
}); return selectors;
}, parentSelector); }
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.