Get The Last Item In An Array

Channel Avatar
Comment
X
Share
Get The Last Item In An Array
Get The Last Item In An Array
If the current subject is an array, you can use the new ES6 Array methods or Lodash helper functions to get the last item and yield it to the next command or assertion

const list [1, 2, 3]
cy.wrap(list).invoke(‘at’, -1).should(‘equal’, 3)
cy.wrap(list).then(Cypress._.last).should(‘equal’, 3)

Find this recipe and many more Cypress examples at https://glebbahmutov.com/cypress-examples

Take the opportunity to connect and share this video with your friends and family if you find it useful.

Read Also

Leave a Reply

Your email address will not be published. Required fields are marked *