黄瓜方案大纲和具有通用步骤定义的示例

tri*_*999 14 cucumber cucumber-jvm

我有一个Feature文件,如下所示:

Scenario Outline: Create ABC

  Given I open the application

  When I enter username as <username>

  And I enter password as <password>

  Then I enter title as <title>

  And press submit


Examples:

| username | password | title |

| Rob      | xyz1      | title1 |

| Bob      | xyz1      | title2 |
Run Code Online (Sandbox Code Playgroud)

这要求我为每个值都有步骤定义.我可以改为

通用步骤定义,可以为每个用户名或密码或标题值映射

示例部分.

即而不是说

@When("^I enter username as Rob$")
public void I_enter_username_as_Rob() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}
Run Code Online (Sandbox Code Playgroud)

我可以进入

@When("^I enter username as <username>$")
public void I_enter_username_as_username(<something to use the value passed>) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*ala 32

你应该使用这种格式

Scenario Outline: Create ABC

    Given I open the application
    When I enter username as "<username>"
    And I enter password as "<password>"
    Then I enter title as "<title>"
    And press submit
Run Code Online (Sandbox Code Playgroud)

哪个会产生

@When("^I enter username as \"([^\"]*)\"$")
public void I_enter_username_as(String arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}
Run Code Online (Sandbox Code Playgroud)

arg1 现在将传递您的用户名/值.

  • 谢谢!我还想解释一下\"([^ \"]*)\"是一个正则表达式,它会查找以引号开头的字符串",并以未定义的其他字符数结尾(星号*表示).因此,如果我们查看"<username>",我们可以看到它以引号开头".希望有助于某人. (3认同)
  • 由@hipokito澄清,作为**替代**所以`@When("^我输入用户名为\"(.+)\"$") (2认同)