From 3b398d1027def107d3437c621bf48f5c733b5cd7 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Tue, 3 Sep 2024 09:06:27 +0530 Subject: [PATCH] feat: produce full path from child element up to top-level parent --- server/src/workflow-management/selector.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/server/src/workflow-management/selector.ts b/server/src/workflow-management/selector.ts index 09b8b88f..40dd414b 100644 --- a/server/src/workflow-management/selector.ts +++ b/server/src/workflow-management/selector.ts @@ -786,13 +786,12 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates }; -export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 5): Promise => { +export const getChildSelectors = async (page: Page, parentSelector: string): Promise => { try { - const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => { + const childSelectors = await page.evaluate((parentSelector: string) => { function getNonUniqueSelector(element: HTMLElement): string { let selector = element.tagName.toLowerCase(); - // Ensure that className is a string before splitting const className = typeof element.className === 'string' ? element.className : ''; if (className) { const classes = className.split(/\s+/).filter((cls: string) => Boolean(cls)); @@ -809,27 +808,23 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD function getSelectorPath(element: HTMLElement | null): string { const path: string[] = []; - let depth = 0; - while (element && element !== document.body && depth < maxDepth) { + while (element && element !== document.body) { const selector = getNonUniqueSelector(element); path.unshift(selector); element = element.parentElement; - depth++; } return path.join(' > '); } - function getAllDescendantSelectors(element: HTMLElement, currentDepth: number = 0): string[] { - if (currentDepth >= maxDepth) return []; - + function getAllDescendantSelectors(element: HTMLElement): string[] { let selectors: string[] = []; const children = Array.from(element.children) as HTMLElement[]; for (const child of children) { selectors.push(getSelectorPath(child)); - selectors = selectors.concat(getAllDescendantSelectors(child, currentDepth + 1)); + selectors = selectors.concat(getAllDescendantSelectors(child)); } return selectors; @@ -839,7 +834,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD if (!parentElement) return []; return getAllDescendantSelectors(parentElement); - }, { parentSelector, maxDepth }); + }, parentSelector); return childSelectors || []; } catch (error) { @@ -850,6 +845,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD + /** * Returns the first pair from the given workflow that contains the given selector * inside the where condition, and it is the only selector there.