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

2 thoughts on “Cypress: How to count number of buttons”

  1. 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)
    })
    
    Reply
  2. 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)
    })
    
    Reply

Leave a Comment