咱们写程序、写复杂的脚本时,若是遇到问题,常常须要打断点进行调式,而Cypress提供了很好的debug命令——debuggerhtml
Cypress测试代码在与应用程序相同的运行循环中运行。这意味着您能够访问在页面上运行的代码,以及浏览器提供给您的内容,如document, window, and debugger。python
基于这些语句,您可能会试图在测试中添加调试器,以下所示:api
/* __author__ = 'Leo' */ it('let me debug like a fiend', function() { cy.visit('https://www.baidu.com/') cy.get('#s-top-left') debugger // Doesn't work })
可是上面的代码并不会运行。Cypress 的文档里面介绍,cy命令是以队列的形式添加到列表里,最后才执行的。
debugger 将在 cy.visit() and cy.get() 以前执行,以下图。浏览器
Let’s use .then()
to tap into the Cypress command during execution and add a debugger
at the appropriate time:app
让咱们使用then()在执行过程当中点击Cypress命令,并在适当的时候添加调试器:函数
it('let me debug when the after the command executes', () => { cy.visit('https://www.baidu.com/') cy.get('#s-top-left') .then(($selectedElement) => { // Debugger is hit after the cy.visit // and cy.get command have completed debugger }) })
这样就能够先运行代码,在 debugger 位置暂停:学习
上面的代码整个工做流程以下测试
使用cy.debug()
Cypress还公开了用于调试命令的快捷方式.debug()。让咱们用这个帮助器方法重写上面的测试:spa
it('let me debug like a fiend', function() { cy.visit('https://www.baidu.com/') cy.get('#s-top-left') .debug() })
此时 cy.get()
会生成一个 subject 变量,在 Developer Tools 里面能够直接使用 console 调试debug
代码格式化一下:
it('let me debug like a fiend', function() { cy.visit('https://www.baidu.com/') cy.get('#s-top-left').debug() })
在测试期间,使用.debug()快速检查应用程序的任何(或许多)部分。您能够将它附加到任何Cypress命令链上,以查看此时系统的状态。
在调试代码时,除了用debug(),咱们还有一个命令就是.pause()命令:
it('let me debug like a fiend', function() { cy.visit('https://www.baidu.com/') cy.pause() cy.get('#s-top-left') })
运行后:
左上角有两个按钮,从左往右分别是
若是对python测试开发相关技术感兴趣的伙伴,欢迎加入测试开发学习交流QQ群:696400122,不积跬步,无以致千里。