简体   繁体   English

使用@badeball/cypress-cucumber-preprocessor 清除测试数据

[英]Cleardown test data using @badeball/cypress-cucumber-preprocessor

I am new to Cucumber testing.我是 Cucumber 测试的新手。 I have a starter test which works ok, but I need to clear down the database in between tests.我有一个可以正常工作的入门测试,但我需要在测试之间清除数据库。

This is the scenario:这是场景:

Scenario: Should update the stock levels
 Given user is on the product page
 When user updates the stock quantity
 Then the new stock quantity is available on the product page

How do I handle cleardown for the the database in Cucumber?如何处理 Cucumber 中的数据库的清除? I've seen the recipe Seeding Your Database in Node but how do I implement that in Cucumber?我已经看到了在 Node 中播种数据库的配方,但是如何在 Cucumber 中实现它?

In your step file, you can just add a beforeEach() hook exactly as you would in a non-cucumber Cypress test.在您的步骤文件中,您可以像在非黄瓜赛普拉斯测试中一样添加一个beforeEach()挂钩。

import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";

beforeEach(() => {
  // reset database
  cy.fixture('my-data.json').then(data => {
    cy.task('seed:db', data)
  })
})

Given('user is on the product page', () => {
  ...
})

You have a few places to call the task.您有几个地方可以调用该任务。

If using the Mocha beforeEach() , it will run first - ahead of the Cucumber methods.如果使用 Mocha beforeEach() ,它将首先运行 - 在 Cucumber 方法之前。

You can also import Cucumber Before() which is a wrapper around the Mocha hook and runs after the beforeEach() call.您还可以导入 Cucumber Before() ,它是 Mocha 挂钩的包装器,在beforeEach()调用之后运行。

Or you could call the database seed task at the start of the Given() method.或者,您可以在Given()方法的开头调用数据库种子任务。

import { When, Then, Before, Given } from "@badeball/cypress-cucumber-preprocessor";

beforeEach(() => {
  // reset database
  cy.fixture('dbdata.json').then(data => {
    cy.task('seed:db', data)
  })
})

Before(() => {
  // reset database
  cy.fixture('dbdata.json').then(data => {
    cy.task('seed:db', data)
  })
})

Given('user is on the product page', () => {
  // reset database
  cy.fixture('dbdata.json').then(data => {
    cy.task('seed:db', data)
  })
  cy.visit(...)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @badeball/cypress-cucumber-preprocessor not generating.json 报告 - @badeball/cypress-cucumber-preprocessor not generating .json report 使用 TypeScript 和 Cypress 10+ 安装和配置 @badeball/cypress-cucumber-preprocessor 的正确方法 - The proper way to install and configure @badeball/cypress-cucumber-preprocessor with TypeScript and Cypress 10+ cypress-cucumber-preprocessor 数据表 - cypress-cucumber-preprocessor Datatables Cypress-Cucumber-Preprocessor:对特定标签/功能文件使用 BeforeAll - Cypress-Cucumber-Preprocessor: Using BeforeAll for specific tags/feature files 柏树棒球所有者的柏树黄瓜预处理器 - Cypress-cucumber-preprocessor for cypress baseball owner 在 Angular 中使用 Cypress 10 配置 cypress-cucumber-preprocessor - Configure cypress-cucumber-preprocessor with Cypress 10 in Angular BeforeEach 使用 cypress-cucumber-preprocessor 与 cy.session 重复 - BeforeEach step is repeated with cy.session using cypress-cucumber-preprocessor Typescript/cypress-cucumber-preprocessor:用动态示例编写场景大纲 - Typescript/cypress-cucumber-preprocessor : Writing a Scenario Outline with dynamic examples Cypress-cucumber-preprocessor 未运行测试和错误:“缺少步骤实现” - Cypress-cucumber-preprocessor not running tests & error: "Step implementation missing for" 无法使用 cypress-cucumber-preprocessor 进行标记 - Can't get tagging to work with cypress-cucumber-preprocessor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM