简体   繁体   English

我如何找出我刚刚在Cucumber的AfterStep挂钩中执行了哪个步骤?

[英]How can I figure out which step I've just executed in Cucumber's AfterStep hook?

I'm writing a method to be executed on the AfterStep callback for Cucumber. 我正在写一个将在Cucumber的AfterStep回调上执行的方法。

https://github.com/cucumber/cucumber/wiki/Hooks#step-hooks https://github.com/cucumber/cucumber/wiki/Hooks#step-hooks

How can I figure out which step was executed before this hook was called? 如何确定在调用此挂钩之前执行了哪个步骤?

Using gem cucumber 2.1.0 and scenario outlines, the scenario object in "Afterstep" is just a test result status, it does not contain the name of the step. 使用宝石黄瓜2.1.0和方案大纲,“后续步骤”中的方案对象只是测试结果状态,它不包含步骤的名称。 I had to use "Before" (called before the first step) that contains a test list. 我必须使用包含测试列表的“之前”(在第一步之前调用)。

require 'logger'

$logger = Logger.new(STDOUT)

Before do |scenario|   

    @count = 0       
    @tests = Array.new

    scenario.test_steps.each{|r|
         if( r.name != "AfterStep hook")
              @tests << r
         end
     }
 end

 AfterStep do |scenario|  # run after each step

     $logger.info(@tests[@count].name.green)
     @count += 1;

 end

The logger is required because 'puts' only display when the scenario outline ends. 记录器是必需的,因为“ puts”仅在方案大纲结束时显示。

The AfterStep hook only receives the scenario as parameter. AfterStep挂钩仅将方案作为参数接收。

What you can do, is count the steps, and then get the current one: 您可以做的是计算步骤,然后获取当前步骤:

AfterStep do |scenario|
  @step ||= 0
  p scenario.steps[@step].name
  @step += 1
end

This will print, in turn, the names of each parameter 这将依次打印每个参数的名称

Note: 注意:

The api has changed slightly. api略有变化。 you now need to use 'to_a' 您现在需要使用“ to_a”

ie the Alex Siri's line above would be changed to: 即Alex Siri的上述代码行将更改为:

  p scenario.steps.to_a[@step].name

Vince has a good solution, I would recommend a refactor: 文斯有一个很好的解决方案,我建议重构:

Before do |scenario|
    @tests = scenario.test_steps.map(&:name).delete_if { |name| name == 'AfterStep hook' }
end

You can use the @tests.count instead of the @count variable 您可以使用@ tests.count而不是@count变量

I would have made this as comment but I don't have enough reputation yet. 我本来可以将此作为评论,但是我还没有足够的声誉。

I worked it out as follows: 我的工作如下:

Before do |scenario|
    ...
    @scenario = scenario
    @step_count = 0
    ...
  end

  AfterStep do |step|
    @step_count += 1
  end

That keeps the step number updated. 这样可以使步骤号保持更新。 In order to get the step name: 为了获得步骤名称:

@scenario.test_steps[@step_count].name

Vince's answer is great! 文斯的答案很好! and SMAG's refactor is cool ! SMAG的重构很棒! but when I applied the solution on my cucumber test project, I got an error: 但是当我将解决方案应用于黄瓜测试项目时,出现了一个错误:

 undefined method `name' for #<Cucumber::Core::Test::Step:>

so, maybe the answer can update as below: 因此,答案可能会更新如下:

Before do |scenario|
    @tests = scenario.test_steps.map(&:text).delete_if { |text| text == 'AfterStep hook' }
end

The API has been changed... Based on afterhook doc you can get the result (Cucumber::Core::Test::Result) and the step (Cucumber::Core::Test::Step) like this: API已更改...根据后钩文档,您可以获取结果 (Cucumber :: Core :: Test :: Result)步骤 (Cucumber :: Core :: Test :: Step),如下所示:

AfterStep do |result, test_step|
  #do something
end

You can get the step name with: 您可以使用以下步骤获取步骤名称:

stepName = test_step.text

or 要么

stepName = test_step.to_s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM