variables - 如何在标题中获取 cucumber 场景变量?

标签 variables cucumber scenarios

通过使用标题本身中的示例,我希望能够让我的场景大纲标题包含更多信息:

 Scenario Outline: A <some> step is <result>
    When a <some> step
    Then I get <result>
    Examples:
    | some    | result  |
    | passing | passed  |
    | failing | skipped |
    Then my scenario titles end up very useful:
    Scenario: A passing step is passed
    Scenario: A failing step is skipped

最佳答案

Then关键字必须高于 Examples .

Feature: Scenario outline with variables

    Scenario Outline: A "<some>" step is "<result>"
      When a "<some>" step
      Then I get "<result>"
      Then my scenario titles end up very useful
      Examples:
        | some    | result  |
        | passing | passed  |
        | failing | skipped |

用胶水 ScratchSteps.java
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class ScratchSteps {

    private String step;
    private String result;

    @Then("^my scenario titles end up very useful$")
    public void myScenarioTitlesEndUpVeryUseful() throws Throwable {
        System.out.printf("step: %s  result: %s%n", step, result);
    }

    @When("^a \"([^\"]*)\" step$")
    public void aStep(String step) throws Throwable {
        this.step = step;
    }

    @Then("^I get \"([^\"]*)\"$")
    public void iGet(String result) throws Throwable {
        this.result = result;
    }
}

输出是
Feature: Scenario outline with variables

  Scenario Outline: A "<some>" step is "<result>" # features/scratch.feature:3
    When a "<some>" step
    Then I get "<result>"
    Then my scenario titles end up very useful

    Examples: 

  Scenario Outline: A "passing" step is "passed" # features/scratch.feature:9
    When a "passing" step                        # ScratchSteps.aStep(String)
    Then I get "passed"                          # ScratchSteps.iGet(String)
step: passing  result: passed
    Then my scenario titles end up very useful   # ScratchSteps.myScenarioTitlesEndUpVeryUseful()

  Scenario Outline: A "failing" step is "skipped" # features/scratch.feature:10
    When a "failing" step                         # ScratchSteps.aStep(String)
    Then I get "skipped"                          # ScratchSteps.iGet(String)
step: failing  result: skipped
    Then my scenario titles end up very useful    # ScratchSteps.myScenarioTitlesEndUpVeryUseful()

2 Scenarios (2 passed)
6 Steps (6 passed)

关于variables - 如何在标题中获取 cucumber 场景变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50631176/

相关文章:

postgresql - Talend:将 db_connection 参数设置为变量

java - 如何从 p 标签获取项目列表

notifications - 使用 Zabbix Web 场景状态进行通知

javascript - 通过步骤定义和页面对象在 cucumber Protractor 中实现场景大纲

java - 在 Java 中打印字符串变量

php - jquery 和 php + 如果 session 已死,则返回错误

javascript - 变量在 for 循环中没有改变 - JavaScript

Selenium 、 capybara 和 cucumber 测试拖放重新排序

mstest - 我们如何检索/获取步骤定义中的功能和场景标题?