Question

I am trying to access the current step name in for a scenario outline test. The code below works for regular scenarios but fails for scenario outlines.

AfterStep do |scenario|
  #step = scenario.steps.find { |s| s.status == :skipped }
  #puts step.keyword + step.name

  case scenario
    when Cucumber::Ast::Scenario
      step = scenario.steps.find { |s| s.status == :skipped }
      puts step.keyword + ' ' + step.name
      #Works correctly

    when Cucumber::Ast::OutlineTable::ExampleRow
      # **!!!!Exception below!!!**
      step = scenario.scenario_outline.steps.find { |s| s.status == :skipped }
      puts step.keyword + ' ' + step.name

  end
end

Exception:

NoMethodError: undefined method `steps' for #<Cucumber::Ast::OutlineTable::ExampleRow:0x007fe0b8214bc0>

Does anyone know how to resolve this? Or if this is possible?

Was it helpful?

Solution

Try the following way:

Before do |scenario|
   ...
   # step counter
   @step_count = 0
end

AfterStep do |step|
   current_feature = if scenario.respond_to?('scenario_outline')
                     # execute the following code only for scenarios outline (starting from the second example)
                       scenario.scenario_outline.feature
                     else
                     # execute the following code only for a scenario and a scenario outline (the first example only)
                       scenario.feature
                     end


   # call method 'steps' and select the current step
   # we use .send because the method 'steps' is private for scenario outline
   step_title = current_feature.feature_elements[0].send(:steps).to_a[@step_count].name

   p step_title

   # increase step counter
   @step_count += 1
end

It has to work fine for both 'scenario' and 'scenario outline'

OTHER TIPS

This did not work for me. I had to make some minor changes:

step_title = step.steps.to_a[@step_count].gherkin_statement.name

Updates circa 10/2018

Before do |scenario|
  # the scenario in after step is Cucumber::Core::Test::Result::Passed and does not have the data
  @scenario = scenario
  # step counter
  @step_count = 0
end

AfterStep do
  feature = @scenario.feature # works for outlines too

  if feature.feature_elements[0].respond_to?(:steps)
    step_title = feature.feature_elements[0].send(:steps).to_a[@step_count].to_s
  else
     step_title = feature.feature_elements[0].send(:raw_steps).to_a[@step_count].to_s
  end

  puts "[finished step] #{step_title}"

  # increase step counter
  @step_count += 1
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top