Verify login functionality using data-cy attributes
Preconditions (4)
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard'
describe('Login Test Using data-cy Attributes', () => { beforeEach(() => { cy.visit('/login'); }); it('should login successfully using data-cy selectors', () => { cy.get('[data-cy="email-input"]').type('user@example.com'); cy.get('[data-cy="password-input"]').type('Password123!'); cy.get('[data-cy="login-button"]').should('be.visible').and('be.enabled').click(); cy.url().should('include', '/dashboard'); }); });
This Cypress test visits the login page before each test to ensure a fresh start.
It uses cy.get('[data-cy="..."]') to select elements by their data-cy attributes, which are stable and unlikely to change, making tests more reliable.
The test types the email and password into the respective inputs, then checks that the login button is visible and enabled before clicking it.
Finally, it asserts that the URL includes '/dashboard' to confirm successful login.
Now add data-driven testing with 3 different sets of login credentials using data-cy selectors
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