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?
Think about which selector is least affected by style or layout changes.
Using data-cy attributes is best practice for stable tests because they are dedicated for testing and unlikely to change with UI redesigns. Other selectors like classes or element order can change often.
You want to check that a success message is visible after form submission. The message has data-cy="success-msg". Which assertion is correct?
Use the exact data-cy attribute and check visibility.
Option B uses the stable data-cy selector and asserts the element is visible, which matches the test goal. Other options either use unstable selectors or wrong assertions.
What will be the result of this Cypress test snippet?
cy.get('[data-cy="login-button"]').click() cy.get('[data-cy="error-msg"]').should('contain.text', 'Invalid credentials')
Consider what the selectors do and what the assertions check.
The test clicks the login button identified by data-cy="login-button" and then asserts the error message with data-cy="error-msg" contains the expected text. If the message appears, the test passes.
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?
Think about asynchronous UI changes and timing.
The test clicks submit, then checks the spinner is gone, then checks success message. If the spinner removal is slow, the success message check may run too early causing intermittent failure. Proper waiting or retries are needed.
In a large Cypress test suite, what is the best way to manage data-cy selectors for maintainability and stability?
Think about how to avoid duplication and ease updates.
Defining data-cy selectors as constants in one place helps keep tests consistent and makes updates easier if selectors change. Hardcoding or using classes/text can cause brittle tests.