Cypress 简明教程

Cypress - Hooks

Cypress Hooks 用于在每次测试之前/之后执行某些操作。一些常见的 hooks 如下:

Cypress Hooks are used to carry out the certain operations prior/post every/each test.Some of the common hooks are as follows −

  1. before − It is executed, once the prior execution of any tests within a describe block is carried out.

  2. after − It is executed, once the post execution of all the tests within a describe block is carried out.

  3. beforeEach − It is executed prior to the execution of an individual, it blocks within a describe block.

  4. afterEach − It is executed post execution of the individual, it blocks within a describe block.

Implementation

下面对 Cypress Hooks 命令的实现进行了说明:

The implementation of commands for the Cypress Hooks is explained below −

describe('Tutorialspoint', function() {
   before(function() {
      // executes once prior all tests in it block
      cy.log("Before hook")
   })
   after(function() {
      // executes once post all tests in it block
      cy.log("After hook")
   })
   beforeEach(function() {
      // executes prior each test within it block
      cy.log("BeforeEach hook")
   })
   afterEach(function() {
      // executes post each test within it block
      cy.log("AfterEac hook")
   })
   it('First Test', function() {
      cy.log("First Test")
   })
   it('Second Test', function() {
      cy.log("Second Test")
   })
})

Execution Results

Execution Results

输出如下:

The output is mentioned below −

cypress hooks

输出日志显示,首先执行的步骤是 BEFORE ALL。

The output logs show that the first executed step is the BEFORE ALL.

最后一个执行的步骤是 AFTER ALL。两者都只运行一次。

The last executed step is the AFTER ALL. Both of them ran only once.

在每个 BEFORE EACH 下执行的步骤运行两次(在每个 TEST BODY 之前)。

The step executed under BEFORE EACH ran twice (before each TEST BODY).

而且,在每个 AFTER EACH 下执行的步骤运行两次(在每个 TEST BODY 之后)。

Also, step executed under AFTER EACH ran twice (after each TEST BODY).

这两个 it 块按实现的顺序执行。

Both the it blocks are executed in order, in which they are implemented.

TAG

除了钩子,Cypress 还有标签 - .only 和 .skip。

Apart from hooks, Cypress has tags - .only and .skip.

而 .only 标签用于执行它所标记的 it 块,.skip 标签则用于排除它所标记的 it 块。

While the .only tag is utilised to execute the it block to which it is tagged, the .skip tag is utilised to exclude the it block to which it is tagged.

Implementation with .only

only 标签在 Cypress 中的实现如下 −

The implementation of .only tag in Cypress is as follows −

describe('Tutorialspoint', function()
   //it block with tag .only
   it.only('First Test', function() {
      cy.log("First Test")
   })
   //it block with tag .only
   It.only('Second Test', function() {
      cy.log("Second Test")
   })
   it('Third Test', function() {
      cy.log("Third Test")
   })
})

Execution Results

Execution Results

输出如下 −

The output is given below −

cypress has tags

输出日志显示带有 .only 标签的 it 块(第一个和第二个测试)只得到了执行。

The output logs show that the it blocks (First and Second Test) with the .only tags only got executed.

Implementation with .skip

skip 标签在 Cypress 中的实现如下 −

The implementation of .skip tag in Cypress is as follows −

describe('Tutorialspoint', function()
   it('First Test', function() {
      cy.log("First Test")
   })
   it('Second Test', function() {
      cy.log("Second Test")
   })
   //it block with tag .skip
   it.skip('Third Test', function() {
      cy.log("Third Test")
   })
})

Execution Results

Execution Results

输出如下 −

The output is as follows −

cypress skip tags

输出日志显示带有 .skip 标签的 it 块(第三个测试)跳过了执行。

The output logs show that the it block (Third Test) with the .skip tag got skipped from the execution.