簡體   English   中英

我如何找出我剛剛在Cucumber的AfterStep掛鈎中執行了哪個步驟?

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

我正在寫一個將在Cucumber的AfterStep回調上執行的方法。

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

如何確定在調用此掛鈎之前執行了哪個步驟?

使用寶石黃瓜2.1.0和方案大綱,“后續步驟”中的方案對象只是測試結果狀態,它不包含步驟的名稱。 我必須使用包含測試列表的“之前”(在第一步之前調用)。

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

記錄器是必需的,因為“ puts”僅在方案大綱結束時顯示。

AfterStep掛鈎僅將方案作為參數接收。

您可以做的是計算步驟,然后獲取當前步驟:

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

這將依次打印每個參數的名稱

注意:

api略有變化。 您現在需要使用“ to_a”

即Alex Siri的上述代碼行將更改為:

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

文斯有一個很好的解決方案,我建議重構:

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

您可以使用@ tests.count而不是@count變量

我本來可以將此作為評論,但是我還沒有足夠的聲譽。

我的工作如下:

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

  AfterStep do |step|
    @step_count += 1
  end

這樣可以使步驟號保持更新。 為了獲得步驟名稱:

@scenario.test_steps[@step_count].name

文斯的答案很好! SMAG的重構很棒! 但是當我將解決方案應用於黃瓜測試項目時,出現了一個錯誤:

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

因此,答案可能會更新如下:

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

API已更改...根據后鈎文檔,您可以獲取結果 (Cucumber :: Core :: Test :: Result)步驟 (Cucumber :: Core :: Test :: Step),如下所示:

AfterStep do |result, test_step|
  #do something
end

您可以使用以下步驟獲取步驟名稱:

stepName = test_step.text

要么

stepName = test_step.to_s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM