Cypress commands are asynchronous. To sort the array after the each
command, you have to use a then
callback:
var likes = []
cy.get('.blogs').each(($el, index) => {
const bcy = cy.wrap($el)
bcy.get('#viewBlog').click()
const like = Math.floor(Math.random() * 10)
likes[index] = like
for(let i=like;i--; )
bcy.get('.blogsDetail').eq(index).contains('likes').click()
}).then(() => {
likes.sort((a, b) => b - a)
})
Wheny you call the sort
method out of a then
, it’s executed before any cypress command and you have an empty array at that moment. See it in debugger:
CLICK HERE to find out more related problems solutions.