问题
We would like to let any test in Cypress fail if console.error is called. Sometimes we have errors in the console log which does not let the test fail. We always have to look into the console window to check any error messages
回答1:
Just add this to your support/index.js
let consoleSpy;
Cypress.on('window:before:load', (win) => {
consoleSpy = cy.spy(win.console, "error")
})
afterEach(() => {
// consoleSpy can be null if test failed already in beforeEach
if (consoleSpy) {
expect(consoleSpy).not.to.be.called
}
})
It will run on each Test and will check if console.error was called and will then let the test fail.
来源:https://stackoverflow.com/questions/61362670/cypress-let-test-fail-if-console-error-occurs