如何在Cucumber-JVM中判断一个表的哪一行正在运行?

时间:2016-08-30 00:17:37

标签: java cucumber gherkin cucumber-java

我试图在“Scenario Outline”下的“Examples”中处理当前行计数。如何在步骤定义中获取当前行数?我不想在示例中传递数字。有什么办法吗?

Scenario Outline:
    When the name is entered as "<fruit>" <days>

Examples:
    |fruit|days|
    |Apple|10|
    |Orange|12|
    |Guava|3|
    ...

在步骤定义中,我希望将行处理为1,2,3,...(代替XXXXX)。

@When("^the name is entered as \"([^\"]*)\" ([^\"]*)$")
public void the_name_is_entered_as(String fruitName, int days) throws Throwable {
    System.out.println("Current Row processsed is: "+XXXXXXX)
}

2 个答案:

答案 0 :(得分:0)

我能想到的最好的方法(不是很漂亮)是自己计算幕后的行:

private static int rowIndex = 0;

@When("^I enter the name as \"([^\"]+)\"$")
public void i_enter_the_name_as(String fruit) {
    rowIndex++;
    // now you can use it in this and subsequent steps
}

这仅在&#34;我输入名称为&#34;仅用于单个场景大纲。如果你想在多个场景中使用它,那么我能想到的最好的事情就是在不同的场景中重置计数器。

答案 1 :(得分:0)

private Scenario scenario;

@Before
public void before(Scenario sce) {
    this.scenario = sce;
    System.out.println("SCENARIO ID -- " +scenario.getId());
}

您将获得类似于场景大纲的字符串 - 功能描述; scenariooutline-description;例子描述; rownumber + 1。

对于一个简单的场景,你会得到 - 特征描述;情景描述。

对于场景大纲案例,您可以使用分隔符“;”进行拆分并在减去1之后使用最后一部分。

这有点像黑客,取决于此模式中剩余的方案ID字符串。