Test Overview
This test checks that a specific child element exists within a parent container on a webpage. It verifies that the child element is correctly nested inside the parent element using cy.parent() and cy.children() commands.
This test checks that a specific child element exists within a parent container on a webpage. It verifies that the child element is correctly nested inside the parent element using cy.parent() and cy.children() commands.
describe('Test cy.parent() and cy.children()', () => { it('verifies child element is inside the parent container', () => { cy.visit('https://example.cypress.io/commands/traversal') // Find the child element with class 'traversal-mark' cy.get('.traversal-mark') .should('exist') // Check its parent has class 'traversal-button-states' .parent() .should('have.class', 'traversal-button-states') // Then check the parent has children and one of them is the original child .children() .should('contain.text', 'Mark') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initializes the Cypress test environment | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io/commands/traversal' | The webpage with traversal commands examples is loaded | — | PASS |
| 3 | Find element with class '.traversal-mark' | The element with text 'Mark' inside the page is located | Check element exists | PASS |
| 4 | Get parent of '.traversal-mark' element | Parent element with class 'traversal-button-states' is selected | Verify parent has class 'traversal-button-states' | PASS |
| 5 | Get children of the parent element | All child elements inside the parent container are selected | Verify children contain text 'Mark' | PASS |
| 6 | Test ends successfully | All assertions passed, test completes | — | PASS |
cy.parent() do?cy.parent()cy.children() finds children, siblings are not targeted by cy.parent(), and it does not find the first child specifically.container in Cypress?children(), so cy.get('.container').children() is correct.parent() gets the parent, child() is not a valid Cypress command, and parents() gets all ancestors, not children.<div class='grandparent'>
<div class='parent'>
<span class='child'>Hello</span>
<span class='child'>World</span>
</div>
</div>cy.get('.child').parent().should('have.class', 'parent') assert?cy.get('.child') selects both span elements. parent() gets their immediate parent, which is the div with class 'parent'.cy.get('.list').children('.item').parent('.container')parent() usageparent() command in Cypress does not accept any selector argument; it always returns the immediate parent element.children() can filter by selector, so that part is valid. parent() does not return all ancestors, only immediate parent. children() returns direct children, not siblings.parent() command does not accept a selector argument -> Option A<ul class='menu'> <li class='item'>Home</li> <li class='item'>About</li> <li class='item'>Contact</li> </ul> <div class='footer'>Footer content</div>
<ul> element starting from the <li> elements with class item?.item elements<li class='item'> is a direct child of the <ul class='menu'> element.parent() to get immediate parentcy.get('.item').parent() correctly gets the immediate parent <ul class='menu'> element.children('ul') is wrong because <li> elements do not have <ul> children. parent().parent() goes too far up. children() gets children, not parent.Pass the test and this pattern moves unknown