From d31f471c4022b9284332db23a963e20558c06c1b Mon Sep 17 00:00:00 2001 From: Ivan Gabriele Date: Wed, 6 Nov 2024 09:58:43 +0100 Subject: [PATCH] feat(cypress): add button role support to clickButton() --- src/cypress/commands/clickButton.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/cypress/commands/clickButton.ts b/src/cypress/commands/clickButton.ts index 27230a87e..578b28e9f 100644 --- a/src/cypress/commands/clickButton.ts +++ b/src/cypress/commands/clickButton.ts @@ -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