我正在开发一个由 Three.js 驱动的应用程序,所以我无法正确测试应用程序的 3d 方面,但我想在浏览器控制台中监听 javascript 错误。
有可能吗?
慕神8447489
浏览 197回答 1
1回答
紫衣仙女
您可以使用 Cypress 拦截控制台消息cy.spy(),但如果您想进一步了解数据 - 我还没有看到任何方法可以做到这一点。该文档可以使用一点再治具,所以这里就是我如何设置的间谍。let spy;Cypress.on('window:before:load', (win) => { spy = cy.spy(win.console, "error") // can be other methods - log, warn, etc})it('Doing something that should not cause a console error', () => { // Run test steps here that may cause a console error cy.wait(100).then(x => { expect(spy).not.to.be.called }) // or perhaps this, to auto-retry (have not tried this syntax) cy.wrap({}).should(() => { expect(spy).not.to.be.called }) // The docs imply you can just do this expect(spy).not.to.be.called // ..but that line may run before any other cy command above finish // so I'd stick with using cy.wrap({}).then(...) to put it in the command chain // The spy call count is reset after each test})