Hi Everyone, I need to have some help from you.Is ...
# best-practices
c
Hi Everyone, I need to have some help from you.Is there any plugin of Cypress or ways to get accessible names and role : getComputedRole/getComputedLabel value?
w
Hi Lucy, I use Cypress Testing Library for this kind of testing https://testing-library.com/docs/cypress-testing-library/intro/ I use the accessible role, name, label as part of the locator itself, sometimes in combination with another locator, for example
Copy code
cy.findByLabelText('First name', {
      selector: '[data-test="header-form-field"]',
    }).click()
In many cases I can simplify by using
cy.contains()
. For example:
cy.contains('button', 'Log In')
or
cy.contains('a', 'Home')
And I prefer these to asserting the role, since these elements have roles by default and I want developers using the real elements, not putting
role='button'
on a div. I don't believe it's possible to get the true computed role of an element, as it appears in the accessibility tree, from JS at the moment, but Testing Library style locators are better in my book anyway.