feat: generate selectors relative to the same parent element as getNonUniqueSelectors

This commit is contained in:
karishmas6
2024-09-03 09:26:07 +05:30
parent 3b398d1027
commit c587a3264b

View File

@@ -806,10 +806,10 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
return selector; return selector;
} }
function getSelectorPath(element: HTMLElement | null): string { function getSelectorPath(element: HTMLElement | null, stopAtParent: HTMLElement | null): string {
const path: string[] = []; const path: string[] = [];
while (element && element !== document.body) { while (element && element !== stopAtParent && element !== document.body) {
const selector = getNonUniqueSelector(element); const selector = getNonUniqueSelector(element);
path.unshift(selector); path.unshift(selector);
element = element.parentElement; element = element.parentElement;
@@ -818,13 +818,13 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
return path.join(' > '); return path.join(' > ');
} }
function getAllDescendantSelectors(element: HTMLElement): string[] { function getAllDescendantSelectors(element: HTMLElement, stopAtParent: HTMLElement | null): string[] {
let selectors: string[] = []; let selectors: string[] = [];
const children = Array.from(element.children) as HTMLElement[]; const children = Array.from(element.children) as HTMLElement[];
for (const child of children) { for (const child of children) {
selectors.push(getSelectorPath(child)); selectors.push(getSelectorPath(child, stopAtParent));
selectors = selectors.concat(getAllDescendantSelectors(child)); selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent));
} }
return selectors; return selectors;
@@ -833,7 +833,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
const parentElement = document.querySelector(parentSelector) as HTMLElement; const parentElement = document.querySelector(parentSelector) as HTMLElement;
if (!parentElement) return []; if (!parentElement) return [];
return getAllDescendantSelectors(parentElement); return getAllDescendantSelectors(parentElement, parentElement);
}, parentSelector); }, parentSelector);
return childSelectors || []; return childSelectors || [];