Test Overview
This test opens a webpage, locates a parent element, then uses cy.find() to find a child button inside it. It clicks the button and verifies the button text changes to confirm the click worked.
This test opens a webpage, locates a parent element, then uses cy.find() to find a child button inside it. It clicks the button and verifies the button text changes to confirm the click worked.
describe('Test cy.find() within parent', () => { it('finds a button inside a parent and clicks it', () => { cy.visit('https://example.cypress.io/commands/actions') cy.get('.action-form') .find('button') .should('have.text', 'Submit') .click() .should('have.text', 'Clicked') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io/commands/actions' | Page loaded with form containing a button labeled 'Submit' | — | PASS |
| 3 | Find element with class '.action-form' using cy.get() | Parent form element found on page | — | PASS |
| 4 | Within the parent form, find the child 'button' element using cy.find() | Button element inside form located with text 'Submit' | Check button text is exactly 'Submit' | PASS |
| 5 | Click the found button | Button clicked, page updates button text to 'Clicked' | Verify button text changed to 'Clicked' | PASS |
| 6 | Test ends successfully | Button text confirmed changed, test passed | — | PASS |
cy.find() in Cypress tests?cy.find() rolecy.find() is used to look for elements inside a previously selected parent element, narrowing the search.cy.get() searches the whole page, while cy.find() limits to children of a parent element.submit-btn inside a div with id form-container using cy.find()?cy.get('#form-container') to select the div with the given id.find() on the parent to locate the child button.find('button.submit-btn') on the parent to get the button inside it.<div id='list'>
<ul>
<li class='item'>Apple</li>
<li class='item'>Banana</li>
<li class='item'>Cherry</li>
</ul>
</div>cy.get('#list').find('li.item').its('length')cy.get('#list') selects the div containing the list.li elements with class 'item' inside the div.find('li.item') selects all three list items: Apple, Banana, Cherry..its('length') returns the number of matched elements, which is 3.cy.get('.container').find('.button').click().find('.button') returns nothing, causing the error.div.card contains a button.delete. How do you click the delete button only inside the second card using cy.find()?cy.get('div.card').eq(1) selects the second card (index 1)..find('button.delete') searches only inside the selected card..click() clicks the delete button inside the second card only.Pass the test and this pattern moves unknown