(element)
| 553 | * Prioritizes unique selectors over non-unique ones |
| 554 | */ |
| 555 | export function getPlaywrightSelector(element) { |
| 556 | if (!element) { |
| 557 | return { method: 'locator', selector: '*' }; |
| 558 | } |
| 559 | |
| 560 | // Build all candidates |
| 561 | const candidates = buildCandidates(element); |
| 562 | |
| 563 | // Sort by score (lower is better) |
| 564 | candidates.sort((a, b) => a.score - b.score); |
| 565 | |
| 566 | // Find the best UNIQUE candidate |
| 567 | for (const candidate of candidates) { |
| 568 | const count = countMatchingElements(candidate); |
| 569 | if (count === 1) { |
| 570 | const { score, ...result } = candidate; |
| 571 | return result; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // No unique candidate found - prefer ID if available (even if GUID-like) |
| 576 | const idAttr = element.getAttribute('id'); |
| 577 | if (idAttr) { |
| 578 | return { method: 'locator', selector: `#${CSS.escape(idAttr)}` }; |
| 579 | } |
| 580 | |
| 581 | // Fallback to best candidate even if not unique |
| 582 | const { score, ...result } = candidates[0]; |
| 583 | return result; |
| 584 | } |
| 585 | |
| 586 | // Keep backward compatibility with old function name |
| 587 | export function getPlaywrightMethod(element) { |
no test coverage detected