What if you could avoid clicking the wrong button every time in your tests?
Why cy.find() within parent in Cypress? - Purpose & Use Cases
Imagine you are testing a webpage with many similar buttons and inputs scattered all over. You try to click a button by searching the whole page manually, but you keep clicking the wrong one because they look alike.
Manually searching the entire page for elements is slow and confusing. You might click the wrong button or input because you can't easily tell which one belongs to which section. This causes mistakes and wastes time.
Using cy.find() lets you first select a specific parent section, then look only inside it for the element you want. This narrows down the search, making your tests faster, clearer, and less error-prone.
cy.get('button').click() // clicks first button on page, maybe wrong onecy.get('.parent-section').find('button').click() // clicks button only inside parent
You can precisely target elements inside specific parts of the page, making your tests reliable and easy to understand.
Testing a shopping cart where each product has its own 'Add to Cart' button. Using cy.find() inside the product container ensures you add the right product, not just the first button on the page.
Manual element selection can be confusing and error-prone.
cy.find() scopes your search inside a parent element.
This makes tests faster, clearer, and more reliable.
Practice
cy.find() in Cypress tests?Solution
Step 1: Understand
cy.find()rolecy.find()is used to look for elements inside a previously selected parent element, narrowing the search.Step 2: Compare with other commands
cy.get()searches the whole page, whilecy.find()limits to children of a parent element.Final Answer:
To search for child elements within a specific parent element -> Option CQuick Check:
cy.find() = child search inside parent [OK]
- Thinking cy.find() searches the whole page
- Confusing cy.find() with cy.get()
- Using cy.find() without a parent element first
submit-btn inside a div with id form-container using cy.find()?Solution
Step 1: Select the parent element correctly
Usecy.get('#form-container')to select the div with the given id.Step 2: Use
Callfind()on the parent to locate the child button.find('button.submit-btn')on the parent to get the button inside it.Final Answer:
cy.get('#form-container').find('button.submit-btn') -> Option BQuick Check:
Parent get + find child = cy.get('#form-container').find('button.submit-btn') [OK]
- Using cy.find() before cy.get() for parent
- Swapping order of get and find
- Using find on a selector string instead of a parent element
<div id='list'>
<ul>
<li class='item'>Apple</li>
<li class='item'>Banana</li>
<li class='item'>Cherry</li>
</ul>
</div>What will the following Cypress code return?
cy.get('#list').find('li.item').its('length')Solution
Step 1: Select the parent div with id 'list'
cy.get('#list')selects the div containing the list.Step 2: Find all
lielements with class 'item' inside the div.find('li.item')selects all three list items: Apple, Banana, Cherry.Step 3: Get the length property
.its('length')returns the number of matched elements, which is 3.Final Answer:
3 -> Option AQuick Check:
3 list items found inside parent [OK]
- Assuming find returns only one element
- Confusing .get() and .find() scopes
- Expecting zero because of wrong selector
cy.get('.container').find('.button').click()But the test fails with "Cannot read property 'click' of undefined". What is the likely cause?
Solution
Step 1: Understand the error message
"Cannot read property 'click' of undefined" means the element was not found, so click() fails.Step 2: Check the selectors used
If '.button' does not exist inside '.container', then.find('.button')returns nothing, causing the error.Final Answer:
The selector '.button' does not exist inside '.container' -> Option AQuick Check:
Missing child element causes undefined click [OK]
- Using find before get
- Assuming visibility causes this error
- Thinking selector syntax is wrong without checking
div.card contains a button.delete. How do you click the delete button only inside the second card using cy.find()?Solution
Step 1: Select the second card div
cy.get('div.card').eq(1)selects the second card (index 1).Step 2: Find the delete button inside that card
.find('button.delete')searches only inside the selected card.Step 3: Click the found button
.click()clicks the delete button inside the second card only.Final Answer:
cy.get('div.card').eq(1).find('button.delete').click() -> Option DQuick Check:
Get second card, then find button inside it [OK]
- Using find before get
- Selecting button globally then filtering
- Using eq() after find instead of before
Ready to prove it?
Pass the test and this pattern moves unknown
