0

I'm new to test e2e and I'm trying to click on a specific button, I gave it a data-cy that automatically is been attributed to other buttons created dynamically.

How can I click on first button? The code i use for click on a single data-cy is this:

When('I click on button data-cy:{string}', (element) => {
  cy.get('[data-cy="' + element + '"]').click();
});

Thanks for attention

EDIT

Already tried to add .first() on line, but i would like to use a method that allowed me to use the selector also for only the second/third... button. Btw for the specific test the .first() syntax works perfectly

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

You can use eq(0) for this.

cy.get('[data-cy="' + element + '"]').eq(0).click()
Alapan Das
  • 17,144
  • 3
  • 29
  • 52