简体   繁体   中英

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

I am trying to integrate Cucumber with Test Rail. So I have a Cucumber Ruby automation setup.

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.

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. 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.

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

touch features/support/hooks.rb

second, add those contents in your hooks.rb file.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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