Tyler
Tyler

Reputation: 51

Grab scenario information in Before and After hooks?

Right now I am trying to grab the information from the current scenario (description, file path, name, etc.). I try to get the scenario information in the before hook like this

Before do |scenario|
    puts scenario.name
    puts scenario.description
    #etc.
end

however things like scenario.description are not available for use. This is because when running in both Before and After hook, scenario is being called as Cucumber::RunningTestCase::Scenario instead part of the core AST module, like feature. I want to be able to access the scenario from Cucumber::Core::Ast::Scenario and Cucumber::Core::Ast::OutlineTable::ExampleRow, where values like current scenario description is available. Is it possible to get access to Cucumber::Core::Ast::Scenario in before and after hooks instead of Cucumber::RunningTestCase::Scenario, or another way to grab information like scenario name, description, file path, etc.? This is using the latest version of the cucumber and cucumber core gems (2.4 and 1.5). In addition, I am also no longer to able to access information about the current step, and step count.

Upvotes: 1

Views: 936

Answers (1)

SMAG
SMAG

Reputation: 798

Before do |scenario|
  all_sources = scenario.outline? ? scenario.scenario_outline.all_source : scenario.all_source
end

From the all_source variable you will have access to several objects from ::Ast

Scenario Outline:

  • Cucumber::Core::Ast::Feature

  • Cucumber::Core::Ast::ScenarioOutline

  • Cucumber::Core::Ast::Examples

  • Cucumber::Core::Ast::ExamplesTable::Row:

  • Cucumber::Core::Ast::ExpandedOutlineStep

Scenario:

  • Cucumber::Core::Ast::Feature

  • Cucumber::Core::Ast::Scenario

  • Cucumber::Core::Ast::Step

From these you should be able to get data about file path, descriptions, comments, tags, hooks, line numbers and so on.

Upvotes: 2

Related Questions