MCPcopy
hub / github.com/codeceptjs/CodeceptJS / findByRole

Function findByRole

lib/helper/Puppeteer.js:3560–3586  ·  view source on GitHub ↗
(matcher, locator)

Source from the content-addressed store, hash-verified

3558}
3559
3560async function findByRole(matcher, locator) {
3561 const resolved = toLocatorConfig(locator, 'role')
3562 const roleSelector = buildRoleSelector(resolved)
3563
3564 if (!resolved.text && !resolved.name) {
3565 return matcher.$$(roleSelector)
3566 }
3567
3568 const allElements = await matcher.$$(roleSelector)
3569 const filtered = []
3570 const accessibleName = resolved.text ?? resolved.name
3571 const matcherFn = createRoleTextMatcher(accessibleName, resolved.exact === true)
3572
3573 for (const el of allElements) {
3574 const texts = await el.evaluate(e => {
3575 const ariaLabel = e.hasAttribute('aria-label') ? e.getAttribute('aria-label') : ''
3576 const labelText = e.id ? document.querySelector(`label[for="${e.id}"]`)?.textContent.trim() || '' : ''
3577 const placeholder = e.getAttribute('placeholder') || ''
3578 const innerText = e.innerText ? e.innerText.trim() : ''
3579 return [ariaLabel || labelText, placeholder, innerText]
3580 })
3581
3582 if (texts.some(text => matcherFn(text))) filtered.push(el)
3583 }
3584
3585 return filtered
3586}
3587
3588function toLocatorConfig(locator, key) {
3589 const matchedLocator = new Locator(locator, key)

Callers 1

selectOptionMethod · 0.70

Calls 7

toLocatorConfigFunction · 0.85
buildRoleSelectorFunction · 0.85
createRoleTextMatcherFunction · 0.85
$$Method · 0.80
evaluateMethod · 0.80
getAttributeMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected