cy.prev() finds the immediate previous sibling element.
cy.next() finds the immediate next sibling element.
Examples
This finds the list item just before the one with class 'active'.
Cypress
cy.get('li.active').prev()
This finds the button right after the button with class 'submit'.
Cypress
cy.get('button.submit').next()
This finds the element immediately after the label for 'email'.
Cypress
cy.get('label[for="email"]').next()
Sample Program
This test visits a sample page with breadcrumb navigation. It finds the active breadcrumb item, then checks the text of the previous and next siblings to confirm they are 'Library' and 'Data'.
Cypress
describe('Test cy.prev() and cy.next()', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/traversal')
})
it('checks previous and next siblings', () => {
// Find the active list item and check its previous sibling text
cy.get('.breadcrumb > .active')
.prev()
.should('have.text', 'Library')
// Find the active list item and check its next sibling text
cy.get('.breadcrumb > .active')
.next()
.should('have.text', 'Data')
})
})
OutputSuccess
Important Notes
If there is no previous or next sibling, the command yields an empty set.
These commands only look at siblings on the same level in the HTML tree.
Summary
cy.prev() gets the element before the current one.
cy.next() gets the element after the current one.
Use them to check or interact with nearby elements easily.
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
Step 1: Understand the purpose of cy.prev()
cy.prev() is used to get the element immediately before the current element in the DOM.
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.
Final Answer:
Selects the element immediately before the current element -> Option C
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
Step 1: Identify the method to get next sibling
To get the next sibling element, Cypress uses cy.next().
Step 2: Match syntax with the element selector
cy.get('#submitBtn').next() correctly selects the button and then its next sibling.
Final Answer:
cy.get('#submitBtn').next() -> Option B
Quick Check:
next() gets next sibling [OK]
Hint: Use .next() after .get() to get next sibling [OK]
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".
Step 2: Understand what invoke('text') does
This command gets the text content of the selected element, so it will return "Item 1".
Final Answer:
"Item 1" -> Option D
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
Step 1: Analyze the error message
The error says Cannot read property 'click' of undefined, meaning next() returned no element.
Step 2: Understand what causes next() to return undefined
If the current element has no next sibling, next() returns nothing, so calling click() fails.
Final Answer:
There is no next sibling element after the element with class 'active' -> Option A
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
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.
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().
Final Answer:
cy.get('.selected').prev().should('have.text', 'Home'); cy.get('.selected').next().should('have.text', 'Contact') -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.