From 8b20b8223b956f358894fd2768069a365c7dbdc8 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Tue, 3 Sep 2024 03:45:06 +0530 Subject: [PATCH] fix: revert --- server/src/workflow-management/selector.ts | 99 ++++++++-------------- 1 file changed, 34 insertions(+), 65 deletions(-) diff --git a/server/src/workflow-management/selector.ts b/server/src/workflow-management/selector.ts index ff269b71..8de62203 100644 --- a/server/src/workflow-management/selector.ts +++ b/server/src/workflow-management/selector.ts @@ -721,23 +721,6 @@ export const getSelectors = async (page: Page, coordinates: Coordinates) => { return null; }; -function generateNonUniqueSelector(element: HTMLElement): string { - let selector = element.tagName.toLowerCase(); - - if (element.className) { - const classes = element.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('.'); - } - } - } - - return selector; -} - - interface SelectorResult { generalSelector: string; @@ -755,23 +738,23 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates try { const selectors = await page.evaluate(({ x, y }: { x: number, y: number }) => { - // function getNonUniqueSelector(element: HTMLElement): string { - // let selector = element.tagName.toLowerCase(); + function getNonUniqueSelector(element: HTMLElement): string { + let selector = element.tagName.toLowerCase(); - // // Avoid using IDs to maintain non-uniqueness - // if (element.className) { - // const classes = element.className.split(/\s+/).filter((cls: string) => Boolean(cls)); - // if (classes.length > 0) { - // // Exclude utility classes and escape special characters - // const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':')); - // if (validClasses.length > 0) { - // selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.'); - // } - // } - // } + // Avoid using IDs to maintain non-uniqueness + if (element.className) { + const classes = element.className.split(/\s+/).filter((cls: string) => Boolean(cls)); + if (classes.length > 0) { + // Exclude utility classes and escape special characters + const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':')); + if (validClasses.length > 0) { + selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.'); + } + } + } - // return selector; - // } + return selector; + } function getSelectorPath(element: HTMLElement | null): string { const path: string[] = []; @@ -779,7 +762,7 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates const maxDepth = 2; // Limiting the depth of the selector path while (element && element !== document.body && depth < maxDepth) { - const selector = generateNonUniqueSelector(element); + const selector = getNonUniqueSelector(element); path.unshift(selector); element = element.parentElement; @@ -806,40 +789,27 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates } }; -export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise => { +export const getChildSelectors = async (page: Page, parentSelector: string): Promise => { try { - const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => { - function getSelectors(element: HTMLElement, currentDepth: number): string[] { - if (currentDepth > maxDepth) return []; - - 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 selectors; - } - - const parentElement = document.querySelector(parentSelector) as HTMLElement; + const childSelectors = await page.evaluate((parentSelector: string) => { + const parentElement = document.querySelector(parentSelector); if (!parentElement) return []; - return getSelectors(parentElement, 0); - }, { parentSelector, maxDepth }); + 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('.'); + } + } + } + return selector; + }); + }, parentSelector); return childSelectors || []; } catch (error) { @@ -848,7 +818,6 @@ 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.