如何参数化GivenStories以及我正在测试的当前故事?

时间:2013-03-29 18:55:39

标签: bdd reusability parameterized jbehave

我想重用一些常见的步骤。例如

Scenario: Navigate to Page 2 and perform first set of validations
Given A user<username> is logged in
When user clicks on <menuItem>
and page <page_name> is displayed
and user clicks on Submit button
Then Page <second_page> is displayed
And validate <condition_1>

Scenario: Navigate to Page 2 and perform second set of validations
Given A user<username> is logged in
When user clicks on <menuItem>
and page <page_name> is displayed
and user clicks on Submit button
Then Page <second_page> is displayed
And validate <condition_2>

Examples:
|username|page_name|menuItem|condition_1|condition_2|
|username1|Page1|Menu1|Condition1|Condition2|

现在,由于给定的条件相同,我想将故事规范化为类似的事情。

在precondition1.story

Given A user<username> is logged in
When user clicks on <menuItem>
and page <page_name> is displayed
and user clicks on Submit button
Then Page <second_page> is displayed

在currentTestStory.story中

GivenStories: precondition1.story

Scenario: Navigate to Page 2 and perform first set of validations
Given Page <second_page> is displayed
And validate <condition_1>

Scenario: Navigate to Page 2 and perform second set of validations
Given Page <second_page> is displayed
And validate <condition_2>

Examples:
|username|page_name|menuItem|condition_1|condition_2|
|username1|Page1|Menu1|Condition1|Condition2|

但我面临的挑战是,我无法从当前的故事中参数化GiveStory。有没有办法实现我想做的事情?

2 个答案:

答案 0 :(得分:0)

您可以从当前故事中参数化给定的故事。

请参阅第http://jbehave.org/reference/stable/given-stories.html页上的“按示例参数化的GivenStories”部分

缺点是你必须修改代码并在java步骤中添加@Named注释。因此,根据页面中的示例,以下方法可以处理示例中的参数:

@When("Something happens")
public void happening(@Named("One") String one, @Named("Two") String two) {
    //Do good
}

答案 1 :(得分:0)

我的方法是在这个故事中根本不使用GivenStories。使用步骤

Given Page <second_page> is displayed

然后在步骤中执行任何操作以显示第二页。