3

I've a question regarding test structure. The docs clearly state to write tests with multiple assertions instead of multiple tests with single assertions.

But if I want to test if multiple elements are visible the test will fail if the first element was not visible and I therefore have no clue about the other elements. Is there anything I can do to run all assertions?

it("Page should show two actions", () => {
  cy.visit("users/list");
  cy.get("[data-cy=createUser]")
    .should("be.visible")
    .and("have.text", "Create user");
  cy.get("[data-cy=exportUserData]")
    .should("be.visible")
    .and("have.text", "Export data");
});
DarkMikey
  • 383
  • 1
  • 4
  • 24

2 Answers2

0

To run all the assertions you have to make sure the elements are visible. Usually while running cypress it takes time to load everything, between each assertion you can wait, by using this function cy.wait(2000)

  • This doesn't answer my question. I've posted the correct way to use soft assertions. FYI: [Cypress discourages you from using cy.wait in most cases](https://docs.cypress.io/api/commands/wait.html#Time) – DarkMikey Jul 06 '20 at 08:15
  • You don't have to use wait in cypress , but the elements visibility check can be taken into consideration. – Sampath Nov 01 '20 at 05:47
0

Apparently I just didn't find the correct wording for this. Of course it is called soft assertion and not build into Cypress and actually against Chai's assertion philosophy. There are workarounds although I couldn't really find official documentation of Cypress or Chai that could tell me why they approached against soft assertions.

What you can do is:

  • Use the npm package soft-assert as mentioned here
  • Implement it yourself as mentioned here (can break Cypress' retry/timeout mechanism)
DarkMikey
  • 383
  • 1
  • 4
  • 24