如何从 Javascript 中的 Cucumber table 中提取数据?

How I can pull data from a Cucumber table in Javascript?

我想执行一个测试用例,尝试使用不同的凭据登录并检查错误消息。这如何在 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。您需要重新表述每个步骤,数据 table 将移至“示例”table:

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 |

场景将针对示例中的每一行执行一次 table。步骤中的 <...> 标记允许您引用示例 table 列之一中的值。

您的 Then 步骤需要改写以通过预期的验证错误。它的步骤定义非常简单,我将把实现留给你。这是存根:

Then('the (string) error msg should appear', (expectedError) => {
  // TODO: Make assertion
});