0
0
Cypresstesting~20 mins

cy.prev() and cy.next() in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cypress PrevNext Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Cypress test snippet?

Consider the following HTML and Cypress test code. What text will the assertion check?

Cypress
<ul>
  <li>Item 1</li>
  <li class="selected">Item 2</li>
  <li>Item 3</li>
</ul>

// Cypress test snippet
cy.get('li.selected').prev().should('have.text', 'Item 1');
AThe test fails because the previous sibling has text 'Item 3'.
BThe test passes because the previous sibling has text 'Item 1'.
CThe test fails because there is no previous sibling.
DThe test passes because the previous sibling has text 'Item 2'.
Attempts:
2 left
💡 Hint

Remember, cy.prev() selects the immediate previous sibling element.

Predict Output
intermediate
2:00remaining
What will this Cypress command select?

Given this HTML snippet, what element will cy.get('.active').next() select?

Cypress
<div>
  <button>Save</button>
  <button class="active">Cancel</button>
  <button>Delete</button>
</div>
ANo element, because <code>.active</code> is the last button.
BThe button with text 'Save'.
CThe button with text 'Delete'.
DThe button with text 'Cancel'.
Attempts:
2 left
💡 Hint

cy.next() selects the immediate next sibling element.

assertion
advanced
2:00remaining
Which assertion correctly verifies the previous sibling's class?

You want to check that the previous sibling of an element with id #current has the class highlight. Which assertion is correct?

Cypress
cy.get('#current').prev()
Acy.get('#current').prev().should('have.class', 'highlight');
Bcy.get('#current').prev().should('contain.text', 'highlight');
Ccy.get('#current').prev().should('have.text', 'highlight');
Dcy.get('#current').prev().should('have.attr', 'class', 'highlight');
Attempts:
2 left
💡 Hint

Use the assertion that checks for a CSS class on the element.

🔧 Debug
advanced
2:00remaining
Why does this Cypress test fail?

Given this HTML and test code, why does the test fail?

Cypress
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
</ul>

// Test code
cy.get('li').last().next().should('exist');
ABecause <code>cy.next()</code> returns no element when called on the last sibling, so the assertion fails.
BBecause <code>cy.last()</code> is not a valid Cypress command.
CBecause <code>cy.get('li').last()</code> selects the first <li> instead of the last.
DBecause <code>cy.next()</code> selects the previous sibling, not the next.
Attempts:
2 left
💡 Hint

Think about what happens when you call next() on the last sibling element.

🧠 Conceptual
expert
3:00remaining
How does cy.prev() behave with non-element siblings?

In Cypress, if an element has a previous sibling that is a text node (like whitespace or newline), what does cy.prev() select?

A<p><code>cy.prev()</code> selects the previous sibling node regardless of type, including text nodes.</p>
B<p><code>cy.prev()</code> returns the parent element if the previous sibling is a text node.</p>
C<p><code>cy.prev()</code> throws an error if the previous sibling is not an element node.</p>
D<p><code>cy.prev()</code> skips non-element nodes and selects the closest previous sibling element node.</p>
Attempts:
2 left
💡 Hint

Think about how Cypress interacts with the DOM elements versus text nodes.