如何使用公共/共享“块”黄瓜特征之间? [英] How to use common/shared "blocks" between cucumber features?

查看:167
本文介绍了如何使用公共/共享“块”黄瓜特征之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的黄瓜,但很喜欢它。

I'm new to cucumber, but enjoying it.

我目前正在写一些弗兰克测试,并希望在多个功能中重复使用黄瓜脚本 - 我想这样做一个黄瓜水平如果可能(不在里面的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?

推荐答案

通常有两种方法:

如果您想要一组步骤

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