0
0
Cypresstesting~10 mins

cy.first(), cy.last(), cy.eq() in Cypress - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Cypress
Cypress
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')
  })
})
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, Cypress ready-PASS
2Browser opens and navigates to 'https://example.cypress.io/commands/actions'Page with action buttons loadedPage URL is correctPASS
3Find all buttons inside '.action-buttons' container and alias as 'buttons'Multiple button elements foundNumber of buttons > 0PASS
4Select first button using cy.first() and check its text is 'Action'First button element selectedButton text equals 'Action'PASS
5Select last button using cy.last() and check its text is 'Disabled'Last button element selectedButton text equals 'Disabled'PASS
6Select third button using cy.eq(2) and check its text is 'Right'Third button element selectedButton text equals 'Right'PASS
7Test endsAll assertions passed-PASS
Failure Scenario
Failing Condition: If the selector '.action-buttons > button' does not find the expected buttons or the text assertions fail
Execution Trace Quiz - 3 Questions
Test your understanding
Which command selects the very first button element in the list?
Acy.first()
Bcy.last()
Ccy.eq(0)
Dcy.get()
Key Result
Use cy.first(), cy.last(), and cy.eq() to select specific elements from a group. Always verify the selected element's content to ensure correct selection.