获取当前的步骤形式场景大纲

时间:2014-03-18 14:37:24

标签: ruby cucumber

我正在尝试访问方案大纲测试中的当前步骤名称。下面的代码适用于常规方案,但不适用于方案大纲。

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

例外:

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

有谁知道如何解决这个问题?或者如果可能的话?

3 个答案:

答案 0 :(得分:4)

尝试以下方式:

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

它必须适用于“场景”和“场景轮廓”

答案 1 :(得分:2)

这对我不起作用。我不得不做一些小改动:

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

答案 2 :(得分:0)

约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