cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.43k stars 3.14k forks source link

Update Selector Playground to prioritise suggestions from Testing Library for accessible queries #29779

Open ahayes91 opened 4 days ago

ahayes91 commented 4 days ago

What would you like?

Today I learned (in an interview with a potential QE candidate) about the Selector Playground feature in the Cypress GUI to help you find selectors, and that its suggestions are ordered in a certain priority. While the suggestions align with the concept of writing selectors that are resilient to change, they don't reflect the guiding principles of Testing Library for accessible, user-based queries. There is a short blurb in the Cypress Best Practices docs about the use of Testing Library for locators as a recommendation.

It would be awesome to go one step further and make the Selector Playground recommend Testing Library locators according to their best practice priority.

So, for a button like this:

<button
  id="main"
  class="btn btn-large"
  name="submission"
  role="button"
  data-cy="submit"
>
  Submit
</button>

I would love for the first suggestion to be cy.findByRole('button', {name: 'Submit'}) instead of cy.get('[data-cy="submit"]').

(One reason you might not want that would be in an application testing multiple languages - but then I'd argue your test should still be testing that your buttons are getting the appropriate labels for that language 😏 )

Why is this needed?

Other

No response

jennifer-shehane commented 4 days ago

Thanks for the feedback. You can edit the priority as a workaround today: https://docs.cypress.io/api/cypress-api/selector-playground-api#Arguments