0
0
Cypresstesting~5 mins

Best practices for selectors in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
Why should you avoid using CSS classes or IDs that might change frequently as selectors in Cypress tests?
Because these selectors can change when the UI design changes, making tests fragile and prone to breaking even if the functionality is fine.
Click to reveal answer
beginner
What is the recommended attribute to use for selectors in Cypress tests?
Use custom data attributes like data-cy or data-test because they are stable and only used for testing, so they don't change with UI updates.
Click to reveal answer
intermediate
How does using semantic HTML elements help with selectors?
Semantic elements like <button> or <nav> make selectors clearer and improve accessibility, which benefits both users and tests.
Click to reveal answer
intermediate
What is a good practice when writing selectors to avoid brittle tests?
Write selectors that are specific enough to find the element but not too tied to styling or layout, for example, use data-cy='submit-button' instead of .btn-primary.
Click to reveal answer
beginner
Why should you avoid using text content as the only selector in Cypress tests?
Because text can change due to translations or content updates, making tests fail even if the element is still there and working.
Click to reveal answer
Which attribute is best to use for stable selectors in Cypress tests?
Adata-cy
Bclass
Cid
Dstyle
What is a downside of using CSS classes as selectors in tests?
AThey improve test speed
BThey are always unique
CThey might change with UI design updates
DThey are recommended by Cypress
Why avoid using visible text as the only selector?
AText is always unique
BText selectors are recommended for accessibility
CText selectors are faster
DText can change due to translations or updates
Which selector practice improves both test stability and accessibility?
AUsing inline styles
BUsing data attributes and semantic HTML
CUsing random IDs
DUsing only class names
What is a good way to write selectors to avoid brittle tests?
AUse custom data attributes with meaningful names
BUse very specific selectors tied to styling
CUse selectors based on element position only
DUse very general selectors like 'div'
Explain why using custom data attributes like data-cy is recommended for selectors in Cypress tests.
Think about what makes a selector less likely to break when the UI changes.
You got /3 concepts.
    Describe how semantic HTML elements and good selector practices contribute to both accessibility and test stability.
    Consider the benefits for users and testers.
    You got /3 concepts.