如何在黄瓜功能之间使用通用/共享"块"?

Stu*_*art 19 cucumber

我是黄瓜新手,但享受它.

我目前正在编写一些Frank测试,并希望在多个功能中重复使用黄瓜块的块 - 如果可能的话,我想做黄瓜级别(不在红宝石内).

例如,我可能有4个脚本,都是从执行相同的登录步骤开始的:

  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 ***
Run Code Online (Sandbox Code Playgroud)

有没有办法在多个脚本之间共享前5行?某种"包含"语法?

Jon*_*n M 25

通常有两种方法:

背景

如果要在特征文件中的每个方案之前运行一组步骤:

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 ***
Run Code Online (Sandbox Code Playgroud)

从步骤定义调用步骤

如果您需要在不同的功能文件中使用"块"步骤,或者背景部分不适合,因为某些场景不需要它,那么创建一个调用其他场景的高级步骤定义:

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
Run Code Online (Sandbox Code Playgroud)

此外,在这种情况下,我很想不再将您的常用步骤作为单独的步骤实现,而是创建一个步骤定义:(假设Capybara)

Given /^I have logged in$/ do
    fill_in 'user-field', :with => 'guest'
    fill_in 'password-field', :with => '1234'
    click_button 'login'
end
Run Code Online (Sandbox Code Playgroud)

这为您的步骤定义带来了更多的意义,而不是创建一系列页面交互,需要在您意识到"哦,本节正在登录"之前进行心理解析.