Using data-cy attributes helps keep tests stable and easy to maintain. They act like special name tags for elements that tests can find reliably.
data-cy attributes for test stability in Cypress
<element data-cy="unique-name">...</element> // In Cypress test: cy.get('[data-cy="unique-name"]')
The data-cy attribute is a custom HTML attribute used only for testing.
Use unique and descriptive names for data-cy values to avoid confusion.
data-cy attribute and how to click it in a test.<button data-cy="submit-button">Submit</button> // Cypress test cy.get('[data-cy="submit-button"]').click()
data-cy and text is typed into it.<input data-cy="email-input" type="email" /> // Cypress test cy.get('[data-cy="email-input"]').type('user@example.com')
<div data-cy="user-profile"> <p>Name: John</p> </div> // Cypress test cy.get('[data-cy="user-profile"]').should('contain', 'John')
This test visits a page, clicks the login button identified by data-cy, and checks if the URL changes to include '/dashboard'.
// HTML snippet <button data-cy="login-button">Log In</button> // Cypress test describe('Login Button Test', () => { it('clicks the login button', () => { cy.visit('https://example.com') cy.get('[data-cy="login-button"]').click() cy.url().should('include', '/dashboard') }) })
Do not use data-cy values that might change often, like user names or dynamic content.
Keep data-cy attributes only for testing to avoid mixing test code with app logic.
Using data-cy makes tests less fragile compared to using classes or IDs that change with design.
data-cy attributes help tests find elements reliably.
They keep tests stable even if the page design changes.
Use clear, unique names for easy test maintenance.
Practice
data-cy attributes recommended for locating elements in Cypress tests?Solution
Step 1: Understand the purpose of
data-cyattributesdata-cyattributes are used as custom selectors in tests to find elements reliably.Step 2: Recognize why stability matters in tests
Usingdata-cykeeps tests stable because these attributes don't change when the page design or CSS changes.Final Answer:
They provide stable and unique selectors that don't change with UI updates. -> Option CQuick Check:
Stable selectors = They provide stable and unique selectors that don't change with UI updates. [OK]
- Thinking
data-cyaffects page style - Confusing
data-cywith test data generation - Assuming
data-cyimproves performance
data-cy='submit-button' in Cypress?Solution
Step 1: Recall the syntax for attribute selectors in Cypress
To select an element by attribute, usecy.get('[attribute=value]').Step 2: Match the attribute and value correctly
The attribute isdata-cyand value issubmit-button, so the selector is[data-cy=submit-button].Final Answer:
cy.get('[data-cy=submit-button]') -> Option AQuick Check:
Attribute selector syntax = cy.get('[data-cy=submit-button]') [OK]
- Omitting square brackets in selector
- Using ID or class selectors instead of attribute
- Not quoting attribute values when needed
<button data-cy="login-btn">Login</button>
What will the following Cypress command do?
cy.get('[data-cy=login-btn]').click()Solution
Step 1: Understand the selector usage
The commandcy.get('[data-cy=login-btn]')selects the element with attributedata-cyequal tologin-btn.Step 2: Understand the action performed
The.click()method clicks on the selected element, which is the login button.Final Answer:
Find the button with data-cy 'login-btn' and click it. -> Option AQuick Check:
Selector + click = Find the button with data-cy 'login-btn' and click it. [OK]
- Assuming selector is invalid syntax
- Thinking it clicks all buttons
- Confusing attribute selector with tag selector
cy.get('[data-cy=submit]').click()But the test fails with "Cannot find element" error. What is the most likely cause?
Solution
Step 1: Check the selector correctness
The selector[data-cy=submit]is correct syntax for attribute selection.Step 2: Identify why element is not found
If the test fails to find the element, it likely means the element does not have thedata-cy='submit'attribute in the HTML.Final Answer:
The element does not have adata-cy='submit'attribute. -> Option BQuick Check:
Missing attribute = The element does not have adata-cy='submit'attribute. [OK]
- Blaming click() method for failure
- Using wrong selector syntax
- Assuming Cypress can't click form elements
data-cy attributes. Which practice will best improve test stability?Solution
Step 1: Understand the goal of
They help tests find elements reliably and keep tests stable even if UI changes.data-cyattributesStep 2: Identify best practice for stability
Using unique, descriptivedata-cyattributes on important elements ensures selectors remain stable and easy to maintain.Final Answer:
Add unique, descriptivedata-cyattributes to key elements and use them in tests. -> Option DQuick Check:
Unique descriptive attributes = Add unique, descriptivedata-cyattributes to key elements and use them in tests. [OK]
data-cy names for stable tests [OK]- Adding attributes only to buttons
- Relying on CSS classes that may change
- Using random values that break selectors
Ready to prove it?
Pass the test and this pattern moves unknown
