feat: produce full path from child element up to top-level parent
This commit is contained in:
@@ -786,13 +786,12 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 5): Promise<string[]> => {
|
export const getChildSelectors = async (page: Page, parentSelector: string): Promise<string[]> => {
|
||||||
try {
|
try {
|
||||||
const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => {
|
const childSelectors = await page.evaluate((parentSelector: string) => {
|
||||||
function getNonUniqueSelector(element: HTMLElement): string {
|
function getNonUniqueSelector(element: HTMLElement): string {
|
||||||
let selector = element.tagName.toLowerCase();
|
let selector = element.tagName.toLowerCase();
|
||||||
|
|
||||||
// Ensure that className is a string before splitting
|
|
||||||
const className = typeof element.className === 'string' ? element.className : '';
|
const className = typeof element.className === 'string' ? element.className : '';
|
||||||
if (className) {
|
if (className) {
|
||||||
const classes = className.split(/\s+/).filter((cls: string) => Boolean(cls));
|
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 {
|
function getSelectorPath(element: HTMLElement | null): string {
|
||||||
const path: string[] = [];
|
const path: string[] = [];
|
||||||
let depth = 0;
|
|
||||||
|
|
||||||
while (element && element !== document.body && depth < maxDepth) {
|
while (element && element !== document.body) {
|
||||||
const selector = getNonUniqueSelector(element);
|
const selector = getNonUniqueSelector(element);
|
||||||
path.unshift(selector);
|
path.unshift(selector);
|
||||||
element = element.parentElement;
|
element = element.parentElement;
|
||||||
depth++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.join(' > ');
|
return path.join(' > ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllDescendantSelectors(element: HTMLElement, currentDepth: number = 0): string[] {
|
function getAllDescendantSelectors(element: HTMLElement): string[] {
|
||||||
if (currentDepth >= maxDepth) return [];
|
|
||||||
|
|
||||||
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));
|
||||||
selectors = selectors.concat(getAllDescendantSelectors(child, currentDepth + 1));
|
selectors = selectors.concat(getAllDescendantSelectors(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
return selectors;
|
return selectors;
|
||||||
@@ -839,7 +834,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD
|
|||||||
if (!parentElement) return [];
|
if (!parentElement) return [];
|
||||||
|
|
||||||
return getAllDescendantSelectors(parentElement);
|
return getAllDescendantSelectors(parentElement);
|
||||||
}, { parentSelector, maxDepth });
|
}, parentSelector);
|
||||||
|
|
||||||
return childSelectors || [];
|
return childSelectors || [];
|
||||||
} catch (error) {
|
} 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
|
* 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.
|
||||||
|
|||||||
Reference in New Issue
Block a user