Selectors help tests find elements on a webpage. Good selectors make tests reliable and easy to maintain.
Best practices for selectors in Cypress
cy.get('selector')
Use cy.get() to find elements by CSS selectors.
Choose selectors that are stable and unique to avoid test failures.
submit-button. IDs are best for uniqueness.cy.get('#submit-button')
data-cy. This is a recommended practice for test selectors.cy.get('[data-cy=login-input]')
nav-item that contains the text 'Home'. Useful when classes are shared.cy.get('.nav-item').contains('Home')
button element on the page. Use carefully as order can change.cy.get('button').eq(0)
This test visits a login page, types username and password using stable selectors, clicks login, and checks for a welcome message.
describe('Login form test', () => { it('should find and type in the username input', () => { cy.visit('https://example.com/login') cy.get('[data-cy=username-input]').type('testuser') cy.get('[data-cy=password-input]').type('password123') cy.get('#login-button').click() cy.get('.welcome-message').should('contain.text', 'Welcome, testuser') }) })
Avoid using selectors based on text that might change often.
Prefer custom attributes like data-cy for test selectors to separate test code from styling.
Do not rely on element order unless necessary, as it can make tests fragile.
Use unique and stable selectors like IDs or custom attributes.
Keep selectors simple and clear to make tests easy to read and maintain.
Test selectors should not depend on styling or text that changes frequently.
Practice
Solution
Step 1: Understand selector stability
Selectors should be stable and unique to avoid breaking tests when UI changes.Step 2: Evaluate options for stability
IDs and custom data attributes are stable and unique, unlike classes or text that can change.Final Answer:
Use unique IDs or custom data attributes for selectors. -> Option DQuick Check:
Stable selectors = IDs or data attributes [OK]
- Using CSS classes that change with styling
- Selecting by visible text that may vary
- Relying on element order which can shift
data-test='submit-btn' in Cypress?Solution
Step 1: Recall attribute selector syntax
In CSS selectors, attributes are selected with square brackets and quotes if needed.Step 2: Match correct Cypress syntax
cy.get('[data-test=submit-btn]') correctly uses attribute selector syntax.Final Answer:
cy.get('[data-test=submit-btn]') -> Option AQuick Check:
Attribute selectors use brackets [] [OK]
- Omitting brackets for attribute selectors
- Using ID or class selectors incorrectly
- Missing quotes around attribute values
<button data-cy='login-btn' class='btn primary'>Login</button>What will
cy.get('.btn.primary').click() do if the class changes to btn secondary later?Solution
Step 1: Understand selector dependency on class names
The selector '.btn.primary' depends on the button having both classes 'btn' and 'primary'.Step 2: Analyze effect of class change
If the class changes to 'btn secondary', the selector '.btn.primary' no longer matches, causing failure.Final Answer:
The test will fail because the selector no longer matches. -> Option AQuick Check:
Class selectors break if classes change [OK]
- Assuming class selectors are stable
- Expecting automatic fallback to other selectors
- Thinking text selectors are used automatically
cy.get('button.submit-btn') but the test fails because the button has no class submit-btn. How can you fix it following best practices?Solution
Step 1: Identify the problem with class selector
The button lacks the class 'submit-btn', so the selector fails.Step 2: Apply best practice for stable selectors
Using a custom attribute like data-cy='submit-btn' is stable and recommended.Final Answer:
Change selector to use a stable attribute like data-cy='submit-btn'. -> Option BQuick Check:
Stable selectors use custom attributes [OK]
- Relying on missing or changing classes
- Selecting too broadly causing flaky tests
- Using text selectors that may change
data-test='save-btn'. The form has multiple buttons with different data-test attributes. Which selector is best to ensure you click the correct button inside the form?Solution
Step 1: Understand the need for context-specific selection
Since multiple buttons have data-test attributes, selecting within the form narrows scope.Step 2: Choose selector that scopes to form and targets correct button
Using cy.get('form').find('[data-test=save-btn]') ensures the button inside the form is selected.Final Answer:
cy.get('form').find('[data-test=save-btn]').click() -> Option CQuick Check:
Scoped selectors avoid ambiguity [OK]
- Selecting buttons globally causing wrong clicks
- Using text selectors that may match multiple elements
- Using index-based selectors that can break easily
Ready to prove it?
Pass the test and this pattern moves unknown
