Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click x number of times in Cypress

I have a list of objects on my site that all have 'Add' buttons next to them. When the first 'Add' button is clicked, that object is added and the row disappears and is replaced by the next one down. The object name is the same. I want to .click() three times to add the first three objects in the list, before saving. How can I do this?

I'm aware of .click() to click a single object. I'm also aware of .click ({ multiple: true}) to click all the objects on the page. However, I want it to stop clicking after the third time.

Currently set to click multiple times to add all the objects in the list (which is incorrect):

    cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
       .should('exist')
       .click({ multiple: true });
like image 294
Secret Squirrel Avatar asked Jul 03 '19 08:07

Secret Squirrel


People also ask

How do you right click on Cypress?

Right click all buttons found on the page By default, Cypress will error if you're trying to right click multiple elements. By passing { multiple: true } Cypress will iteratively apply the right click to each element and will also log to the Command Log multiple times.

How do you get attribute values in Cypress?

To find elements by data attribute, query using the attribute selector. cy. get() yields a jQuery object, you can get its attribute by invoking the . attr() method.

How do you click hidden elements in Cypress?

For example, to click a hidden element we can use the Cypress command click and pass the option {force : true} as a parameter to it - click({ force: true }). This modifies the hidden characteristics of the hidden element and we can click it.

What does force true do in Cypress?

In summary, { force: true } skips the checks, and it will always fire the event at the desired element.


1 Answers

To hammer click a button you can use this:

for(let n = 0; n < 10; n ++){
  cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
    .click()
}

The multiple: true is used to click several elements, for example to click all buttons on the page.

like image 67
Mr. J. Avatar answered Oct 03 '22 22:10

Mr. J.