0
0
Cypresstesting~5 mins

cy.prev() and cy.next() in Cypress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does cy.prev() do in Cypress?

cy.prev() gets the immediately preceding sibling element of the current element in the DOM.

Click to reveal answer
beginner
What is the purpose of cy.next() in Cypress?

cy.next() gets the immediately following sibling element of the current element in the DOM.

Click to reveal answer
intermediate
How would you use cy.prev() to check the text of the previous sibling element?

Use cy.get(selector).prev().should('have.text', 'expected text') to assert the text of the previous sibling.

Click to reveal answer
intermediate
Can cy.next() be chained after cy.get()? Give an example.

Yes. Example: cy.get('.item').next().should('have.class', 'active') checks if the next sibling has class 'active'.

Click to reveal answer
intermediate
What happens if there is no previous or next sibling when using cy.prev() or cy.next()?

The command yields an empty set, so assertions on it will fail because no element is found.

Click to reveal answer
What does cy.prev() select?
AThe first child element
BThe next sibling element
CThe parent element
DThe previous sibling element
Which command gets the next sibling element in Cypress?
Acy.prev()
Bcy.next()
Ccy.parent()
Dcy.children()
What will happen if cy.next() is called on the last sibling element?
AIt will yield an empty set
BIt will throw an error immediately
CIt will select the first sibling
DIt will select the parent element
How can you check the class of the previous sibling element?
Acy.get(selector).children().should('have.class', 'className')
Bcy.get(selector).next().should('have.class', 'className')
Ccy.get(selector).prev().should('have.class', 'className')
Dcy.get(selector).parent().should('have.class', 'className')
Which of these is a correct use of cy.next()?
Acy.get('.btn').next().click()
Bcy.get('.btn').prev().click()
Ccy.get('.btn').parent().next()
Dcy.get('.btn').children().next()
Explain how cy.prev() and cy.next() help in navigating sibling elements in Cypress tests.
Think about how you move left or right between items on a shelf.
You got /4 concepts.
    Describe what happens when cy.prev() or cy.next() is used on an element with no respective sibling.
    Imagine looking for a neighbor that does not exist.
    You got /4 concepts.