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.
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.
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'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | — | PASS |
| 2 | Browser opens and navigates to https://example.com/login | Login page is loaded with login button having data-cy='login-button' | — | PASS |
| 3 | Find element with selector [data-cy=login-button] | Login button element is found using data-cy attribute | Element exists and is visible | PASS |
| 4 | Click the login button | Login button is clicked, triggering navigation | — | PASS |
| 5 | Check that URL includes '/dashboard' after click | Browser navigated to dashboard page | URL contains '/dashboard' | PASS |
data-cy attributes recommended for locating elements in Cypress tests?data-cy attributesdata-cy attributes are used as custom selectors in tests to find elements reliably.data-cy keeps tests stable because these attributes don't change when the page design or CSS changes.data-cy affects page styledata-cy with test data generationdata-cy improves performancedata-cy='submit-button' in Cypress?cy.get('[attribute=value]').data-cy and value is submit-button, so the selector is [data-cy=submit-button].<button data-cy="login-btn">Login</button>
cy.get('[data-cy=login-btn]').click()cy.get('[data-cy=login-btn]') selects the element with attribute data-cy equal to login-btn..click() method clicks on the selected element, which is the login button.cy.get('[data-cy=submit]').click()[data-cy=submit] is correct syntax for attribute selection.data-cy='submit' attribute in the HTML.data-cy='submit' attribute. -> Option Bdata-cy='submit' attribute. [OK]data-cy attributes. Which practice will best improve test stability?data-cy attributesdata-cy attributes on important elements ensures selectors remain stable and easy to maintain.data-cy attributes to key elements and use them in tests. -> Option Ddata-cy attributes to key elements and use them in tests. [OK]data-cy names for stable tests [OK]Pass the test and this pattern moves unknown