Cypress: How to add loop based on the array length?
I have this code where i get 'ul[class="menu"] > li' get there text and convert it to arrays 'list'
it('List to array', () => {
const list = []
cy.get('ul[class="menu"] > li').children()
.each(($ele) => {
list.push($ele.text().toString().trim())
})
.then(() => {
cy.get('ul[class="menu"] > li').should('contain',list[0])
cy.log(list[0])
})
})
Now i want to add a loop until it finished my array in list that will assert (should) based on my last code because the length of list is constantly changing and i don't want a repeated code
How can i achieved this?
Take a look at docs for each(), Cypress passes in each element and also it's index.
Use the index to get the value from the list.
cy.get('ul.menu > li') // same as 'ul[class="menu"] > li'
.each(($el, index) => {
expect($el.text()).to.eq(list[index])
})