1
votes

I need to reverse taking element from array. I have to start from the last one. I can't find any different solution to use in Cypress than jQuery 'reverse' but I have problem to use it.

Don't work in both way:

 cy.get('.btn-remove.action.delete').reverse().each(($el, index, $list) =>  {
                        cy.get($el).click({force:true})
                       cy.wait(1000)
                })

answer from Cypress - TypeError: cy.get(...).reverse is not a function

And

cy.get('.btn-remove.action.delete').invoke('reverse').each(($el, index, $list) =>  {
                        cy.get($el).click({force:true})
                        cy.wait(1000)
                })

answer from Cypress - CypressError: Timed out retrying: cy.invoke() errored because the property: 'reverse' does not exist on your subject.

Or maybe there is different way than reverse()?

1

1 Answers

1
votes

What about trying with for loop and iterating from the end, using eq():

cy.get('.btn-remove.action.delete').its('length').then((numberOfItems) => {
  for (var i = numberOfItems-1; i>=0; i--) {
    cy.get('.btn-remove.action.delete').eq(i).click({force:true})
  }
})

Maybe it is not the cleanest way, but should work.

Not sure what was the cy.wait(1000) for in your code, but it shouldn't be necessary here. (Actually, you should avoid using cy.wait() as much as possible)

Let me know if you have any questions.