1

Why does cypress only loop through once inside the for loop?

Test code is this:

cy.get('body').contains('Automation').each(($el, index) => {
  cy.get('body').contains('Automation').parents()
    .eq(1)
    .find('mfc-dropdown > div > mfc-button > button', { timeout: 6000 })
    .first()
    .click({ force: true });
  cy.get(this.DELETE_FILE_BUTTON).click();
  cy.get('.mfc-dialog-container')
    .find(this.CONFIRM_DELETE)
    .click({ force: true });
});
Niles P
  • 29
  • 5
khiz
  • 37
  • 3

3 Answers3

5

@JessefSpecialisterren has given the right reason why no looping occurs.

What can you do about it?

You can move the contains inside the get() using the :contains() pseudo-selector

Description: Select all elements that contain the specified text.

cy.get(':contains(Automation)').each(($el, index) => {

You really want to target the element directly, not the <body> element

For example

cy.get('button:contains(Automation)').each(($el, index) => {

What about lines 2-3?

That looks dubious, I think you want

cy.get('button:contains(Automation)').each(($el, index) => {
  cy.wrap($el).parents().eq(1)
  ...
Michael Hines
  • 928
  • 1
  • 7
3

Because you're looping over the result of the contains command, and contains only returns the first matching element, not all matching elements. See https://docs.cypress.io/api/commands/contains#Single-Element

0

I wasn't looping through all items, but rather only looking at the first one by saying

cy.get().contains()

I needed to loop through the whole list by doing

cy.get('button:contains('Automation'))
ZygD
  • 22,092
  • 39
  • 79
  • 102
khiz
  • 37
  • 3