Cypress: How to count number of buttons

Viewed 536

Hi I want to count the number of buttons inside a specific tag How can I print the sum of buttons?

I tried

cy.log(cy.get('dropdown').find('button').count())

But it doesn't work

2 Answers

You can get the length by using .its:

cy.get('dropdown').find('button').its('length').should('eq', 4)

Or, You can also get the length like:

cy.get('dropdown').find('button').its('length').then((len) => {
    cy.log('No. of buttons are: ' + len)
})

You can save the count with an alias

cy.get('dropdown').find('button').its('length').as('buttonCount')

... // more actions e.g add a button

cy.get('@buttonCount').then(previousCount => {
  cy.get('dropdown').find('button').its('length')
    .should('be.gt', previousCount)
})
Related