Steelshark910
Steelshark910

Reputation: 13

Cypress - verify if each table row in one column contains the same item and ignoring header

I have a filtered table, a grid table created by DIVs, so no real table elements, that I want to confirm that my filter has worked in but the ways I have tried keep trying to also check the header which is then telling me that my results are wrong

  cy.get('div[data-field="name"]')
    .then($col => {
      const count = $col.length
        .wrap($col)
        .should('have.text', 'Bob'.repeat(count))
    })

I have 3 bobs in my example, but it is also viewing the header so I am getting an error telling me that it is trying to find 3 instances of Bob but also finds Name, so finds 4.

If I try

  cy.get('div[data-field="name"]')
    .each($col => {
      expect($col.text()).to.eq('Bob')
    })

It fails as it tells me the first entry is 'Name' as it is finding the header and I am not sure how to avoid it counting/reading the header.

I'm sure its something simple that I am missing and not thinking of but any help is appreciated!

NOTE: I got both these ways of checking from this other stack answer Cypress - verify if each table row in one column contains the same item

Upvotes: 1

Views: 756

Answers (1)

T.Beard
T.Beard

Reputation: 178

There are probably many ways to solve this, here are a couple.

For the first sample, take one off the count (since there is only one header)

  cy.get('div[data-field="name"]')
    .then($col => {
      const count = $col.length
        .wrap($col)
        .should('have.text', 'Bob'.repeat(count-1))
    })

For the second sample, don't look at the first element

  cy.get('div[data-field="name"]')
    .each(($col, index) => {
      if (index > 0) {
        expect($col.text()).to.eq('Bob')
      }
    })

Upvotes: 3

Related Questions