Discover how a tiny change in choosing selectors can save hours of test failures!
Why Best practices for selectors in Cypress? - Purpose & Use Cases
Imagine you are testing a website by clicking buttons and filling forms manually. You try to find elements by guessing their names or looking at their colors. Sometimes you pick the wrong button or miss a field because it looks similar to another.
Manually picking elements by their look or random attributes is slow and full of mistakes. If the website changes colors or layout, your guesses break. You waste time fixing tests that fail for no real reason.
Using best practices for selectors means choosing stable, clear ways to find elements, like unique IDs or data attributes. This makes tests reliable and easy to maintain, even if the page design changes.
cy.get('.btn-primary').click()cy.get('[data-cy="submit-button"]').click()It enables writing tests that keep working smoothly as the website grows and changes, saving time and frustration.
For example, a shopping site uses data attributes like data-cy='add-to-cart' on buttons. Testers can click the right button every time, even if the style or position changes.
Manual element picking is slow and error-prone.
Best selectors use stable, unique attributes like data-cy.
Good selectors make tests reliable and easy to update.
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
