问题描述
我想执行一个测试用例,我尝试使用不同的凭据登录并检查错误消息。如何在 Cucumber 中做到这一点?
Feature: Login
Login Test Suite
Background:
Given I'm on the login page
Scenario: 01. Should not be able to login with invalid cred
When I log in with "username" and "password"
| username | password | ExpectedError |
| asdasd | anything | Invalid credentials specified |
| | anything | Please specify a username |
| asdasd | | Please specify a password |
| | | No username or password specified |
Then An error msg should appear
这里我想传递两个参数,用户名和密码:
When('I log in with (string) and (string)',(username,password) => {
p.loginWith(username,password)
})
解决方法
看起来您想要一个 Scenario Outline。您需要重新表述每个步骤,数据表将移至“示例”表:
Feature: Login
Login Test Suite
Background:
Given I'm on the login page
Scenario Outline: 01. Should not be able to login with invalid cred
When I log in with "<username>" and "<password>"
Then the "<ExpectedError>" error msg should appear
Examples:
| username | password | ExpectedError |
| asdasd | anything | Invalid credentials specified |
| | anything | Please specify a username |
| asdasd | | Please specify a password |
| | | No username or password specified |
该场景将针对示例表中的每一行执行一次。步骤中的 <...>
标记允许您引用示例表列之一中的值。
您的 Then
步骤需要重新表述以通过预期的验证错误。它的步骤定义非常简单,我将把实现留给你。这是存根:
Then('the (string) error msg should appear',(expectedError) => {
// TODO: Make assertion
});