How to assert equal to all array element result?

Viewed 42

i have this code

cy.get('body').then(($body) => {
            if ($body.find('.asis-mailto-obfuscated-email').length > 0){ 
                cy.get('.asis-mailto-obfuscated-email').invoke('text').should('eq',$email)
        }else if ($body.find(':nth-child(1) > .panel-body').length > 0){ 
            cy.log('It goes here')
        }
    })
    })

But '.asis-mailto-obfuscated-email' returning me this
enter image description here
and i want all this 6 element can assert equal to $email. How can i achieved that?

3 Answers

Maybe use .each()

cy.get('.asis-mailto-obfuscated-email')
  .each($el => {
    cy.wrap($el).invoke('text').should('eq', $email)
  })

If you are just checking that in the array of emails, there should be one email present, you can do this. This is assuming that $email has the email text.

cy.get('.asis-mailto-obfuscated-email').should('include.text', $email)

If you want to check that the number of emails is 6 and assuming $email has the number 6, you can do this.

cy.get('.asis-mailto-obfuscated-email').its('length').should('eq', $email)

The each will work on $body.find('.asis-mailto-obfuscated-email') as well.

$body.find('.asis-mailto-obfuscated-email')
  .each(function(index) {
    expect(Cypress.$(this).text()).to.eq($email)
  })
Related