Leslie Bain
Leslie Bain

Reputation: 139

Converting should expression to soft assertions

I have a complex form with many individual features to test, and I'd like to traverse the whole page without failing the test. I've seen soft assertions but cannot figure out how to make it work with attributes such as disabled.

For example

<input data-id="name"></input>
<input data-id="email"></input>
<input data-id="password" disabled></input>

cy.get('[data-id="name"]').should('not.be.disabled') 
cy.get('[data-id="email"]').should('be.disabled')            // fails - convert to soft assertion?
cy.get('[data-id="password"]').should('have.attr', 'disabled') 

Upvotes: 3

Views: 152

Answers (1)

Fody
Fody

Reputation: 31974

You could use alfonso-presa/soft-assert to change the way expect works.

It would mean you have to alter the style of .should() to the callback version.

Also, since the expect() not longer fails when the expression is false, you lose retry capability. Only use this on a stable HTML page.

const { proxy, flush } = require("@alfonso-presa/soft-assert");
expect = proxy(expect);

it('tests by soft assert', () => {

  cy.get('[data-id="name"]').should($el => expect($el).not.to.be.disabled) 

  // shows failed in log but test continues
  cy.get('[data-id="email"]').should($el => expect($el).to.be.disabled)  

  cy.get('[data-id="password"]').should($el => expect($el).to.have.attr('disabled')) 

  cy.then(flush)  // flush any errors
})

Note The flush() method should be added to Cypress command chain, otherwise the test ends before it is evaluated.

Upvotes: 3

Related Questions