feat: move child selector in a separate function

This commit is contained in:
karishmas6
2024-09-02 20:40:17 +05:30
parent 8a26450633
commit 66c40ed643

View File

@@ -721,10 +721,6 @@ export const getSelectors = async (page: Page, coordinates: Coordinates) => {
return null; return null;
}; };
interface NonUniqueSelectorsResult {
generalSelector: string;
childSelectors: string[];
}
/** /**
* Returns the best non-unique css {@link Selectors} for the element on the page. * Returns the best non-unique css {@link Selectors} for the element on the page.
@@ -734,7 +730,7 @@ interface NonUniqueSelectorsResult {
* @returns {Promise<Selectors|null|undefined>} * @returns {Promise<Selectors|null|undefined>}
*/ */
export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates): Promise<NonUniqueSelectorsResult> => { export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates) => {
try { try {
const selectors = await page.evaluate(({ x, y }: { x: number, y: number }) => { const selectors = await page.evaluate(({ x, y }: { x: number, y: number }) => {
@@ -752,6 +748,7 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
} }
} }
} }
return selector; return selector;
} }
@@ -762,45 +759,32 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
while (element && element !== document.body && depth < maxDepth) { while (element && element !== document.body && depth < maxDepth) {
const selector = getNonUniqueSelector(element); const selector = getNonUniqueSelector(element);
path.unshift(selector); path.unshift(selector);
element = element.parentElement; element = element.parentElement;
depth++; depth++;
} }
return path.join(' > '); return path.join(' > ');
} }
function getChildSelectors(parent: HTMLElement): string[] {
const childSelectors: string[] = [];
const children = parent.querySelectorAll('*');
children.forEach(child => {
const childSelector = getSelectorPath(child as HTMLElement);
childSelectors.push(childSelector);
});
return childSelectors;
}
const element = document.elementFromPoint(x, y) as HTMLElement | null; const element = document.elementFromPoint(x, y) as HTMLElement | null;
if (!element) return null; if (!element) return null;
const generalSelector = getSelectorPath(element); const generalSelector = getSelectorPath(element);
const childSelectors = getChildSelectors(element);
return { return {
generalSelector, generalSelector,
childSelectors,
}; };
}, coordinates); }, coordinates);
return selectors || { generalSelector: '', childSelectors: [] }; return selectors || {};
} catch (error) { } catch (error) {
console.error('Error in getNonUniqueSelectors:', error); console.error('Error in getNonUniqueSelectors:', error);
return { generalSelector: '', childSelectors: [] }; return {};
} }
}; };
/** /**
* 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.