0
0
Cypresstesting~10 mins

data-cy attributes for test stability in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a login button with a data-cy attribute can be found and clicked reliably. It verifies that using data-cy attributes helps keep tests stable even if other page elements change.

Test Code - Cypress
Cypress
describe('Login Button Test', () => {
  it('should find and click the login button using data-cy attribute', () => {
    cy.visit('https://example.com/login');
    cy.get('[data-cy="login-button"]').click();
    cy.url().should('include', '/dashboard');
  });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner initialized-PASS
2Browser opens and navigates to https://example.com/loginLogin page is loaded with login button having data-cy='login-button'-PASS
3Find element with selector [data-cy=login-button]Login button element is found using data-cy attributeElement exists and is visiblePASS
4Click the login buttonLogin button is clicked, triggering navigation-PASS
5Check that URL includes '/dashboard' after clickBrowser navigated to dashboard pageURL contains '/dashboard'PASS
Failure Scenario
Failing Condition: The element with data-cy='login-button' is missing or changed
Execution Trace Quiz - 3 Questions
Test your understanding
Why is using data-cy attributes recommended for locating elements in tests?
AThey are less likely to change and keep tests stable
BThey make the page load faster
CThey improve the visual style of the page
DThey automatically fix broken tests
Key Result
Using dedicated data-cy attributes for element selectors makes tests more stable and less likely to break when UI changes, because these attributes are meant only for testing and do not affect styling or functionality.