Skip to content

Commit

Permalink
Add the quotes on the selectors in the engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Dec 4, 2024
1 parent 4906504 commit d700b4d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions common/js/selector_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
function findBestSelector(element) {
// 1. Check for `data-testid`
if (element.hasAttribute('data-testid')) {
return `[data-testid="${element.getAttribute('data-testid')}"]`;
return `'[data-testid="${element.getAttribute('data-testid')}"]'`;
}

// 2. Check for `id`
if (element.id) {
return `#${element.id}`;
return `'#${element.id}'`;
}

// 3. Check for role and accessible name (explicit or implicit roles)
const role = getRole(element);
if (role) {
const name = getAccessibleName(element);
if (name) {
return `role=${role}[name=${name}]`;
return `'role=${role}[name="${name}"]'`;
}
return `role=${role}`;
return `'role=${role}'`;
}

// 4. Check for visible text
const text = element.textContent.trim();
if (text) {
return `text="${text}"`;
return `'text="${text}"'`;
}

// 5. Fallback to XPath
Expand Down Expand Up @@ -80,17 +80,17 @@
// Helper function to generate XPath as a fallback
function generateXPath(element) {
if (element.id) {
return `//*[@id="${element.id}"]`;
return `'//*[@id="${element.id}"]'`;
}
const siblings = Array.from(element.parentNode.children).filter(
(el) => el.nodeName === element.nodeName
);
const index = siblings.indexOf(element) + 1;
const tagName = element.nodeName.toLowerCase();
if (element.parentNode === document) {
return `/${tagName}[${index}]`;
return `'/${tagName}[${index}]'`;
}
return `${generateXPath(element.parentNode)}/${tagName}[${index}]`;
return `'${generateXPath(element.parentNode)}/${tagName}[${index}]'`;
}

// Highlight and Selector Display
Expand Down

0 comments on commit d700b4d

Please sign in to comment.