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.