0
0
Cypresstesting~20 mins

Best practices for selectors in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Selector Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why prefer data-* attributes for selectors?

In Cypress tests, why is it best to use data-* attributes as selectors instead of classes or IDs?

ABecause classes are always dynamically generated and cannot be used in selectors.
BBecause <code>data-*</code> attributes are less likely to change and do not affect styling or functionality.
CBecause IDs are not unique and can cause test failures.
DBecause <code>data-*</code> attributes automatically improve test speed.
Attempts:
2 left
💡 Hint

Think about what changes often in UI and what stays stable for testing.

assertion
intermediate
2:00remaining
Which selector best follows best practices?

Given a button with HTML: <button data-test='submit-btn' class='btn primary'>Submit</button>, which Cypress selector is best for stable tests?

Acy.get('[data-test="submit-btn"]')
Bcy.get('#submit-btn')
Ccy.get('button:contains("Submit")')
Dcy.get('.btn.primary')
Attempts:
2 left
💡 Hint

Look for the selector that is least likely to break if styles or text change.

Predict Output
advanced
2:00remaining
What is the test result of this selector usage?

Consider this Cypress test code snippet:

cy.get('[data-cy=login]').click()
cy.get('.btn-primary').should('be.visible')

If the data-cy attribute is missing but the class exists, what happens?

AThe first command fails with a <code>Cannot find element</code> error; the second passes if the button is visible.
BBoth commands pass because class selectors always work.
CBoth commands fail because <code>data-cy</code> is required for all selectors.
DThe test passes but clicks the wrong element.
Attempts:
2 left
💡 Hint

Think about what happens if a selector targets a missing attribute.

🔧 Debug
advanced
2:00remaining
Identify the problem with this selector

Review this Cypress selector code:

cy.get('button.submit-btn.active').click()

The test intermittently fails because the button is not found. What is the likely cause?

AThe button element cannot be clicked because it is disabled.
BThe selector is missing a <code>data-*</code> attribute, causing syntax errors.
CThe selector requires the button to have both classes <code>submit-btn</code> and <code>active</code>, which may not always be true.
DThe selector uses invalid CSS syntax and causes a runtime error.
Attempts:
2 left
💡 Hint

Consider how class combinations affect element matching.

framework
expert
3:00remaining
Which Cypress selector strategy best supports maintainable tests?

Choose the selector strategy that best supports maintainable and robust Cypress tests in a large project.

AUse text content selectors like <code>:contains()</code> to select elements by visible text.
BUse IDs exclusively because they are unique and fast to select.
CUse complex CSS selectors combining multiple classes and element types for precision.
DUse unique <code>data-cy</code> attributes for all interactive elements and avoid relying on classes or IDs.
Attempts:
2 left
💡 Hint

Think about what makes selectors stable and easy to maintain as UI changes.