【问题标题】:Using Before() in @badeball/cypress-cucumber-preprocessor在@badeball/cypress-cucumber-preprocessor 中使用 Before()
【发布时间】:2022-09-30 04:20:23
【问题描述】:

我是黄瓜测试的新手。我有一个可以正常工作的入门测试,但我需要在测试之间清除数据库。

这是场景:

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

如何处理 Cucumber 中数据库的清除?我见过Seeding Your Database in Node 的配方,但我如何在 Cucumber 中实现它?

    标签: cypress


    【解决方案1】:

    您有几个地方可以调用该任务。

    如果使用 Mocha beforeEach(),它将首先运行 - 在 Cucumber 方法之前。

    您还可以导入 Cucumber Before(),它是 Mocha 钩子的包装器并运行beforeEach() 电话。

    或者,您可以在 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(...)
    })
    

    【讨论】:

      【解决方案2】:

      在您的步骤文件中,您可以像在非黄瓜赛普拉斯测试中一样添加一个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', () => {
        ...
      })
      
      

      【讨论】:

        猜你喜欢
        • 2023-01-31
        • 2022-11-20
        • 2023-02-02
        • 2022-11-10
        • 1970-01-01
        • 2022-06-28
        • 1970-01-01
        • 2023-02-21
        • 1970-01-01
        相关资源
        最近更新 更多