Cucumber 场景大纲和带有通用步骤定义的示例示例、大纲、步骤、场景

2023-09-07 23:39:35 作者:在分类里有一种可以说是最拽最酷的,个性中带有幽默,幽默中带有

我有一个功能文件如下:

I have a Feature file which is as below:

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 |

这要求我对每个值都有步骤定义.我可以换一个

This mandates me to have step definitions for each of these values. Can i instead have a

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

generic step definition that can be mapped for every username or password or title values in

示例部分.

也就是说,而不是说

@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();
}

我可以进入吗

@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();
}

推荐答案

你应该使用这种格式

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

这会产生

@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();
}

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

arg1 will now have your username/value passed.