Cypress 简明教程

Cypress - Debugging

Cypress 具有非常好的调试功能,我们可以通过它进行时间旅行,并查看测试执行期间实际发生了什么。这可以通过将鼠标悬停在测试运行器日志上完成。

Cypress has a very good debugging feature, where we can time travel and see what has actually happened during the test execution. This can be done by hovering the mouse over the Test Runner logs.

当我们在测试运行器窗口中浏览各个步骤时,元素会高亮显示。我们还可以使用 Cypress 命令 pause。这会暂停执行,在此期间我们可以调试前一个步骤。之后,我们可以再次恢复执行。

As we move through the steps in the Test Runner window, the elements get highlighted.We can also use the Cypress command pause. This pauses the execution, during which we can debug the previous steps. After that, we can again resume execution.

Implementation

用于在 Cypress 中调试的命令的实现如下 −

The implementation of commands for debugging in Cypress is as follows −

describe('Tutorialspoint Test', function () {
   // test case
   it('Scenario 1', function (){
      // launch the application
      cy.visit("https://accounts.google.com");
      // enable cookie logging
      Cypress.Cookies.debug(true)
      cy.getCookies
      //pause execution
      cy.pause()
      cy.setCookie('cookie1', 'value1' )
   });
});

Execution Results

Execution Results

输出如下 −

The output is as follows −

debugging

输出日志显示执行已暂停(由已暂停按钮表示)。然后,我们可以在调试完前一个步骤后再次恢复执行,方法是单击恢复按钮(显示在已暂停按钮旁边)。

The output logs show that the execution has been paused (denoted by Paused button).Then again, we can resume it after debugging the previous steps by clicking the Resume button (appear beside Paused button).

clicking the resume

输出日志现在包含从暂停恢复后执行的所有步骤。

The output logs now have all the steps executed after resume from pause.

如果我们在浏览器中打开开发者控制台(按 F12),并从测试运行器中选择一个步骤,控制台将显示所用命令和产生值。

If we open the Developer Console (pressing F12) on the browser, and select a step from the Test Runner, the Console shall show the Command used and the valued Yielded.

例如,对于 setCookie 步骤,控制台显示命令 − setCookie,而产生值显示 cookie 名称 − cookie1 和值 − value1。

For example, for the setCookie step, the Console shows Command − setCookie and Yielded shows the cookie name − cookie1 and value − value1.

setcookie