Bird
Raised Fist0
Cypresstesting~20 mins

cy.siblings() and cy.closest() in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cypress DOM Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What elements does cy.siblings() select?
Given the following HTML structure:
<ul>
  <li class="item">Item 1</li>
  <li class="item selected">Item 2</li>
  <li class="item">Item 3</li>
</ul>

And the Cypress command:
cy.get('.selected').siblings()

Which elements will be selected by cy.siblings()?
Cypress
cy.get('.selected').siblings()
ANo elements are selected
BOnly the &lt;li&gt; element with text 'Item 2'
CAll &lt;li&gt; elements including 'Item 2'
DThe &lt;li&gt; elements with text 'Item 1' and 'Item 3'
Attempts:
2 left
💡 Hint
Remember, siblings() selects elements sharing the same parent except the element itself.
❓ Predict Output
intermediate
2:00remaining
What element does cy.closest() select?
Given the HTML:
<div class="container">
  <section>
    <article class="post">
      <p class="text">Hello</p>
    </article>
  </section>
</div>

And the Cypress command:
cy.get('.text').closest('section')

Which element will be selected by closest('section')?
Cypress
cy.get('.text').closest('section')
AThe &lt;article&gt; element with class 'post'
BThe &lt;section&gt; element that contains the &lt;p&gt;
CThe &lt;div&gt; with class 'container'
DThe &lt;p&gt; element with class 'text'
Attempts:
2 left
💡 Hint
The closest() command finds the nearest ancestor matching the selector.
❓ assertion
advanced
2:00remaining
Which assertion correctly verifies cy.siblings() selection count?
You want to check that the siblings of the element with id 'active' are exactly 3.
Which assertion is correct?
Cypress
cy.get('#active').siblings()
Acy.get('#active').siblings().should('have.length', 3)
Bcy.get('#active').siblings().should('contain.length', 3)
Ccy.get('#active').siblings().should('have.count', 3)
Dcy.get('#active').siblings().should('length', 3)
Attempts:
2 left
💡 Hint
Check the correct Cypress assertion syntax for length.
🔧 Debug
advanced
2:00remaining
Why does cy.closest() fail to find the element?
Given this code:
cy.get('.button').closest('.container')

But the test fails because no element is found.
HTML snippet:
<div class="wrapper">
  <button class="button">Click</button>
</div>

Why does closest('.container') fail?
Cypress
cy.get('.button').closest('.container')
ABecause there is no ancestor with class 'container' wrapping the button
BBecause <code>closest()</code> only searches siblings, not ancestors
CBecause the selector '.button' is incorrect
DBecause <code>closest()</code> requires a tag name, not a class selector
Attempts:
2 left
💡 Hint
Check the HTML structure and the selector used in closest().
🧠 Conceptual
expert
3:00remaining
How do cy.siblings() and cy.closest() differ in element selection?
Choose the best description of the difference between cy.siblings() and cy.closest().
A<code>cy.siblings()</code> selects the element itself; <code>cy.closest()</code> selects child elements.
B<code>cy.siblings()</code> selects all ancestor elements; <code>cy.closest()</code> selects all sibling elements.
C<code>cy.siblings()</code> selects elements sharing the same parent except the element itself; <code>cy.closest()</code> selects the nearest ancestor matching a selector.
D<code>cy.siblings()</code> selects descendants; <code>cy.closest()</code> selects elements with the same class.
Attempts:
2 left
💡 Hint
Think about the relationship in the DOM tree each command targets.

Practice

(1/5)
1. What does the Cypress command cy.siblings() do in a test?
easy
A. Finds all elements on the page
B. Finds the closest parent element matching a selector
C. Finds child elements inside the current element
D. Finds all elements at the same level as the current element

Solution

  1. Step 1: Understand the purpose of cy.siblings()

    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.
  2. Step 2: Compare with other options

    cy.closest() finds a parent, not siblings. Child elements are found with cy.children(). Finding all elements on the page is not related.
  3. Final Answer:

    Finds all elements at the same level as the current element -> Option D
  4. Quick Check:

    cy.siblings() = same-level elements [OK]
