Child commands in Cypress - Build an Automation Script
describe('Child commands test', () => { beforeEach(() => { cy.visit('http://localhost:3000/items'); }); it('should verify child elements inside #item-list', () => { cy.get('#item-list') .children('.item') .should('have.length', 5) .first() .should('contain.text', 'Item 1'); cy.get('#item-list') .children('.item') .last() .should('contain.text', 'Item 5'); }); });
The test starts by visiting the target URL in the beforeEach hook to ensure a fresh page load before each test.
We use cy.get('#item-list') to select the container by its id, which is a reliable and clear selector.
Then, .children('.item') fetches all direct child elements with the class item inside the container.
We assert the number of these child elements is exactly 5 using .should('have.length', 5).
Next, we check the first child element contains the text 'Item 1' using .first().should('contain.text', 'Item 1').
Similarly, we check the last child element contains the text 'Item 5' using .last().should('contain.text', 'Item 5').
This approach uses Cypress chaining and child commands properly, making the test clear, maintainable, and reliable.
Now add data-driven testing with 3 different containers each having different numbers of child items and verify their counts and first/last item texts.
Practice
.find() command do in Cypress testing?Solution
Step 1: Understand the purpose of
The.find().find()command is used to search for elements inside another element, meaning it looks for child elements.Step 2: Compare with other commands
Commands like.click(),.type(), and.wait()perform actions or wait, but do not locate child elements.Final Answer:
It locates child elements inside a parent element. -> Option DQuick Check:
.find()locates children = B [OK]
- Confusing .find() with .click() or .type()
- Thinking .find() waits for elements
- Using .find() without a parent element
button inside a div with class container using Cypress?Solution
Step 1: Understand chaining commands in Cypress
First,cy.get()selects the parent element. Then,.find()locates child elements inside it.Step 2: Analyze each option
cy.get('.container').find('button') correctly usescy.get('.container')to select the parent, then.find('button')to find the child button. Other options misuse the order or commands.Final Answer:
cy.get('.container').find('button') -> Option AQuick Check:
Parent first with get(), then child with find() = A [OK]
- Swapping .get() and .find() order
- Using .find() without cy.get() first
- Selecting child before parent
cy.get('ul.menu').find('li.active').should('have.length', 2)Solution
Step 1: Understand the selector chain
cy.get('ul.menu')selects theulelement with classmenu. Then.find('li.active')findslichildren with classactive.Step 2: Understand the assertion
.should('have.length', 2)asserts that exactly 2 suchli.activeelements exist inside theul.menu.Final Answer:
It selects alllielements insideul.menuwith classactiveand checks there are 2. -> Option AQuick Check:
Find children with class and check count = A [OK]
- Confusing parent and child selectors
- Thinking it clicks or types instead of asserting
- Misreading the assertion purpose
cy.find('.list').get('li').should('have.length', 3)Solution
Step 1: Check command order
cy.find()is not a valid starting command.cy.get()must be used first to select the parent element.Step 2: Correct usage
The correct order iscy.get('.list').find('li')to findlichildren inside the element with classlist.Final Answer:
The order of commands is incorrect; .get() should come before .find(). -> Option CQuick Check:
Use cy.get() first, then .find() = D [OK]
- Starting with .find() instead of cy.get()
- Assuming .find() works alone
- Ignoring command chaining rules
form with id login contains exactly 3 input fields that are visible. Which Cypress code correctly uses child commands to do this?Solution
Step 1: Select the parent form element
Usecy.get('#login')to select the form with id 'login'.Step 2: Find visible input children and assert count
Use.find('input:visible')to get visible input fields inside the form, then assert there are exactly 3 with.should('have.length', 3).Final Answer:
cy.get('#login').find('input:visible').should('have.length', 3) -> Option BQuick Check:
Parent get(), then find visible inputs, then check length = C [OK]
- Using .find() before cy.get()
- Checking visibility after length assertion
- Incorrect selector order
Ready to prove it?
Pass the test and this pattern moves unknown
