How to iterate through a table and extract the values from a row and assert using cypress

480 Views Asked by At
let values = []
cy.visit(url)
cy.get('selector')
  .find('td')
  .each(($el, $index) => {
    cy.wrap($el)
      .invoke('text')
      .then(text => {
        if ($index!==0) {
          values.push(text.trim())
        }
      })
  })
 .then(() => expect(values).to.deep.eq.(["Value1", "Value2", "Value3", "Value4"])

I tried to print all values for example in the row the values are aaray[0] value1, aaray[1] value2, aaray[2] value3, aaray[3] value4. While executing above only the values 1, 2, 3 are displayed array[0] value is not showing, how to assert all the 4 values

1

There are 1 best solutions below

1
On

You would get all the values if you removed the if().

cy.get('selector')
  .find('td')
  .each(($el, $index) => {
    cy.wrap($el).invoke('text')
      .then(text => 
        values.push(text.trim())
      })
  })
  .then(() => expect(values).to.deep.eq.(["Value1", "Value2", "Value3", "Value4"])