How cy.click on all buttons starting from 2nd element in Cypress?

Viewed 2407

How can I click on all the the buttons but starting from the 2nd element? Here is my code:

it('Click menu buttons one by one', function () {

    cy.visit('http://localhost:25000/', { timeout: 300000 })
    cy.wait(10000)
    cy.get('.dijitReset.dijitInline.dijitMenuItemLabel.dijitMenuItem', { timeout: 200000}) .click({multiple: true }) 
    cy.waitSomeTime(10000)

})
2 Answers

Use each():

cy
  .get('.dijitReset.dijitInline.dijitMenuItemLabel.dijitMenuItem')
  .each(($btn, index) => {
    if (index >= 1) cy.wrap($btn).click();
});

More examples could be found in the doc here.

Something new I saw today in a Cypress internal test

cy.get('.dijitReset.dijitInline.dijitMenuItemLabel.dijitMenuItem')
  .invoke('slice', 1)  // take from 2nd onwards
  .click({multiple: true})

Ref: jQuery .slice()

Reduce the set of matched elements to a subset specified by a range of indices

Related