← Back to libraryQuestion 174 of 468
🌲CypressBeginner

Selecting Elements and Best-Practice Selectors

📌 Definition:

Cypress selects elements with cy.get (CSS selectors) and cy.contains (by text). The recommended best practice is to target dedicated test attributes like data-cy or data-testid rather than brittle CSS/XPath.

📖 Detailed Explanation:

cy.get('.btn-primary') works but couples tests to styling that changes often; cy.contains('Submit') couples to copy that changes with translations. Cypress recommends adding data-cy (or data-test/data-testid) attributes purely for testing, e.g. <button data-cy='submit'>, and selecting cy.get('[data-cy=submit]'). This isolates tests from CSS/content churn and makes intent explicit. You can chain to scope selectors (cy.get('[data-cy=form]').find('input')), and .within() to scope commands to a container. Avoid overly-specific selectors and auto-generated class hashes.

🔑 Key Points:
  • Prefer data-cy / data-testid over CSS classes or text
  • cy.get = CSS selectors; cy.contains = by text
  • Scope with .find() and .within() to reduce brittleness
  • Avoid volatile selectors (styling classes, generated ids)
🌍 Real-World Example:

A design refactor renames every Bootstrap class; tests using cy.get('.btn.btn-lg.btn-primary') all break, while tests using cy.get('[data-cy=checkout]') keep passing because the test attribute was untouched.

🎯 Scenario-Based Interview Question:

Your team's Cypress tests break every sprint after CSS changes. What's the fix and why is it more stable?