简体   繁体   English

如何在步骤定义中访问Cucumber步骤名称?

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

I am trying to integrate Cucumber with Test Rail. 我正在尝试将Cucumber与Test Rail集成。 So I have a Cucumber Ruby automation setup. 因此,我有一个Cucumber Ruby自动化设置。

I would like to be able to pass the Cucumber Gherkin steps from the feature file as a variable into the automation. 我希望能够将功能文件中的黄瓜小黄瓜步骤作为变量传递到自动化系统中。

It is because I would like to send the Cucumber Gherkin steps as HTTP POST into a test management system. 这是因为我想将黄瓜小黄瓜步骤作为HTTP POST发送到测试管理系统中。

Example gherkin feature file: 示例小黄瓜功能文件:

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

Step definition code: 步骤定义代码:

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

Or a better way, maybe: Before I start any automated tests, I run through some sort of way to parse all the feature files and send HTTP POST to Test Rail to update or populate any new tests that I added into Cucumber. 也许是一种更好的方法:在开始任何自动化测试之前,我先通过某种方式解析所有功能文件,并将HTTP POST发送到Test Rail以更新或填充我添加到Cucumber的任何新测试。 If that is the case, how should I go about that? 如果是这样,我应该怎么做?

You can capture the step name like this: 您可以像这样捕获步骤名称:

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

It works even if the step takes arguments: 即使该步骤接受参数,它也可以工作:

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

That said, I agree with Dave McNulla's comment that Cucumber plus version control doesn't leave much for a test management system to do. 就是说,我同意Dave McNulla的评论,即Cucumber plus版本控制对测试管理系统的作用不大。

Parsing feature files sounds like a separate question. 解析功能文件听起来像一个单独的问题。

I suppose you must have solved the problem cause this question was asked two years ago. 我想您一定已经解决了这个问题,因为这个问题是两年前提出的。 still, I have solved it recently and I think maybe my solution can make some sense. 不过,我最近已经解决了这个问题,我认为也许我的解决方案可能会有所帮助。

Two steps: 两步:

first, create a new file named hooks.rb under features/support 首先,在功能/支持下创建一个名为hooks.rb的新文件

touch features/support/hooks.rb

second, add those contents in your hooks.rb file. 其次,将这些内容添加到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

run

cucumber features/XXX.feature

you will get to find the step name print out on your terminal. 您将在终端上找到打印出来的步骤名称。

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

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