Verify parent and children elements using cy.parent() and cy.children()
Preconditions (2)
✅ Expected Result: The test confirms that the <ul> element has exactly 3 <li> children and that the parent of the first <li> is the <ul> with id 'menu'.
describe('Test cy.parent() and cy.children()', () => { it('should verify parent and children elements correctly', () => { cy.visit('http://localhost:3000/menu'); // Get the <ul> element with id 'menu' cy.get('#menu') // Get all <li> children .children('li') // Assert there are exactly 3 children .should('have.length', 3) // Select the first <li> child .first() // Get its parent element .parent() // Assert the parent has id 'menu' .should('have.id', 'menu'); }); });
The test starts by visiting the target page with cy.visit(). Then it locates the <ul> element by its id menu using cy.get('#menu'). Next, cy.children('li') fetches all <li> child elements of the <ul>. We assert that there are exactly 3 children using should('have.length', 3). Then, we select the first child with first() and get its parent element using cy.parent(). Finally, we assert that the parent element has the id menu with should('have.id', 'menu'). This confirms the parent-child relationship is correct.
Now add data-driven testing to verify parent and children for three different menu structures with varying number of <li> items.
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