Hint: Siblings share the same parent element [OK]
Common Mistakes:
  • Confusing siblings with parents
  • Thinking siblings are child elements
  • Assuming it finds all elements on the page
2. Which of the following is the correct syntax to find the closest parent with class .container using Cypress?
easy
A. cy.get('.container').closest()
B. cy.closest('.container')
C. cy.get('.child').closest('.container')
D. cy.siblings('.container')

Solution

  1. Step 1: Identify the element to start from

    You must start from a known element, for example cy.get('.child'), then find its closest parent matching .container.
  2. Step 2: Use closest() with selector

    The correct syntax is cy.get('.child').closest('.container'). cy.get('.container').closest() is wrong because closest() needs to be called on an element, not on the selector itself. cy.closest('.container') is invalid syntax. cy.siblings('.container') uses siblings(), which finds siblings, not parents.
  3. Final Answer:

    cy.get('.child').closest('.container') -> Option C
  4. Quick Check:

    Start element + closest('.container') = cy.get('.child').closest('.container') [OK]
Hint: Call closest() on a found element with selector [OK]
Common Mistakes:
  • Calling closest() without a starting element
  • Using siblings() instead of closest()
  • Passing no selector to closest()
3. Given this HTML snippet:
<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?
medium
A. 2
B. 3
C. 1
D. 0

Solution

  1. Step 1: Identify siblings of the selected element

    The element with class .selected is the first <li>. Its siblings with class .item are the other two <li> elements.
  2. Step 2: Count matching siblings

    There are exactly 2 siblings with class .item besides the selected one.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Siblings with class '.item' count = 2 [OK]
Hint: Count siblings with matching class [OK]
Common Mistakes:
  • Counting the selected element itself
  • Confusing siblings with children
  • Miscounting the number of siblings
4. You wrote this Cypress code:
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?
medium
A. The click() command is not valid after siblings()
B. There are no sibling elements with class '.container' next to the closest parent
C. The siblings() command only works on child elements, not parents
D. The closest() command cannot be chained after get()

Solution

  1. Step 1: Understand the chaining of commands

    cy.get('.button') finds the button, closest('.container') finds its nearest parent container, then siblings('.container') looks for containers at the same level as that parent.
  2. Step 2: Reason about the DOM structure

    If no sibling containers exist next to the closest parent, siblings('.container') returns empty, causing click() to fail.
  3. Final Answer:

    There are no sibling elements with class '.container' next to the closest parent -> Option B
  4. Quick Check:

    No siblings found = test fails [OK]
Hint: Check if siblings actually exist in DOM [OK]
Common Mistakes:
  • Thinking closest() can't chain after get()
  • Believing siblings() works on parents
  • Assuming click() is invalid after siblings()
5. You want to test that a clicked button highlights its sibling buttons but not buttons in other containers. Which Cypress command chain correctly finds only sibling buttons inside the same container?
hard
A. cy.get('.button.clicked').siblings('.button')
B. cy.get('.button.clicked').closest('.container').siblings('.button')
C. cy.get('.button.clicked').closest('.container').find('.button').siblings()
D. cy.get('.button.clicked').siblings().closest('.container')

Solution

  1. Step 1: Identify the target buttons

    You want siblings of the clicked button that share the same parent container, so siblings('.button') after selecting the clicked button is correct.
  2. 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.
  3. Final Answer:

    cy.get('.button.clicked').siblings('.button') -> Option A
  4. Quick Check:

    Siblings with class '.button' = cy.get('.button.clicked').siblings('.button') [OK]
Hint: Use siblings() directly on clicked button for siblings [OK]
Common Mistakes:
  • Using closest() before siblings() incorrectly
  • Selecting sibling containers instead of buttons
  • Calling siblings() without a selector
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard