These commands help you pick specific elements from a group on a web page. It makes testing easier by focusing on one item.
cy.first(), cy.last(), cy.eq() in Cypress
cy.get('selector').first() cy.get('selector').last() cy.get('selector').eq(index)
cy.get('selector') finds all elements matching the selector.
first() picks the first element, last() picks the last, and eq(index) picks the element at the given zero-based index.
cy.get('ul li').first()
cy.get('button').last()
cy.get('.item').eq(2)
This test visits a sample page with buttons. It checks the first button's text, confirms the last button is visible, and verifies the third button has a specific class.
describe('Test first, last, and eq commands', () => { beforeEach(() => { cy.visit('https://example.cypress.io/commands/actions') }) it('checks first, last, and eq elements', () => { // Check first button text cy.get('.action-btn').first().should('have.text', 'Button') // Check last button is visible cy.get('.action-btn').last().should('be.visible') // Check third button has correct class cy.get('.action-btn').eq(2).should('have.class', 'btn-primary') }) })
Index in eq() starts at 0, so eq(0) is the first element.
Use these commands after cy.get() to narrow down elements.
These commands help keep tests simple and focused on one element.
first() selects the first element from a group.
last() selects the last element from a group.
eq(index) selects the element at the given position (starting at 0).
Practice
cy.first() do in Cypress testing?Solution
Step 1: Understand the purpose of
cy.first()cy.first()is used to pick the very first element from a list of elements found by a selector.Step 2: Compare with other options
Options A, B, and C describe different behaviors: last element, selecting by class, or selecting all elements, which are not whatcy.first()does.Final Answer:
Selects the first element from a group of matched elements -> Option CQuick Check:
cy.first()= first element [OK]
- Confusing first() with last()
- Thinking first() selects by class or id
- Assuming first() selects all elements
Solution
Step 1: Recall zero-based indexing in
Theeq()eq()command uses zero-based index, so the third element is at index 2.Step 2: Check syntax correctness
cy.get('li').eq(2) useseq(2)correctly. Options A, C, and D either use wrong index or invalid syntax.Final Answer:
cy.get('li').eq(2) -> Option AQuick Check:
Third element index = 2 [OK]
- Using eq(3) for the third element
- Trying to pass a number to first() or last()
- Confusing eq() with first() or last()
<ul> <li>Apple</li> <li>Banana</li> <li>Cherry</li> <li>Date</li> </ul>
What text will
cy.get('li').last().invoke('text') return?Solution
Step 1: Identify what
cy.last()selectscy.last()picks the last element in the matched list, which is the <li> containing "Date".Step 2: Understand
This command extracts the text content of the selected element, so it returns "Date".invoke('text')Final Answer:
"Date" -> Option DQuick Check:
last() element text = "Date" [OK]
- Selecting first element text instead of last
- Confusing zero-based index with last()
- Expecting eq(3) to return "Date" without zero-based indexing
cy.get('button').eq(5).click()It throws an error: "Cannot read property 'click' of undefined". What is the most likely cause?
Solution
Step 1: Understand eq() index and element count
eq(5)tries to select the 6th element (index 5). If fewer than 6 buttons exist, it returns undefined.Step 2: Analyze error message
The error "Cannot read property 'click' of undefined" means the element does not exist, so click() fails.Final Answer:
There are fewer than 6 buttons on the page, so eq(5) returns undefined -> Option AQuick Check:
Index out of range = undefined element [OK]
- Using wrong eq() syntax
- Assuming click() can't chain after eq()
- Ignoring zero-based index causing out-of-range
cy.get('.item'). Which Cypress command chain correctly achieves this?Solution
Step 1: Recall eq() indexing in Cypress
Cypresseq(n)supports negative indices: eq(-1) is last, eq(-2) is second-to-last.Step 2: Compare options
Options A and B have invalid syntax passing chainables to eq(). cy.get('.item').eq(-1).click({ force: true }) uses eq(-1) for last item. cy.get('.item').eq(-2).click() correctly uses eq(-2).Final Answer:
cy.get('.item').eq(-2).click() -> Option BQuick Check:
eq(-2)= second-to-last [OK]
- Using negative index in eq() incorrectly (e.g., -1 instead of -2)
- Trying to subtract length directly without .then()
- Passing Cypress chainable as eq() argument
Ready to prove it?
Pass the test and this pattern moves unknown
