Raziel Zohar
Raziel Zohar

Reputation: 149

Cypress: How to count number of buttons

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

Upvotes: 1

Views: 1050

Answers (2)

user14903560
user14903560

Reputation:

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)
})

Upvotes: 1

Alapan Das
Alapan Das

Reputation: 18624

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)
})

Upvotes: 2

Related Questions