2

I am trying to get the current feature name or description when running tests. This is done in a Before hook, like this name = scenario.feature.name or name = scenario.feature.description. However it seems that feature was removed even from legacy API sometime back, and I can't find the new way of grabbing the feature information. Is it still possible to get the current feature name or description or any other information about the feature in the Cucumber 2.4 (Ruby gem)?

This is no longer in Cucumber gem 2.4.0.

Tyler
  • 51
  • 7
  • Did you try? Does it give an error? – Jeremie Mar 10 '17 at 23:51
  • Yes, the attribute does not exist anymore. I want to know if there is a replacement in the updated Cucumber for Ruby.. – Tyler Mar 11 '17 at 00:02
  • Just out of curiosity, what error does it return? – Jeremie Mar 11 '17 at 00:33
  • undefined method `feature' for nil:NilClass (NoMethodError) – Tyler Mar 11 '17 at 00:44
  • So, the scenario is nil. What about if you replace scenario.feature by just feature? – Jeremie Mar 11 '17 at 00:52
  • You'll be interested in [this](http://stackoverflow.com/q/218384/1863564) and [this](http://stackoverflow.com/q/4660142/1863564). While they're not directly Ruby-related, most of the answers are abstract enough to apply here. – Nic Mar 11 '17 at 06:03
  • I know if has an issue with a null pointer, I want to know where the feature part of scenario has been moved. – Tyler Mar 14 '17 at 19:44
  • @Tyler, please show a minimal hook and feature that exhibit the behaviour you mention. Like that we should be able to spot the bug. – Raffael Mar 14 '17 at 22:22
  • it looks there is no method named "name" in http://www.rubydoc.info/gems/cucumber/Cucumber/RunningTestCase/Scenario. where is the "name" method defined? – Shawn S Aug 12 '19 at 15:07

1 Answers1

3

The hooks now pass the scenario object to their blocks as an argument:

Before do |scenario|
  puts scenario.feature.name
  puts scenario.name
end

This works with cucumber 2.4.0 and its dependency cucumber-core 1.5.0.
I verified it with both Before and After hooks; BeforeStep and AfterStep hooks work differently.

Note that secenario.feature is a Cucumber::Core::Ast::Feature (no longer Cucumber::Ast::Feature; the core now lives in its own gem).

Raffael
  • 2,639
  • 16
  • 15
  • What version is that in? This is how I am doing it right now, and feature is no longer part of scenario. – Tyler Mar 14 '17 at 19:44
  • undefined method `feature' for nil:NilClass (NoMethodError) – Tyler Mar 14 '17 at 20:10
  • Definitely works on my machine ;) I added a bit more info to the answer. – Raffael Mar 14 '17 at 22:19
  • When you do this are you referencing the cucumber gem, or cucumber core gem? – Tyler Mar 14 '17 at 22:58
  • Tried feature = Cucumber::Core::Ast::Feature, and then getting name and description but the same error occurred. – Tyler Mar 14 '17 at 23:20
  • I got it working with just scenario name, and feature name now. I want to be able set certain variables based off of if the scenario is Cucumber::Core::Ast::Scenario or Cucumber::Core::Ast::ExamplesTable::Row. Using a switch statement based on scenario to see which it is. – Tyler Mar 14 '17 at 23:30
  • Ah, so that is what you were trying to do. Glad to hear you got it working. Please consider sharing your solution by adding your own answer to the question. I am curious, and other might be interested in the solutions as well. – Raffael Mar 16 '17 at 17:59
  • I just used the way you provided to get the name. I am unable to get the scenario or feature description though. Was that changed (using scenario.description, and it doesn't exist)? – Tyler Mar 16 '17 at 20:40
  • It looks like when running the above code it is scenario from http://www.rubydoc.info/gems/cucumber/Cucumber/RunningTestCase/Scenario , and not AST. This is why getting things like description is having issues. Is there way change this? – Tyler Mar 17 '17 at 20:16