feat: revert to listSelector

This commit is contained in:
amhsirak
2024-11-20 03:46:33 +05:30
parent 18ca1bef18
commit 0765a1a363

View File

@@ -791,93 +791,74 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
}; };
export const getChildSelectors = async (page: Page, outerHTML: string): Promise<string[]> => { export const getChildSelectors = async (page: Page, parentSelector: string): Promise<string[]> => {
try { try {
const childSelectors = await page.evaluate((outerHTML: string) => { const childSelectors = await page.evaluate((parentSelector: string) => {
// Function to get a non-unique selector based on tag and class (if present)
function getNonUniqueSelector(element: HTMLElement): string { function getNonUniqueSelector(element: HTMLElement): string {
// Start with tag name
let selector = element.tagName.toLowerCase(); let selector = element.tagName.toLowerCase();
// Add meaningful attributes const className = typeof element.className === 'string' ? element.className : '';
const attributesToConsider = [ if (className) {
'class', const classes = className.split(/\s+/).filter((cls: string) => Boolean(cls));
'data-testid', if (classes.length > 0) {
'data-cy', const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
'data-test', if (validClasses.length > 0) {
'aria-label', selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
'title',
'id'
];
// Collect additional attributes
const additionalAttrs: string[] = [];
attributesToConsider.forEach(attrName => {
if (attrName === 'class') {
// Handle classes
const className = typeof element.className === 'string' ? element.className : '';
if (className) {
const classes = className.split(/\s+/)
.filter((cls: string) => Boolean(cls))
.filter(cls => !cls.startsWith('!') && !cls.includes(':'));
if (classes.length > 0) {
additionalAttrs.push(
classes.map(cls => `.${CSS.escape(cls)}`).join('')
);
}
}
} else {
// Handle other attributes
const attrValue = element.getAttribute(attrName);
if (attrValue) {
additionalAttrs.push(`[${attrName}="${CSS.escape(attrValue)}"]`);
} }
} }
}); }
// Combine selector with attributes return selector;
return selector + additionalAttrs.join('');
} }
function getSelectorPath(element: HTMLElement | null, root: HTMLElement): string { // Function to generate selector path from an element to its parent
if (!element || element === root) return ''; function getSelectorPath(element: HTMLElement | null): string {
if (!element || !element.parentElement) return '';
const parentSelector = getSelectorPath(element.parentElement, root);
const parentSelector = getNonUniqueSelector(element.parentElement);
const elementSelector = getNonUniqueSelector(element); const elementSelector = getNonUniqueSelector(element);
return parentSelector ? `${parentSelector} ${elementSelector}` : elementSelector; return `${parentSelector} > ${elementSelector}`;
} }
function parseOuterHTML(outerHTML: string): HTMLElement { // Function to recursively get all descendant selectors
const tempContainer = document.createElement('div'); function getAllDescendantSelectors(element: HTMLElement): string[] {
tempContainer.innerHTML = outerHTML.trim(); let selectors: string[] = [];
return tempContainer.firstElementChild as HTMLElement; const children = Array.from(element.children) as HTMLElement[];
for (const child of children) {
const childPath = getSelectorPath(child);
if (childPath) {
selectors.push(childPath); // Add direct child path
selectors = selectors.concat(getAllDescendantSelectors(child)); // Recursively process descendants
}
}
return selectors;
} }
function getAllDescendantSelectors(root: HTMLElement): string[] { // Find all occurrences of the parent selector in the DOM
const descendants = root.querySelectorAll('*'); const parentElements = Array.from(document.querySelectorAll(parentSelector)) as HTMLElement[];
const selectors = Array.from(descendants).map(element => const allChildSelectors = new Set<string>(); // Use a set to ensure uniqueness
getSelectorPath(element as HTMLElement, root)
);
// Include the root element itself
const rootSelector = getNonUniqueSelector(root);
return [rootSelector, ...selectors];
}
const rootElement = parseOuterHTML(outerHTML); // Process each parent element and its descendants
if (!rootElement) return []; parentElements.forEach((parentElement) => {
return getAllDescendantSelectors(rootElement); const descendantSelectors = getAllDescendantSelectors(parentElement);
}, outerHTML); descendantSelectors.forEach((selector) => allChildSelectors.add(selector)); // Add selectors to the set
});
return Array.from(allChildSelectors); // Convert the set back to an array
}, parentSelector);
return childSelectors || []; return childSelectors || [];
} catch (error) { } catch (error) {
console.error('Error in getChildSelectorsFromOuterHTML:', error); console.error('Error in getChildSelectors:', error);
return []; 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.