Skip to content

Instantly share code, notes, and snippets.

@andrzejsliwa
Created January 13, 2011 20:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andrzejsliwa/778535 to your computer and use it in GitHub Desktop.
Save andrzejsliwa/778535 to your computer and use it in GitHub Desktop.
list all available cucumber steps - (rake cucumber:steps)
# based on http://www.natontesting.com/2010/01/11/updated-script-to-list-all-cucumber-step-definitions/
#
desc 'List all defined steps'
task :steps do
require 'hirb'
extend Hirb::Console
puts "CUCUMBER steps:"
puts ""
step_definition_dir = "features/step_definitions"
Dir.glob(File.join(step_definition_dir,'**/*.rb')).each do |step_file|
puts "File: #{step_file}"
puts ""
results = []
File.new(step_file).read.each_line.each_with_index do |line, number|
next unless line =~ /^\s*(?:Given|When|Then)\s+|\//
res = /(?:Given|When|Then)[\s\(]*\/(.*)\/([imxo]*)[\s\)]*do\s*(?:$|\|(.*)\|)/.match(line)
next unless res
matches = res.captures
results << OpenStruct.new(
:steps => matches[0],
:modifier => matches[1],
:args => matches[2]
)
end
table results, :resize => false, :fields=>[:steps, :modifier, :args]
puts ""
end
end
# alternative version ... single box :)
# task :steps do
# require 'hirb'
# extend Hirb::Console
# puts "CUCUMBER steps:"
# puts ""
# step_definition_dir = "features/step_definitions"
#
# results = []
# add_empty = lambda { results << OpenStruct.new(:step => "", :line_number => "", :args => "") }
# add_step = lambda do |step, line_number, args|
# results << OpenStruct.new(:step => step, :line_number => line_number, :args => args)
# end
# Dir.glob(File.join(step_definition_dir,'**/*.rb')).each do |step_file|
#
# add_empty.call
# add_step.call("File: #{step_file}", "", "")
# add_empty.call
#
# File.new(step_file).read.each_line.each_with_index do |line, number|
#
# next unless line =~ /^\s*(?:Given|When|Then)\s+|\//
# res = /(?:Given|When|Then)[\s\(]*\/(.*)\/([imxo]*)[\s\)]*do\s*(?:$|\|(.*)\|)/.match(line)
# next unless res
# matches = res.captures
# add_step.call(matches[0], number, matches[2])
# end
# end
# table results, :resize => false, :fields=>[:step, :line_number, :args]
# end
using:
rake cucumber:steps
output:
CUCUMBER steps:
File: features/step_definitions/email_steps.rb
+------------------------------------------------------------+----------+-------------------+
| steps | modifier | args |
+------------------------------------------------------------+----------+-------------------+
| ^all emails? (?:have|has) been delivered$ | | |
| ^(\d)+ emails? should be delivered$ | | count |
| ^(?:I|they) follow "([^"]*?)" in #{capture_email}$ | | link, email_ref |
| ^(?:I|they) click the first link in #{capture_email}$ | | email_ref |
| ^(\d)+ emails? should be delivered to (.*)$ | | count, to |
| ^(\d)+ emails? should be delivered with #{capture_fields}$ | | count, fields |
| ^#{capture_email} should be delivered to (.+)$ | | email_ref, to |
| ^#{capture_email} should not be delivered to (.+)$ | | email_ref, to |
| ^#{capture_email} should have #{capture_fields}$ | | email_ref, fields |
| ^#{capture_email} should contain "(.*)"$ | | email_ref, text |
| ^#{capture_email} should not contain "(.*)"$ | | email_ref, text |
| ^#{capture_email} should link to (.+)$ | | email_ref, page |
| ^show me the emails?$ | | |
+------------------------------------------------------------+----------+-------------------+
13 rows in set
File: features/step_definitions/pickle_steps.rb
+--------------------------------------------------------------------------------------+----------+----------------------------------------+
| steps | modifier | args |
+--------------------------------------------------------------------------------------+----------+----------------------------------------+
| ^#{capture_model} exists?(?: with #{capture_fields})?$ | | name, fields |
| ^(\d+) #{capture_plural_factory} exist(?: with #{capture_fields})?$ | | count, plural_factory, fields |
| ^the following #{capture_plural_factory} exists?:?$ | | plural_factory, table |
| ^#{capture_model} should exist(?: with #{capture_fields})?$ | | name, fields |
| ^#{capture_model} should not exist(?: with #{capture_fields})?$ | | name, fields |
| ^the following #{capture_plural_factory} should exists?:?$ | | plural_factory, table |
| ^(\d+) #{capture_plural_factory} should exist(?: with #{capture_fields})?$ | | count, plural_factory, fields |
| ^#{capture_model} should be #{capture_model}$ | | a, b |
| ^#{capture_model} should be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$ | | target, owner, association |
| ^#{capture_model} should not be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$ | | target, owner, association |
| ^#{capture_model} should be #{capture_model}(?:'s)? (\w+)$ | | target, owner, association |
| ^#{capture_model} should not be #{capture_model}(?:'s)? (\w+)$ | | target, owner, association |
| ^#{capture_model} should (?:be|have) (?:an? )?#{capture_predicate}$ | | name, predicate |
| ^#{capture_model} should not (?:be|have) (?:an? )?#{capture_predicate}$ | | name, predicate |
| ^#{capture_model}'s (\w+) (should(?: not)?) be #{capture_value}$ | | name, attribute, expectation, expected |
| ^#{capture_model} should have (\d+) (\w+)$ | | name, size, association |
+--------------------------------------------------------------------------------------+----------+----------------------------------------+
16 rows in set
File: features/step_definitions/web_steps.rb
+--------------------------------------------------------------------------+----------+------------------------+
| steps | modifier | args |
+--------------------------------------------------------------------------+----------+------------------------+
| ^(?:|I )am on (.+)$ | | page_name |
| ^(?:|I )go to (.+)$ | | page_name |
| ^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$ | | button, selector |
| ^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$ | | link, selector |
| ^(?:|I )fill in "([^"]*)" with "([^"]*)"(?: within "([^"]*)")?$ | | field, value, selector |
| ^(?:|I )fill in "([^"]*)" for "([^"]*)"(?: within "([^"]*)")?$ | | value, field, selector |
| ^(?:|I )fill in the following(?: within "([^"]*)")?:$ | | selector, fields |
| ^(?:|I )select "([^"]*)" from "([^"]*)"(?: within "([^"]*)")?$ | | value, field, selector |
| ^(?:|I )check "([^"]*)"(?: within "([^"]*)")?$ | | field, selector |
| ^(?:|I )uncheck "([^"]*)"(?: within "([^"]*)")?$ | | field, selector |
| ^(?:|I )choose "([^"]*)"(?: within "([^"]*)")?$ | | field, selector |
| ^(?:|I )attach the file "([^"]*)" to "([^"]*)"(?: within "([^"]*)")?$ | | path, field, selector |
| ^(?:|I )should see JSON:$ | | expected_json |
| ^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$ | | text, selector |
| ^(?:|I )should see \/([^\/]*)\/(?: within "([^"]*)")?$ | | regexp, selector |
| ^(?:|I )should not see "([^"]*)"(?: within "([^"]*)")?$ | | text, selector |
| ^(?:|I )should not see \/([^\/]*)\/(?: within "([^"]*)")?$ | | regexp, selector |
| ^the "([^"]*)" field(?: within "([^"]*)")? should contain "([^"]*)"$ | | field, selector, value |
| ^the "([^"]*)" field(?: within "([^"]*)")? should not contain "([^"]*)"$ | | field, selector, value |
| ^the "([^"]*)" checkbox(?: within "([^"]*)")? should be checked$ | | label, selector |
| ^the "([^"]*)" checkbox(?: within "([^"]*)")? should not be checked$ | | label, selector |
| ^(?:|I )should be on (.+)$ | | page_name |
| ^(?:|I )should have the following query string:$ | | expected_pairs |
| ^show me the page$ | | |
+--------------------------------------------------------------------------+----------+------------------------+
24 rows in set
@SFEley
Copy link

SFEley commented Jun 29, 2011

Found this via Google. In case it helps anyone, I created a fork that also follows 'require' dependencies so that step definitions from gems (Capybara, Aruba, etc.) can be documented as well. https://gist.github.com/1054553

@developer88
Copy link

Created a fork that group by When|Then|Given, but not by filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment