Bird
0
0

What is the main advantage of using data-cy attributes instead of CSS classes for selecting elements in Cypress tests?

easy📝 Conceptual Q1 of 15
Cypress - Selecting Elements
What is the main advantage of using data-cy attributes instead of CSS classes for selecting elements in Cypress tests?
AThey replace the need for unique IDs in HTML
BThey automatically improve page load speed
CThey allow Cypress to run tests without a browser
DThey are less likely to change during UI redesigns, improving test reliability
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute purpose

    data-cy attributes are specifically added for testing purposes.
  2. Step 2: Compare with CSS classes

    CSS classes often change due to styling updates, causing flaky tests.
  3. Final Answer:

    They are less likely to change during UI redesigns, improving test reliability -> Option D
  4. Quick Check:

    Test selectors should be stable and independent of styling changes [OK]
Quick Trick: Use dedicated attributes for stable selectors [OK]
Common Mistakes:
  • Assuming classes never change
  • Believing data-cy improves performance
  • Confusing data-cy with IDs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes