0


Cucumber.js对使用和Cypress某些步骤定义 的最佳实践有一些疑问。

基本上,测试需要一个可以相同实现的状态和结果:

这是一个状态的例子:

Given(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)

这是一个结果的例子:

Then(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)

在这种情况下,我只看到代码重复,因为GivenThen实现相同的代码,执行相同的检查。我想了解所有可能的解决方案,以最有效的方式实现这一点。

双重实施

保留描述中定义的双步骤
PRO:更清晰的
缺点:双代码\

单一实现

留下一个实现,在Given或 中Then,因为 cucumber 实际上并没有检查特征文件中的 Step 和定义文件中与 step 相关的函数的匹配
PRO:代码的单一实现
缺点:不太清楚,更混乱在步骤定义中

外部功能

创建一个函数以放入例如 Cypress 的某些命令或 utils 中,并在两个定义中使用它
PRO:单一实现
缺点:过度设计?在 Cucumber.js 中从未见过这种方法

检查所有这些,您会建议哪一个?谢谢

4

1 回答 1

1

还有一个选项,基本上是“单一实现”,但更清楚一点。我假设您正在使用cypress-cucumber-preprocessor,除了Given// WhenThen您还可以 import defineStepwhich 做完全相同的事情,但从代码的角度来看,更清楚的是,以这种方式定义的 step 是通用的。

import { defineStep } from "cypress-cucumber-preprocessor/steps";

defineStep('the {int} item of the {string} component is visible', 
    (index, ComponentIndex, mapper: ComponentMapper) => {
    ...
});
于 2021-03-14T14:50:06.890 回答