4

If I have a simple cucumber feature and scenario, like this (example code is from cucumber wiki):

Feature: Eating cucumbers

Scenario: eat 5 out of 12
  Given there are 12 cucumbers
  When I eat 5 cucumbers
  Then I should have 7 cucumbers

I know how to get feature and scenario name in before hook:

Before do |scenario|
  p [scenario.feature.name, scenario.name]
end

The above code returns:

["Eating cucumbers", "eat 5 out of 12"]

The problem is if the feature has scenario outline:

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |  12   |  5  |  7   |

When I run the above code I get:

undefined method `feature' for #<Cucumber::Ast::OutlineTable::ExampleRow:0x007fb0f94a8240> (NoMethodError)

How do I get feature and scenario outline name in cucumber before hook?

Željko Filipin
  • 56,372
  • 28
  • 94
  • 125

2 Answers2

6

Change before hook to this:

Before do |scenario|
  p [scenario.scenario_outline.feature.name, scenario.scenario_outline.name, scenario.name]
end

Output:

["Eating cucumbers", "eating", "| 12 | 5 | 7 |"]
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • 1
    scenario.feature is removed in cucumber 1.2.4 - I haven't yet found another way to get at the feature.name – user132837 Apr 08 '13 at 10:17
0

scenario.gets should give you feature name.

  • This answer is a stub and should rather be a [comment](https://stackoverflow.com/help/privileges/comment). Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to comment. If you don't have enough reputation, read [this post](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) on how to contribute if you don't have enough reputation yet. – ascripter Jun 08 '18 at 20:34