0
0
Cypresstesting~20 mins

data-cy attributes for test stability in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data-Cy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Choosing the best locator for stable tests

You want to select a button in your Cypress test that is least likely to break if the UI changes. Which locator is the most stable?

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

Think about which selector is least affected by style or layout changes.

assertion
intermediate
2:00remaining
Correct assertion using data-cy attribute

You want to check that a success message is visible after form submission. The message has data-cy="success-msg". Which assertion is correct?

Acy.get('[data-cy="success-msg"]').should('not.exist')
Bcy.get('[data-cy="success-msg"]').should('be.visible')
Ccy.get('div').contains('Success').should('be.visible')
Dcy.get('.success-msg').should('contain', 'Success')
Attempts:
2 left
💡 Hint

Use the exact data-cy attribute and check visibility.

Predict Output
advanced
2:00remaining
Output of Cypress test with data-cy selectors

What will be the result of this Cypress test snippet?

Cypress
cy.get('[data-cy="login-button"]').click()
cy.get('[data-cy="error-msg"]').should('contain.text', 'Invalid credentials')
ATest passes if error message 'Invalid credentials' appears after clicking login
BTest fails because data-cy selectors are invalid syntax
CTest passes even if error message is missing
DTest fails due to missing .click() on error message
Attempts:
2 left
💡 Hint

Consider what the selectors do and what the assertions check.

🔧 Debug
advanced
2:00remaining
Why does this test fail intermittently?

This Cypress test sometimes fails:

cy.get('[data-cy="submit-btn"]').click()
cy.get('[data-cy="loading-spinner"]').should('not.exist')
cy.get('[data-cy="success-msg"]').should('be.visible')

What is the most likely cause?

AThe data-cy attributes are misspelled
BThe click command is missing a .then() callback
CThe test does not wait for the spinner to disappear before checking success message
DThe success message is checked before clicking submit
Attempts:
2 left
💡 Hint

Think about asynchronous UI changes and timing.

framework
expert
3:00remaining
Best practice for using data-cy attributes in a large test suite

In a large Cypress test suite, what is the best way to manage data-cy selectors for maintainability and stability?

AAvoid using <code>data-cy</code> attributes and rely on element text content
BHardcode <code>data-cy</code> selectors directly in each test for clarity
CUse CSS classes instead of <code>data-cy</code> attributes to reduce HTML clutter
DDefine all <code>data-cy</code> selectors as constants in a separate file and import them in tests
Attempts:
2 left
💡 Hint

Think about how to avoid duplication and ease updates.