Laxmi
Laxmi

Reputation: 13

Cypress: How to validate 2 rows in a table has same value/ID

Hi I have a table below where 2 rows have same value i need to iterate and check 2 rows have ID: POL-330 etc which is passed as parameter. using cypress typescript please

enter image description here

i have tried

```getNumberOfDocuments(document: string) {
    cy.contains('tr > td:nth-child(1)', document).should('have.length.greaterThan', 1);
  }```

and also

```getNumberOfDocuments(document: string) {
    cy.contains('tr > td:nth-child(1)', document).each(($ele, index, list) => {
        cy.wrap($ele).should('have.length.greaterThan', 1)
    })
  }```

please help

Upvotes: 1

Views: 931

Answers (1)

Fody
Fody

Reputation: 32052

The .contains() command only ever returns the first matching element, so you can't use it to check for two rows.

But you can use :contains() inside the selector

const selector = `tr > td:nth-child(1):contains(${document})`  // use string template to build selector

cy.get(selector).should('have.length.greaterThan', 1)

If you want to loop, this is how it might work

let count = 0;
cy.get('tr > td:nth-child(1)')
  .each($el => {
    if ($el.text() === document) {
      count = count + 1;
    }
  })
  .then(() => {                
    expect(count).to.be.gt(1)   
  })

Upvotes: 2

Related Questions