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

问题描述

我想执行一个测试用例,我尝试使用不同的凭据登录并检查错误消息。如何在 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
});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...