Home > front end >  cypress .each() .wrap() .invoke('text') takes long time
cypress .each() .wrap() .invoke('text') takes long time

Time:06-01

I am trying to check the items shown in my table for some assertions. My way is to put all of the items in an array and then iterate over that array.

My problem: All assertions already passed but the cypress runner still takes a lot of time to finish the cy.wrap(.invoke(text)) jobs.

Since this is a very core command of my cypress tests it would be great to have a more efficient function.

My command:

cy.get('table tbody').within(() => {
        cy.get('tr').each((tr) => {
            cy.wrap(tr.children().eq(index)).invoke('text').then((text) => {
                text = text.trim();
                arrayWithValuesOfTheList.push(text);
            });
        })
            .then(() => {
                //in here all the (quickly passing) assertions are...
            });
    });

Thanks for any help in advance. I appreciate you all!

CodePudding user response:

I would use a .map() to get the array of texts.

cy.get('table tbody tr td:eq(index)') // every row, nth cell
  .then($tds => {
    const arrayWithValuesOfTheList = [...$tds].map(el => el.innerText.trim())
    return arrayWithValuesOfTheList 
  })
  .then((arrayWithValuesOfTheList) => {
    //in here all the (quickly passing) assertions are...
  })
})

Ref Array.map() - this function takes an array of element and converts it to the corresponding texts.

CodePudding user response:

You can avoid wrapping the value, will give some increase in speed but it's hard to say what is the slowest part.

const arrayWithValuesOfTheList  = []
cy.get('table tbody tr')
  .each($tr => {
    arrayWithValuesOfTheList.push($tr.children().eq(index).text())
  })
  .then(() => {
    //in here all the (quickly passing) assertions are...
  })
})

CodePudding user response:

You can do something like this. It gets the tr values one by one and matches in against a regex pattern.

cy.get('table tbody tr').each(($ele) => {
  cy.wrap($ele.text().trim())
    .should('match', /myregexp/)
    .and('not.include', 'some text')
})
  • Related