Discover how a tiny change in choosing selectors can save hours of test failures!
Why Best practices for selectors in Cypress? - Purpose & Use Cases
Imagine you are testing a website by clicking buttons and filling forms manually. You try to find elements by guessing their names or looking at their colors. Sometimes you pick the wrong button or miss a field because it looks similar to another.
Manually picking elements by their look or random attributes is slow and full of mistakes. If the website changes colors or layout, your guesses break. You waste time fixing tests that fail for no real reason.
Using best practices for selectors means choosing stable, clear ways to find elements, like unique IDs or data attributes. This makes tests reliable and easy to maintain, even if the page design changes.
cy.get('.btn-primary').click()cy.get('[data-cy="submit-button"]').click()It enables writing tests that keep working smoothly as the website grows and changes, saving time and frustration.
For example, a shopping site uses data attributes like data-cy='add-to-cart' on buttons. Testers can click the right button every time, even if the style or position changes.
Manual element picking is slow and error-prone.
Best selectors use stable, unique attributes like data-cy.
Good selectors make tests reliable and easy to update.