Bird
Raised Fist0
Cypresstesting~15 mins

cy.prev() and cy.next() in Cypress - Build an Automation Script

Choose your learning style10 modes available
Verify navigation to previous and next sibling elements using cy.prev() and cy.next()
Preconditions (2)
Step 1: Open the web page with the list
Step 2: Locate the list item with text 'Item 2'
Step 3: Use cy.prev() to get the previous sibling element
Step 4: Verify the previous sibling's text is 'Item 1'
Step 5: Use cy.next() to get the next sibling element
Step 6: Verify the next sibling's text is 'Item 3'
✅ Expected Result: The test should confirm that the previous sibling of 'Item 2' is 'Item 1' and the next sibling is 'Item 3', passing all assertions.
Automation Requirements - Cypress
Assertions Needed:
Assert that the previous sibling's text equals 'Item 1'
Assert that the next sibling's text equals 'Item 3'
Best Practices:
Use cy.contains() to locate elements by text
Chain cy.prev() and cy.next() correctly
Use should() assertions for verifying text content
Avoid hardcoded selectors; prefer text-based selectors for clarity
Automated Solution
Cypress
describe('Test cy.prev() and cy.next() commands', () => {
  beforeEach(() => {
    cy.visit('/list-page') // Replace with actual URL or path
  })

  it('should verify previous and next siblings of Item 2', () => {
    cy.contains('li', 'Item 2')
      .prev()
      .should('have.text', 'Item 1')

    cy.contains('li', 'Item 2')
      .next()
      .should('have.text', 'Item 3')
  })
})

This test suite visits the page containing the list before each test.

The test locates the list item with text 'Item 2' using cy.contains('li', 'Item 2').

Then it uses .prev() to get the previous sibling and asserts its text is 'Item 1' with .should('have.text', 'Item 1').

Similarly, it uses .next() to get the next sibling and asserts its text is 'Item 3'.

This approach uses clear selectors and Cypress chaining for readability and reliability.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using cy.get() with a generic selector instead of cy.contains() to find the specific list item', 'why_bad': 'Generic selectors may select multiple elements, causing the test to fail or behave unpredictably.', 'correct_approach': "Use cy.contains('li', 'Item 2') to precisely locate the list item by its text."}
Not chaining .prev() or .next() directly after locating the element
{'mistake': "Using .text() instead of .should('have.text', ...)", 'why_bad': ".text() is not a Cypress command and will cause errors; assertions must use Cypress's should() syntax.", 'correct_approach': "Use .should('have.text', 'expected text') to assert element text content."}
Bonus Challenge

Now add data-driven testing to verify previous and next siblings for 'Item 3' and 'Item 4' as well.

Show Hint

Practice

(1/5)
1. What does cy.prev() do in Cypress testing?
easy
A. Selects the element immediately after the current element
B. Selects all sibling elements of the current element
C. Selects the element immediately before the current element
D. Selects the parent element of the current element

Solution

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

    cy.prev() is used to get the element immediately before the current element in the DOM.
  2. Step 2: Compare with other options

    cy.next() gets the element after, siblings means all at the same level, and parent is the container element. So only cy.prev() matches Selects the element immediately before the current element.
  3. Final Answer:

    Selects the element immediately before the current element -> Option C
  4. Quick Check:

    cy.prev() = previous element [OK]
Hint: Prev means previous element before current [OK]
Common Mistakes:
  • Confusing cy.prev() with cy.next()
  • Thinking it selects parent or all siblings
  • Assuming it selects elements after current
2. Which of the following is the correct syntax to get the next sibling element of a button with id submitBtn using Cypress?
easy
A. cy.get('#submitBtn').prev()
B. cy.get('#submitBtn').next()
C. cy.get('#submitBtn').parent()
D. cy.get('#submitBtn').children()

Solution

  1. Step 1: Identify the method to get next sibling

    To get the next sibling element, Cypress uses cy.next().
  2. Step 2: Match syntax with the element selector

    cy.get('#submitBtn').next() correctly selects the button and then its next sibling.
  3. Final Answer:

    cy.get('#submitBtn').next() -> Option B
  4. Quick Check:

    next() gets next sibling [OK]
Hint: Use .next() after .get() to get next sibling [OK]
Common Mistakes:
  • Using .prev() instead of .next()
  • Using .parent() which gets the container
  • Using .children() which gets child elements
3. Given the HTML structure:
<ul>
  <li>Item 1</li>
  <li id="selected">Item 2</li>
  <li>Item 3</li>
</ul>

What text will the following Cypress command get?
cy.get('#selected').prev().invoke('text')
medium
A. Error: No previous element
B. "Item 3"
C. "Item 2"
D. "Item 1"

Solution

  1. Step 1: Identify the current element and its previous sibling

    The element with id "selected" is the second <li> with text "Item 2". Its previous sibling is the first <li> with text "Item 1".
  2. Step 2: Understand what invoke('text') does

    This command gets the text content of the selected element, so it will return "Item 1".
  3. Final Answer:

    "Item 1" -> Option D
  4. Quick Check:

    prev() gets previous sibling text [OK]
Hint: prev() gets previous sibling's text [OK]
Common Mistakes:
  • Confusing prev() with next()
  • Expecting text of current element
  • Assuming error if previous exists
4. You wrote this Cypress code:
cy.get('.active').next().click()

But the test fails with an error saying Cannot read property 'click' of undefined. What is the most likely cause?
medium
A. There is no next sibling element after the element with class 'active'
B. The selector '.active' is incorrect
C. You should use prev() instead of next()
D. The click() method is not valid in Cypress

Solution

  1. Step 1: Analyze the error message

    The error says Cannot read property 'click' of undefined, meaning next() returned no element.
  2. Step 2: Understand what causes next() to return undefined

    If the current element has no next sibling, next() returns nothing, so calling click() fails.
  3. Final Answer:

    There is no next sibling element after the element with class 'active' -> Option A
  4. Quick Check:

    next() returns undefined if no next sibling [OK]
Hint: Check if next sibling exists before clicking [OK]
Common Mistakes:
  • Assuming selector is wrong without checking DOM
  • Using prev() instead of next() without reason
  • Thinking click() is invalid in Cypress
5. You want to verify that a list item with class selected has a previous sibling with text "Home" and a next sibling with text "Contact". Which Cypress code correctly checks this?
hard
A. cy.get('.selected').prev().should('have.text', 'Home'); cy.get('.selected').next().should('have.text', 'Contact')
B. cy.get('.selected').prev().should('have.text', 'Home').next().should('have.text', 'Contact')
C. cy.get('.selected').prev().should('have.text', 'Contact').next().next().should('have.text', 'Home')
D. cy.get('.selected').prev().next().should('have.text', 'Contact')

Solution

  1. Step 1: Understand chaining of prev() and next()

    Using prev() then next() on the same chain moves from previous element back to the selected element, not to the next sibling.
  2. Step 2: Separate commands for previous and next siblings

    To check previous and next siblings independently, use separate cy.get('.selected') calls with prev() and next().
  3. Final Answer:

    cy.get('.selected').prev().should('have.text', 'Home'); cy.get('.selected').next().should('have.text', 'Contact') -> Option A
  4. Quick Check:

    Use separate calls for prev() and next() [OK]
Hint: Use separate cy.get() for prev() and next() checks [OK]
Common Mistakes:
  • Chaining prev() and next() incorrectly
  • Using next().next() instead of next()
  • Assuming prev().next() gets next sibling
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