Child commands in Cypress are commands that operate on elements found within a parent element. They help you narrow down your search to elements inside a specific container.
Click to reveal answer
beginner
How do you chain child commands in Cypress?
You chain child commands by first selecting a parent element, then using commands like .find() or .children() to get elements inside it.
Click to reveal answer
intermediate
What is the difference between .find() and .children() in Cypress?
.find() searches for all descendants matching the selector inside the parent, while .children() only gets direct children elements.
Click to reveal answer
beginner
Why use child commands instead of global selectors in Cypress?
Using child commands limits the search to a specific part of the page, making tests faster and more reliable by avoiding selecting wrong elements.
Click to reveal answer
beginner
Example: How to get all li elements inside a ul with id menu using child commands?
Use cy.get('#menu').find('li'). This first gets the ul with id menu, then finds all li inside it.
Click to reveal answer
Which Cypress command gets only direct children of an element?
A.find()
B.children()
C.parent()
D.siblings()
✗ Incorrect
.children() gets only direct children, while .find() gets all descendants.
What does cy.get('.list').find('li') do?
AFinds all <code>li</code> elements on the page
BFinds direct children with class <code>list</code>
CFinds elements with class <code>list</code> inside <code>li</code>
DFinds <code>li</code> elements inside elements with class <code>list</code>
✗ Incorrect
It first gets elements with class list, then finds li inside them.
Why is chaining child commands useful in Cypress?
ATo select elements globally
BTo slow down tests
CTo select elements faster and more precisely
DTo avoid using selectors
✗ Incorrect
Chaining narrows the search, making tests faster and more reliable.
Which command would you use to get all descendants matching a selector inside a parent element?
A.find()
B.children()
C.parent()
D.closest()
✗ Incorrect
.find() searches all descendants matching the selector.
If you want to get only direct children elements, which command is best?
A.children()
B.siblings()
C.find()
D.next()
✗ Incorrect
.children() returns only direct children.
Explain how child commands work in Cypress and why they are useful.
Think about selecting elements inside a container.
You got /3 concepts.
Describe the difference between .find() and .children() commands in Cypress.
Consider the depth of element selection.
You got /3 concepts.
Practice
(1/5)
1. What does the .find() command do in Cypress testing?
easy
A. It clicks on an element.
B. It types text into an input field.
C. It waits for a page to load.
D. It locates child elements inside a parent element.
Solution
Step 1: Understand the purpose of .find()
The .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 D
Quick Check:
.find() locates children = B [OK]
Hint: Remember: .find() searches inside a parent element [OK]
Common Mistakes:
Confusing .find() with .click() or .type()
Thinking .find() waits for elements
Using .find() without a parent element
2. Which of the following is the correct syntax to find a button inside a div with class container using Cypress?
easy
A. cy.get('.container').find('button')
B. cy.find('.container').get('button')
C. cy.get('button').find('.container')
D. cy.find('button').get('.container')
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 uses cy.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 A
Quick Check:
Parent first with get(), then child with find() = A [OK]
Hint: Use cy.get() first, then .find() for child elements [OK]
C. The order of commands is incorrect; .get() should come before .find().
D. The assertion .should('have.length', 3) is not valid.
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 is cy.get('.list').find('li') to find li children inside the element with class list.
Final Answer:
The order of commands is incorrect; .get() should come before .find(). -> Option C
Quick Check:
Use cy.get() first, then .find() = D [OK]
Hint: Always start with cy.get() before .find() [OK]
Common Mistakes:
Starting with .find() instead of cy.get()
Assuming .find() works alone
Ignoring command chaining rules
5. You want to verify that a form with id login contains exactly 3 input fields that are visible. Which Cypress code correctly uses child commands to do this?
hard
A. cy.find('#login').get('input:visible').should('have.length', 3)
B. cy.get('#login').find('input:visible').should('have.length', 3)
C. cy.get('#login').find('input').should('have.length', 3).and('be.visible')
D. cy.get('input:visible').find('#login').should('have.length', 3)
Solution
Step 1: Select the parent form element
Use cy.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 B
Quick Check:
Parent get(), then find visible inputs, then check length = C [OK]
Hint: Chain get() then find(':visible') before asserting length [OK]
Common Mistakes:
Using .find() before cy.get()
Checking visibility after length assertion
Incorrect selector order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.