0
0
Cypresstesting~5 mins

cy.siblings() and cy.closest() in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does cy.siblings() do in Cypress?

cy.siblings() selects all sibling elements of the current element. Siblings share the same parent.

Click to reveal answer
beginner
What is the purpose of cy.closest() in Cypress?

cy.closest() finds the nearest ancestor element that matches a selector, starting from the current element and moving up the DOM tree.

Click to reveal answer
intermediate
How does cy.siblings() differ from cy.closest()?

cy.siblings() selects elements at the same level (same parent), while cy.closest() selects the nearest matching ancestor (parent or higher).

Click to reveal answer
beginner
Example: How to select all siblings of a button with class .submit-btn?
cy.get('button.submit-btn').siblings()

This selects all elements that share the same parent as the button with class submit-btn.

Click to reveal answer
beginner
Example: How to find the closest <form> ancestor of an input field?
cy.get('input').closest('form')

This finds the nearest form element that contains the input.

Click to reveal answer
What does cy.siblings() select?
AThe first child element of the current element
BThe closest ancestor element matching a selector
CAll child elements of the current element
DAll sibling elements of the current element
What does cy.closest('div') do?
AFinds the nearest ancestor <code>div</code> element
BSelects all child <code>div</code> elements
CSelects all sibling <code>div</code> elements
DFinds the first child <code>div</code> element
Which command would you use to select elements at the same level as the current element?
Acy.closest()
Bcy.siblings()
Ccy.children()
Dcy.parent()
If you want to find the nearest section ancestor of an element, which command is correct?
Acy.get('section')
Bcy.siblings('section')
Ccy.closest('section')
Dcy.children('section')
True or False: cy.siblings() can select the current element itself.
AFalse
BTrue
COnly if specified
DDepends on the selector
Explain in your own words the difference between cy.siblings() and cy.closest().
Think about where the elements are located relative to the current element.
You got /3 concepts.
    Describe a real-life scenario where you would use cy.closest() in a test.
    Imagine you want to check something about a parent container of an element.
    You got /3 concepts.