簡體   English   中英

如何在步驟定義中訪問Cucumber步驟名稱?

[英]How can I access a Cucumber step name in the step definition?

我正在嘗試將Cucumber與Test Rail集成。 因此,我有一個Cucumber Ruby自動化設置。

我希望能夠將功能文件中的黃瓜小黃瓜步驟作為變量傳遞到自動化系統中。

這是因為我想將黃瓜小黃瓜步驟作為HTTP POST發送到測試管理系統中。

示例小黃瓜功能文件:

Scenario: login scenario
    Given I am on webpage
    When I login
    Then I should see that I am logged in

步驟定義代碼:

Given(/^I am on webpage$/) do

#do this Given step from the regex match
#but also how do I, some how grab the string 'Given I am on webpage'
#so I can do an HTTP POST on that string

end

也許是一種更好的方法:在開始任何自動化測試之前,我先通過某種方式解析所有功能文件,並將HTTP POST發送到Test Rail以更新或填充我添加到Cucumber的任何新測試。 如果是這樣,我應該怎么做?

您可以像這樣捕獲步驟名稱:

Given(/^(I am on webpage)$/) do |step_name|
  puts step_name # or whatever
end

即使該步驟接受參數,它也可以工作:

Given(/^(I am on (my|your|their) webpage)$/) do |step_name, pronoun|
  puts step_name # or whatever
  visit send("#{pronoun}_path")
end

就是說,我同意Dave McNulla的評論,即Cucumber plus版本控制對測試管理系統的作用不大。

解析功能文件聽起來像一個單獨的問題。

我想您一定已經解決了這個問題,因為這個問題是兩年前提出的。 不過,我最近已經解決了這個問題,我認為也許我的解決方案可能會有所幫助。

兩步:

首先,在功能/支持下創建一個名為hooks.rb的新文件

touch features/support/hooks.rb

其次,將這些內容添加到hooks.rb文件中。

Before do |scenario| 
  $step_index = 0
  $stop_count = scenario.test_steps.count
  @scenario = scenario
end

AfterStep do |step|
  if $step_index < $stop_count
    puts "steps: #{@scenario.test_steps[$step_index].text}\n"
  end
  $step_index += 2
end

cucumber features/XXX.feature

您將在終端上找到打印出來的步驟名稱。

暫無
暫無

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

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