【问题标题】:Is there a way to check which cucumber scenario called a certain step?有没有办法检查哪个黄瓜场景称为某个步骤?
【发布时间】:2018-11-27 20:08:16
【问题描述】:

我在两个不同的功能文件中有两个场景,但两个场景都测试搜索功能,但在我页面的不同部分。 我的场景看起来像这样:

Scenario Outline: Search items from QuickSearch
Given that the following items
  | id | title       | 
  | 1  | Item1       | 
  | 2  | Item2       |
When I search for <criteria> in this search
Then I should get <result>
And I should not get <excluded>

Examples:
|criteria|result    | excluded  |
| 1      | 1        | 2         |
| 2      | 2        | 1         |

和:

Scenario Outline: Using a filter
Given that I have the following things:
 |id |name     |
 |1  | thing1  |
 |2  | thing2  |
When I use the <filter> filled with <criteria>
Then I should obtain these <results>
And I should not obtain these <exclusions>

Examples:
|filter     |criteria   |results    |exclusions |
|name       |thing      |1,2        |           |
|id         |1          |1          |2          |

正如您在第二个场景中所说的那样,我已经更改了 get 一词,以便为这两个场景编写单独的步骤。 我需要两个不同步骤的唯一原因是因为 2 个不同场景中的 id 映射到不同的名称(不,我不能对两者使用相同的名称,并且不想在第二个中从 id 3 开始)

所以我正在考虑这两种场景的通用步骤(至少在涉及到 then 步骤时),我想要一个哈希映射 id 和 name 一起进行验证,但我希望哈希是根据调用步骤的场景而有所不同。

那么,cucumber + capybara 有没有办法可以分辨出哪个场景叫做 step?

【问题讨论】:

  • 值得注意的是 things != items.

标签: cucumber capybara


【解决方案1】:

我不知道从 Cucumber 步骤直接访问场景名称的方法。但是,您可以在 before 挂钩中访问该名称并将其存储在一个变量中,以便您的步骤可以使用它。

在钩子之前添加这个到你的env.rb:

Before do |scenario|
    case scenario
        when Cucumber::Ast::OutlineTable::ExampleRow
            @scenario_name = scenario.scenario_outline.name
        when Cucumber::Ast::Scenario
            @scenario_name = scenario.name
        else
            raise('Unhandled scenario class')
    end
end

编辑:如果您使用的是更新版本的 Cucumber,请尝试:

Before do |scenario|
  case scenario.source.last
  when Cucumber::Core::Ast::ExamplesTable::Row
    @scenario_name = scenario.scenario_outline.name
  when Cucumber::Core::Ast::Scenario
    @scenario_name = scenario.name
  else
    raise('Unhandled scenario class')
  end
end

然后您的步骤可以使用@scenario_name 检查方案名称。示例:

Then /I should get (.*)/ do |result|
  if @scenario_name == 'Search items from QuickSearch'
    # Do scenario specific stuff
  elsif @scenario_name == 'Using a filter'
    # Do scenario specific stuff
  end

  # Do any scenario stuff
end

【讨论】:

  • Tyvm,希望这已经以某种方式实现,但我可以使用它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2019-03-13
相关资源
最近更新 更多