如何使用公共/共享“块";黄瓜功能之间? [英] How to use common/shared "blocks" between cucumber features?
问题描述
我是黄瓜的新手,但很喜欢它.
I'm new to cucumber, but enjoying it.
我目前正在编写一些 Frank 测试,并希望在多个功能中重用黄瓜脚本块 - 如果可能的话,我想在黄瓜级别执行此操作(而不是在 ruby 内部).
I'm currently writing some Frank tests, and would like to reuse blocks of cucumber script across multiple features - I'd like to do this a the cucumber level if possible (not inside the ruby).
例如,我可能有 4 个脚本都以执行相同的登录步骤开始:
For example, I might have 4 scripts that all start by doing the same login steps:
given my app has started
then enter "guest" in "user-field"
and enter "1234" in "password-field"
and press "login"
then I will see "welcome"
then *** here's the work specific to each script ***
有没有办法在多个脚本中共享前 5 行?某种包含"语法?
Is there any way to share these first 5 lines across multiple scripts? Some kind of "include" syntax?
推荐答案
一般有两种方法:
如果您希望在功能文件中的每个场景之前运行一组步骤:
If you want a set of steps to run before each of the scenarios in a feature file:
Background:
given my app has started
then enter "guest" in "user-field"
and enter "1234" in "password-field"
and press "login"
then I will see "welcome"
Scenario: Some scenario
then *** here's the work specific to this scenario ***
Scenario: Some other scenario
then *** here's the work specific to this scenario ***
从步骤定义中调用步骤
如果您需要在不同的功能文件中使用块"步骤,或者背景部分不适合,因为某些场景不需要它,那么创建一个调用其他步骤的高级步骤定义:
Calling steps from step definitions
If you need the 'block' of steps to be used in different feature files, or a Background section is not suitable because some scenarios don't need it, then create a high-level step definition which calls the other ones:
Given /^I have logged in$/ do
steps %Q {
given my app has started
then enter "guest" in "user-field"
and enter "1234" in "password-field"
and press "login"
then I will see "welcome"
}
end
<小时>
此外,在这种情况下,我会不将您的常用步骤作为单独的步骤来实现,而是创建一个单一的步骤定义:(假设 Capybara)
Also, in this case I'd be tempted not to implement your common steps as separate steps at all, but to create a single step definition: (assuming Capybara)
Given /^I have logged in$/ do
fill_in 'user-field', :with => 'guest'
fill_in 'password-field', :with => '1234'
click_button 'login'
end
这为您的步骤定义赋予了更多意义,而不是创建一系列页面交互,这些交互序列需要在您意识到哦,这部分正在让我登录"之前进行心理解析.
This lends a little bit more meaning to your step definitions, rather than creating a sequence of page interactions which need to be mentally parsed before you realise 'oh, this section is logging me in'.
这篇关于如何使用公共/共享“块";黄瓜功能之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!