* Build selector candidates (ported from buildNoTextCandidates and buildTextCandidates)
(element, testIdAttributeName = 'data-testid')
| 408 | * Build selector candidates (ported from buildNoTextCandidates and buildTextCandidates) |
| 409 | */ |
| 410 | function buildCandidates(element, testIdAttributeName = 'data-testid') { |
| 411 | const candidates = []; |
| 412 | const tagName = element.nodeName; |
| 413 | |
| 414 | // ============ NO-TEXT CANDIDATES (from buildNoTextCandidates) ============ |
| 415 | |
| 416 | // 1. Test ID attributes - highest priority |
| 417 | const testIdAttrs = ['data-testid', 'data-test-id', 'data-test']; |
| 418 | for (const attr of testIdAttrs) { |
| 419 | const value = element.getAttribute(attr); |
| 420 | if (value) { |
| 421 | const score = attr === testIdAttributeName ? kTestIdScore : kOtherTestIdScore; |
| 422 | candidates.push({ |
| 423 | method: 'getByTestId', |
| 424 | testId: value, |
| 425 | score |
| 426 | }); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // 2. Placeholder (for INPUT/TEXTAREA) |
| 431 | if (tagName === 'INPUT' || tagName === 'TEXTAREA') { |
| 432 | const placeholder = element.placeholder; |
| 433 | if (placeholder) { |
| 434 | candidates.push({ |
| 435 | method: 'getByPlaceholder', |
| 436 | placeholder, |
| 437 | score: kPlaceholderScore |
| 438 | }); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // 3. Labels |
| 443 | const labels = getElementLabels(element); |
| 444 | for (const label of labels) { |
| 445 | if (label.normalized) { |
| 446 | candidates.push({ |
| 447 | method: 'getByLabel', |
| 448 | label: label.normalized, |
| 449 | score: kLabelScore |
| 450 | }); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // 4. ARIA role without name |
| 455 | const ariaRole = getAriaRole(element); |
| 456 | if (ariaRole && !['none', 'presentation'].includes(ariaRole)) { |
| 457 | candidates.push({ |
| 458 | method: 'getByRole', |
| 459 | role: ariaRole, |
| 460 | score: kRoleWithoutNameScore |
| 461 | }); |
| 462 | } |
| 463 | |
| 464 | // 5. CSS ID (if not GUID-like) |
| 465 | const idAttr = element.getAttribute('id'); |
| 466 | if (idAttr && !isGuidLike(idAttr)) { |
| 467 | candidates.push({ |
no test coverage detected