How can I click on first button with data-cy equal to other buttons?

389 Views Asked by At

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

1

There are 1 best solutions below

1
Alapan Das On BEST ANSWER

You can use eq(0) for this.

cy.get('[data-cy="' + element + '"]').eq(0).click()