0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.