0

In cypress I'm able to pop up a assert message when get() is good and contain() or ep() fails with custom overwrite should command. But not able to figure if get() fails to locate element.

Can we overwrite the get() function.

Cypress.Commands.overwrite('should', (originalFn, assertion, expected, options) => {
  if (options && options.message) {
    cy.removeAllListeners('fail') // remove all 'fail' listeners
    // And we're starting from fresh!
    cy.on('fail', (error, runnable) => {
      error.name = 'CustomError'
      error.message = options.message
      throw error // throw error to have test still fail
    })
  }

  return originalFn( assertion, expected, options)
})

it('fails with custom message via overwrite of should', () => {
cy.wrap(1).should('eq', 1, { message: 'custom error message 1'})
cy.wrap(1).should('eq', 2, { message: 'custom error message 2'})

})

Fody
  • 23,754
  • 3
  • 20
  • 37
Pain
  • 1

1 Answers1

0

The code you presented is incorrect, there are five parameters to the overwrite

Cypress.Commands.overwrite('should', (originalFn, actual, assertion, expected, options)
  // actual = 1
  // assertion = eq
  // expected = 2
  // options = { message: ... }
  ... // now the rest of the code works - sort of

Note that cy.on('fail',... continues to operate after the current command and current test, unless explicitly overwritten.

Fody
  • 23,754
  • 3
  • 20
  • 37