Skip to content

Instantly share code, notes, and snippets.

@ianwhite
Created November 20, 2009 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianwhite/239570 to your computer and use it in GitHub Desktop.
Save ianwhite/239570 to your computer and use it in GitHub Desktop.
How to use any cucumber step as a scoped step
# Add selector mappings here, any selector in the format "..." is treated as raw css
#
module SelectorsHelper
include ActionController::RecordIdentifier
def selector_for(selector)
case selector
when /^"(.*)"$/
$1
# example mappings form phrases to selectors
when "my cart"
'div#cart'
# active record
when /^the user "(.*)"$/
'#' + dom_id(User.find_by_login($1))
# pickle
when /^#{capture_model}$/
'#' + dom_id(model!($1))
else
raise "Can't convert #{selector} to a css selector. Use quotes if giving a straight css selector"
end
end
end
World(SelectorsHelper)
# Allow all cucumber steps to be reused as scoped steps, and enable human friendly
# interpretation of selectors.
#
# Found at: http://gist.github.com/gists/239570 - Ian White & Nick Rutherford, Nov 2009
#
# to enable these steps:
#
# the default 'webrat_steps.rb' must
# * have 'response' replaced by 'current_dom'
# * all 'within' steps removed
#
# you should have support/selectors.rb installed
# This allows steps like
#
# When I press "foo" within "div.panel"
# Then I should see "Total: £20" in my cart
#
register_rb_step_definition(/^(.+?) within (.+?)$/) do |step, selector|
within(selector_for(selector)) { __cucumber_invoke step }
end
# This allows steps like
#
# When within the product's panel:
# | I fill in "Price" with "$100" |
# | I press "Submit" |
#
register_rb_step_definition(/^within (.+?):?$/) do |selector, table|
steps = table.raw.flatten
within(selector_for(selector)) { steps.each {|step| __cucumber_invoke step } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment