如何使用赛普拉斯数据表循环多个测试场景?

How to use Cypress datatables to loop through multiple test scenarios?

我正在尝试使用数据表来测试下面我的赛普拉斯场景中的一个输入字段

 Scenario: The one where the user enters a value to calculate the factorial
     Given the user navigates to the Factoriall calculator screen
     When the user enters a value into the input box
         | value    |
         | 12       |
         | 34.56    |

     And the user clicks the Calculate button
     Then a message is displayed to the user
         | message                           |
         | The factorial of 12 is: 479001600 |
         | Please enter an integer           |

这是当前的测试行为:

  1. 输入框输入12
  2. 输入框输入34.56
  3. 已单击“计算”按钮。
  4. 消息已验证(显示“请输入整数”),因为输入内容为 1234.56

这就是我希望测试的行为方式:

  1. 输入框输入12
  2. 已单击“计算”按钮。
  3. 显示阶乘值。
  4. 输入框清空
  5. 输入框输入34.56
  6. 已单击“计算”按钮。
  7. 显示错误信息。

我知道我当前的逻辑存在问题,因为它在单击按钮之前在开头输入了所有文本,但是有人可以告诉我需要进行哪些更新以便我可以按预期进行此测试吗?

这是我的步骤定义:

When('the user enters a value into the input box', (userInputs) => {
    userInputs.hashes().forEach((userInput) => {
        myValue = userInput.value
        factoriall.getInputBox().type(myValue)
    });
});

When('the user clicks the Calculate button', () => {
    factoriall.getBtnCalculate().click();
});

Then('a message is displayed to the user', (expectedMessages) => {
    expectedMessages.hashes().forEach((expectedMessage) => {
        factoriall.getInputBox().clear();
        myMessage = expectedMessage.message
        factoriall.getResultParagraph().should('have.text', myMessage)
    });
});

最好使用Scenario Outline
使用这个,你的整个场景将被执行 2 次(或者你在 Examples 下的 table 中添加的行数)并且每次,带尖括号的词 <value><message> 将替换为 Examples: table.

中的相应值
Scenario Outline: The one where the user enters value <value> to calculate the factorial
     Given  the user navigates to the Factoriall calculator screen
     When   the user enters value <value> into the input box
     And    the user clicks the Calculate button
     Then   this message is displayed to the user: <message>
     Examples:
         | value | message                           |
         | 12    | The factorial of 12 is: 479001600 |
         | 34.56 | Please enter an integer           |

要使用值 <value><message>,您需要这样调用您的步骤:

When(/^the user enters value (.+) into the input box$/, (value) => {
    ...
});

Then(/^this message is displayed to the user: (.+)$/, (message) => {
    ...
});

在我上面的示例中,我使用正则表达式来捕获我的变量。还有其他方法,但我更喜欢这个。