Cypress - let test fail if console.error occurs

百般思念 提交于 2021-01-29 09:52:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!