lundi 12 octobre 2020

How can I perform conditional testing in Cypress inside each loop

I have a dynamic table, where some rows can be active or not, so I have to click only available ones. Even though I'm clicking only available rows I still can receive an error, so I must also assert, that no error message is displayed. In short, I must loop through table rows and find one, that doesn't trigger any errors and continue to the next test step. I tried as it was suggested in Cypress documentation, but I can't break from each loop returning 'false' inside then() block

        cy.get('#table')
            .find('tr').each(($tr) => {
                if ($tr.attr('class') != 'not-avail') {
                    cy.get($tr).click()
                    cy.get('.blocker > .q-spinner').should('not.be.visible')

                    return cy.get('body').then(($body) => {
                        if (!$body.find('#error').length) {
                            return false
                        }
                    })
                }
            })

I also tried this variant, but it fails the test, when no error message is displayed

        cy.get('#table')
            .find('tr').each(($tr) => {
                if ($tr.attr('class') != 'not-avail') {
                    cy.get($tr).click()
                    cy.get('.blocker > .q-spinner').should('not.be.visible')

                    const $body = cy.get('body')
                    if (!$body.find('#error').length) {
                        return false
                    }
                }
            })

Aucun commentaire:

Enregistrer un commentaire