Test Overview
This test checks that the cy.first(), cy.last(), and cy.eq() commands correctly select the first, last, and a specific element from a list on a webpage. It verifies the text content of these selected elements.
This test checks that the cy.first(), cy.last(), and cy.eq() commands correctly select the first, last, and a specific element from a list on a webpage. It verifies the text content of these selected elements.
describe('Test cy.first(), cy.last(), and cy.eq()', () => { beforeEach(() => { cy.visit('https://example.cypress.io/commands/actions') }) it('selects first, last, and eq elements correctly', () => { // Select all buttons in the .action-buttons container cy.get('.action-buttons > button').as('buttons') // Check the first button text cy.get('@buttons').first().should('have.text', 'Action') // Check the last button text cy.get('@buttons').last().should('have.text', 'Disabled') // Check the third button text (index 2) cy.get('@buttons').eq(2).should('have.text', 'Right') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, Cypress ready | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io/commands/actions' | Page with action buttons loaded | Page URL is correct | PASS |
| 3 | Find all buttons inside '.action-buttons' container and alias as 'buttons' | Multiple button elements found | Number of buttons > 0 | PASS |
| 4 | Select first button using cy.first() and check its text is 'Action' | First button element selected | Button text equals 'Action' | PASS |
| 5 | Select last button using cy.last() and check its text is 'Disabled' | Last button element selected | Button text equals 'Disabled' | PASS |
| 6 | Select third button using cy.eq(2) and check its text is 'Right' | Third button element selected | Button text equals 'Right' | PASS |
| 7 | Test ends | All assertions passed | — | PASS |
cy.first() do in Cypress testing?cy.first()cy.first() is used to pick the very first element from a list of elements found by a selector.cy.first() does.cy.first() = first element [OK]eq()eq() command uses zero-based index, so the third element is at index 2.eq(2) correctly. Options A, C, and D either use wrong index or invalid syntax.<ul> <li>Apple</li> <li>Banana</li> <li>Cherry</li> <li>Date</li> </ul>
cy.get('li').last().invoke('text') return?cy.last() selectscy.last() picks the last element in the matched list, which is the <li> containing "Date".invoke('text')cy.get('button').eq(5).click()eq(5) tries to select the 6th element (index 5). If fewer than 6 buttons exist, it returns undefined.cy.get('.item'). Which Cypress command chain correctly achieves this?eq(n) supports negative indices: eq(-1) is last, eq(-2) is second-to-last.eq(-2) = second-to-last [OK]Pass the test and this pattern moves unknown