Discover how to stop guessing and start finding exactly the elements you need in your tests!
Why cy.siblings() and cy.closest() in Cypress? - Purpose & Use Cases
Imagine you are testing a web page manually and need to find elements related to a specific button, like its nearby buttons or the closest container. You have to look around the page, click many times, and guess which elements are connected. This takes a lot of time and can be confusing.
Manually searching for related elements is slow and easy to mess up. You might miss some siblings or pick the wrong container. It's hard to repeat the same steps exactly every time, so your tests become unreliable and frustrating.
Using cy.siblings() and cy.closest() in Cypress lets you quickly and precisely find elements near or above your target element in the page structure. This makes your tests faster, clearer, and more reliable without guessing or clicking around.
cy.get('button').parent().find('button') // manually searching siblings
cy.get('button').siblings() // directly gets sibling elements cy.get('button').closest('.container') // finds nearest container
It enables writing clear, fast tests that easily find related elements based on their position in the page, making your testing smarter and less error-prone.
For example, when testing a form, you can quickly check if error messages next to an input appear by finding its siblings, or find the closest form container to verify its state.
Manual element searching is slow and unreliable.
cy.siblings() and cy.closest() simplify finding related elements.
These commands make tests faster, clearer, and easier to maintain.
Practice
cy.siblings() do in a test?Solution
Step 1: Understand the purpose of
This command selects all elements that share the same parent as the current element, meaning they are at the same level in the DOM tree.cy.siblings()Step 2: Compare with other options
cy.closest()finds a parent, not siblings. Child elements are found withcy.children(). Finding all elements on the page is not related.Final Answer:
Finds all elements at the same level as the current element -> Option DQuick Check:
cy.siblings()= same-level elements [OK]
- Confusing siblings with parents
- Thinking siblings are child elements
- Assuming it finds all elements on the page
.container using Cypress?Solution
Step 1: Identify the element to start from
You must start from a known element, for examplecy.get('.child'), then find its closest parent matching.container.Step 2: Use
The correct syntax isclosest()with selectorcy.get('.child').closest('.container'). cy.get('.container').closest() is wrong becauseclosest()needs to be called on an element, not on the selector itself. cy.closest('.container') is invalid syntax. cy.siblings('.container') usessiblings(), which finds siblings, not parents.Final Answer:
cy.get('.child').closest('.container') -> Option CQuick Check:
Start element + closest('.container') = cy.get('.child').closest('.container') [OK]
- Calling closest() without a starting element
- Using siblings() instead of closest()
- Passing no selector to closest()
<div class='container'>
<ul>
<li class='item selected'>Item 1</li>
<li class='item'>Item 2</li>
<li class='item'>Item 3</li>
</ul>
</div>What will
cy.get('.selected').siblings('.item').should('have.length', ?) assert?Solution
Step 1: Identify siblings of the selected element
The element with class.selectedis the first<li>. Its siblings with class.itemare the other two<li>elements.Step 2: Count matching siblings
There are exactly 2 siblings with class.itembesides the selected one.Final Answer:
2 -> Option AQuick Check:
Siblings with class '.item' count = 2 [OK]
- Counting the selected element itself
- Confusing siblings with children
- Miscounting the number of siblings
cy.get('.button').closest('.container').siblings('.container').click()But the test fails with an error that no elements are found. What is the likely problem?
Solution
Step 1: Understand the chaining of commands
cy.get('.button')finds the button,closest('.container')finds its nearest parent container, thensiblings('.container')looks for containers at the same level as that parent.Step 2: Reason about the DOM structure
If no sibling containers exist next to the closest parent,siblings('.container')returns empty, causingclick()to fail.Final Answer:
There are no sibling elements with class '.container' next to the closest parent -> Option BQuick Check:
No siblings found = test fails [OK]
- Thinking closest() can't chain after get()
- Believing siblings() works on parents
- Assuming click() is invalid after siblings()
Solution
Step 1: Identify the target buttons
You want siblings of the clicked button that share the same parent container, sosiblings('.button')after selecting the clicked button is correct.Step 2: Analyze other options
cy.get('.button.clicked').closest('.container').siblings('.button') finds sibling containers, not buttons. cy.get('.button.clicked').closest('.container').find('.button').siblings() finds buttons inside the container but then calls siblings() without a selector, which may include unrelated elements. cy.get('.button.clicked').siblings().closest('.container') finds siblings first, then closest container, which does not select sibling buttons correctly.Final Answer:
cy.get('.button.clicked').siblings('.button') -> Option AQuick Check:
Siblings with class '.button' = cy.get('.button.clicked').siblings('.button') [OK]
- Using closest() before siblings() incorrectly
- Selecting sibling containers instead of buttons
- Calling siblings() without a selector
Ready to prove it?
Pass the test and this pattern moves unknown
