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 {
const childSelectors = await page.evaluate((parentSelector: string) => {
const parentElement = document.querySelector(parentSelector);
if (!parentElement) return [];
const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => {
function getSelectors(element: HTMLElement, currentDepth: number): string[] {
if (currentDepth > maxDepth) return [];
const childElements = Array.from(parentElement.children);
return childElements.map(child => {
let selector = child.tagName.toLowerCase();
if (child.className) {
const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls));
if (classes.length > 0) {
const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
if (validClasses.length > 0) {
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
const selectors: string[] = [];
const childElements = Array.from(element.children);
for (const child of childElements) {
let selector = child.tagName.toLowerCase();
if (child.className) {
const classes = child.className.split(/\s+/).filter((cls: string) => Boolean(cls));
if (classes.length > 0) {
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;
});
}, parentSelector);
return selectors;
}
const parentElement = document.querySelector(parentSelector) as HTMLElement;
if (!parentElement) return [];
return getSelectors(parentElement, 0);
}, { parentSelector, maxDepth });
return childSelectors || [];
} 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
* inside the where condition, and it is the only selector there.