Home > database >  How can i click on first button with data-cy equal to other buttons?
How can i click on first button with data-cy equal to other buttons?

Time:11-02

im new to test e2e and im trying to click on a specific button, i gave it a data-cy that automatically is been attributed to other buttons created dinamycally.

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

CodePudding user response:

You can use eq(0) for this.

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