How to use Nested Loop (.each) for iterating HTML table in Cypress

I need urgent help. I am stuck in problem . I have to iterate a table . say we have table in which there are columns reference number, Purpose and button. I want to check if my reference number == desired text and purpose == desired text then click on button.
Right now problem is it only check first row, not iterating other rows.

Below is the code i am trying:

clickButton(purpose) {
    cy.xpath(this.objectFactory.referenceNoColumnXpath).each(
      ($e, index, $list) => {
        const refNumberText = $e.text()
        if (refNumberText.includes('automationRefNumberEdit')) {
          cy.log(refNumberText)

          cy.xpath(this.objectFactory.purposeColumnXpath).each(
            ($ee, indexx, $listt) => {
              const purposeText = $ee.text()
              cy.log(purposeText)
              if (purposeText.includes(purpose)) {
                const signatureElement = cy
                  .xpath(this.objectFactory.signatureButtonXpath)
                  .eq(indexx)
                  .find('img')
                  .should('have.attr', 'src')
                if (signatureElement.should('contain', 'signature_gray')) {
                  cy.log(indexx)
                  cy.xpath(this.objectFactory.signatureButtonXpath)
                    .eq(indexx)
                    .first()
                    .click()
                  cy.xpath(this.objectFactory.applyNowButtonXpath)
                    .should('be.visible')
                    .click()
                  cy.VerifyAlert(
                    'Listing(s) has been updated successfully',
                    'rgb(65, 140, 89)'
                  )
                }
              }
              if (!purposeText.includes(purpose)) {
                assert.fail('Purpose not found in table')
              }
              return false
            }
          )
        }
        if (!refNumberText.includes('automationRefNumberEdit')) {
          assert.fail('Reference number not found in table')
        }
        return false
      }
    )
  }

Source: Ask Javascript Questions

LEAVE A COMMENT