Skip to content

Commit

Permalink
feat(cypress): add button role support to clickButton()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Nov 6, 2024
1 parent 9bf4411 commit d31f471
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/cypress/commands/clickButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,34 @@ function findButton(
return buttonElementByTitle as HTMLElement
}

const menuItemElement = findElementByText(`${preSelector}[role="menuitem"]`, label, {
const buttonRoleElement = findElementByText(`${preSelector}[role="button"]`, label, {
index,
inElement: prevSubjectElement
})
if (menuItemElement) {
return menuItemElement as HTMLElement
if (buttonRoleElement) {
return buttonRoleElement as HTMLElement
}

const buttonRoleElementByAriaLabel = prevSubjectElement
? prevSubjectElement.querySelectorAll(`${preSelector}[role="button"][aria-label="${label}"]`)[index]
: Cypress.$(`${preSelector}[role="button"][aria-label="${label}"]`).get(index)
if (buttonRoleElementByAriaLabel) {
return buttonRoleElementByAriaLabel as HTMLElement
}

const buttonRoleElementByTitle = prevSubjectElement
? prevSubjectElement.querySelectorAll(`${preSelector}[role="button"][title="${label}"]`)[index]
: Cypress.$(`${preSelector}[role="button"][title="${label}"]`).get(index)
if (buttonRoleElementByTitle) {
return buttonRoleElementByTitle as HTMLElement
}

const menuItemRoleElement = findElementByText(`${preSelector}[role="menuitem"]`, label, {
index,
inElement: prevSubjectElement
})
if (menuItemRoleElement) {
return menuItemRoleElement as HTMLElement
}

return undefined
Expand Down

0 comments on commit d31f471

Please sign in to comment.