为了账号安全,请及时绑定邮箱和手机立即绑定

Cypress:监控控制台输出

Cypress:监控控制台输出

慕神8447489 2021-12-02 14:57:34
我知道 Cypress 可以在浏览器控制台中打印调试信息,但是它可以在测试期间从控制台读取数据吗?我正在开发一个由 Three.js 驱动的应用程序,所以我无法正确测试应用程序的 3d 方面,但我想在浏览器控制台中监听 javascript 错误。有可能吗?
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

您可以使用 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


})


查看完整回答
反对 回复 2021-12-02
  • 1 回答
  • 0 关注
  • 186 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信