Bird
0
0

How can you verify that the first and last elements in a list have specific texts "Start" and "End" respectively using Cypress commands?

hard📝 Application Q9 of 15
Cypress - Selecting Elements
How can you verify that the first and last elements in a list have specific texts "Start" and "End" respectively using Cypress commands?
Acy.get('li').eq('Start').should('exist'); cy.get('li').eq('End').should('exist')
Bcy.get('li').eq(0).should('have.text', 'End'); cy.get('li').eq(-1).should('have.text', 'Start')
Ccy.get('li').first('Start'); cy.get('li').last('End')
Dcy.get('li').first().should('have.text', 'Start'); cy.get('li').last().should('have.text', 'End')
Step-by-Step Solution
Solution:
  1. Step 1: Use first() and last() to select elements

    The commands first() and last() select the first and last elements respectively.
  2. Step 2: Use should('have.text', text) to assert text content

    Using should('have.text', 'Start') and should('have.text', 'End') verifies the text content of those elements.
  3. Final Answer:

    cy.get('li').first().should('have.text', 'Start'); cy.get('li').last().should('have.text', 'End') -> Option D
  4. Quick Check:

    first() and last() with should('have.text') verify texts [OK]
Quick Trick: Chain first()/last() with should('have.text', ...) to check texts [OK]
Common Mistakes:
  • Using eq() with string arguments
  • Passing arguments to first() or last()
  • Confusing text assertions with existence checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes