0
0
Cypresstesting~20 mins

cy.first(), cy.last(), cy.eq() in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cypress Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What element does cy.first() select?
Given the following HTML:
<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

And this Cypress code:
cy.get('li').first().invoke('text')

What text will the command yield?
Cypress
cy.get('li').first().invoke('text')
A"Banana"
B"Apple"
C"Cherry"
D"AppleBananaCherry"
Attempts:
2 left
💡 Hint
cy.first() selects the very first element from the matched set.
Predict Output
intermediate
1:30remaining
What element does cy.last() select?
Given the same HTML:
<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

And this Cypress code:
cy.get('li').last().invoke('text')

What text will the command yield?
Cypress
cy.get('li').last().invoke('text')
A"Cherry"
B"Banana"
C"Apple"
D"AppleBananaCherry"
Attempts:
2 left
💡 Hint
cy.last() picks the last element from the matched set.
Predict Output
advanced
1:30remaining
What element does cy.eq(1) select?
Given the same HTML:
<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

And this Cypress code:
cy.get('li').eq(1).invoke('text')

What text will the command yield?
Cypress
cy.get('li').eq(1).invoke('text')
A"Banana"
B"Apple"
C"Cherry"
D"AppleBananaCherry"
Attempts:
2 left
💡 Hint
cy.eq(index) selects the element at the zero-based index.
assertion
advanced
1:30remaining
Which assertion correctly checks the text of the first item?
You want to check that the first <li> element contains the text 'Apple'. Which Cypress assertion is correct?
Cypress
cy.get('li').first()
Acy.get('li').first().should('contain.text', 'Banana')
Bcy.get('li').first().should('have.value', 'Apple')
Ccy.get('li').first().should('have.text', 'Apple')
Dcy.get('li').first().should('have.html', 'Apple')
Attempts:
2 left
💡 Hint
Use 'have.text' to check exact text content.
🧠 Conceptual
expert
2:00remaining
Why use cy.eq() instead of cy.first() or cy.last()?
Which statement best explains when to use cy.eq(index) over cy.first() or cy.last()?
Acy.eq(index) selects elements by their text content, cy.first() and cy.last() select by position.
Bcy.eq(index) only works on single elements, cy.first() and cy.last() work on multiple elements.
Ccy.eq(index) selects elements randomly, cy.first() and cy.last() select fixed positions.
Dcy.eq(index) selects an element at any zero-based position, while cy.first() and cy.last() only select the first or last element respectively.
Attempts:
2 left
💡 Hint
Think about selecting elements by position.