skip to Main Content

I have test that opens every item on the list. Then it validates if there are no empty (‘–‘) fields. I want to log in console that there were empty fields and go to another item and run the loop till the end. When Cypress throws error tests are stopped.

How can I catch this error and continue running my loop for other items and log errors.

     cy.get('body').then(($body) => { 
        if ($body.find('.MuiTableRow-root.MuiTableRow-hover .MuiTableCell-body').length > 0) {                 globalActions.getTableRow().then((element) => { 
            if (element.is(':visible')) { 
            globalActions.checkProgressBarDoesntExist().then(() => {
            cy.get('table[data-testid="table-items"] tbody tr').each(($row) => { 
            cy.wrap($row).find('.type').invoke('text').then((stageText) => {
             if (stageText.includes('Testing')) { 
            cy.wrap($row).click();
                      ItemsValues.forEach((field) => {
                        cy.wrap(null).then(() => {
                          itemsActions.getValue(field)
                            .should('not.contain', '--')
                            .catch((err) => {
                              cy.task('log', `DOESNT WORK: ${err.message}`);
                            });
                        });
                      });
                    }
                  });
              });
            });
          }
        });
        }
       });

When I try this I get error : Expected .. not to contain —

2

Answers


  1. There is no .catch in Cypress. This is by design since test execution is supposed to be predictable and relies on catching errors, which implies the app may be in an unknown state and is no longer safe to continue.

    However, you know that it is safe to continue. So you can change the test so that we instead collect a list of "bad" fields and call "should()" on this at the end of the loop. Inside the loop, we log each time a bad field is encountered, but we don’t assert on it right there.

    cy.get("body").then(($body) => {
      if (
        $body.find(".MuiTableRow-root.MuiTableRow-hover .MuiTableCell-body")
          .length > 0
      ) {
        globalActions.getTableRow().then((element) => {
          if (element.is(":visible")) {
            globalActions.checkProgressBarDoesntExist().then(() => {
              cy.get('table[data-testid="table-items"] tbody tr').each(($row) => {
                cy.wrap($row)
                  .find(".type")
                  .invoke("text")
                  .then((stageText) => {
                    if (stageText.includes("Testing")) {
                      cy.wrap($row).click();
    
                      const emptyFields = [];
                      cy.wrap(ItemsValues)
                        .each((field) => {
                          const val = itemsActions.getValue(field);
                          if (val.indexOf("--") !== -1) {
                            cy.log(`""${field}" was empty"`);
                            emptyFields.push(field);
                          }
                        })
                        .then(() => {
                          cy.wrap(emptyFields).should("be.empty");
                        });
                    }
                  });
              });
            });
          }
        });
      }
    });
    
    Login or Signup to reply.
  2. To stop the error Expected .. not to contain "–" you can change the should() into a then().

    When you use should() Cypress will stop with a test failure if the condition isn’t met.

    If you substitute then() you can take whatever action you want inside the callback, and as long as it’s not expect() or assert() the test won’t fail, it will carry on.

    itemsActions.getValue(field)
      .then(value => {
        const failed = value.includes('--')                  // same as "contains"
        if (failed) {
          cy.task('log', `DOESNT WORK: ${err.message}`);
        }
      });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search