【问题标题】:Get current Step form Scenario outline获取当前 Step 表单 Scenario outline
【发布时间】:2018-10-26 09:34:20
【问题描述】:

我正在尝试访问当前步骤名称以进行场景大纲测试。下面的代码适用于常规场景,但不适用于场景大纲。

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>

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

【问题讨论】:

  • 您为什么要这样做?我无法说出你想要达到的目标。 Cucumber 会免费告诉你哪一步失败了。
  • 我有一些代码可以用 Step 数据写出干净的文本结果。我想使用后续步骤将该数据以特殊格式写入文件
  • Cucumber 已经写出了哪个步骤运行和哪个失败了,您只需要使用排除 --no-source 从您的格式化程序。我尽量不做任何特别的事情,大多数功能都带有它。您可以尝试在 github.com/cucumber/cucumber/wiki/… 查找自定义格式化程序
  • 谢谢@DaveMcNulla 感谢您的帮助。感谢您的参考。我会做更多的研究。

标签: ruby cucumber


【解决方案1】:

试试下面的方法:

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

它必须适用于“场景”和“场景大纲”

【讨论】:

  • 感谢您的帮助。我开始认为这是不可能的。
  • 这似乎随着版本的变化而改变。我不得不使用step_title = current_feature.feature_elements[0].send(:raw_steps).to_a[@step_count]
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    大约 2018 年 10 月更新

    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
    

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 2019-12-16
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多