首页 > 解决方案 > 在'step 2'中执行'step 1' - cypress+cucumber

问题描述

我在步骤中遇到了许多类似的代码,在“更大”步骤中重复使用步骤可以解决问题。是否可以在步骤 2 中运行步骤 1?

And('My Step 1', () => {
  some code;
});

And('My Step 2', () => {
  can I execute 'My Step 1' here?
  code;
});

标签: cucumbercypressbddcypress-cucumber-preprocessor

解决方案


您可以在命令文件中添加这些步骤,然后在测试中调用它们。您的命令将如下所示:

Cypress.Commands.add("Step1", function () {
  Do stuff here
});

然后在您的测试中,您可以像这样重用这些命令:

it('My Step 2', () => {
  cy.Step1();
  code;
});

记得在你的 index.js 中导入命令文件


推荐阅读