Skip to content

Instantly share code, notes, and snippets.

@ianwhite
Created November 24, 2008 04:19
Show Gist options
  • Save ianwhite/28376 to your computer and use it in GitHub Desktop.
Save ianwhite/28376 to your computer and use it in GitHub Desktop.
auto generate scoped versions of your webrat steps
# Stick this in features/step_definitions/scoped_webrat_steps.rb
#
# If your webrat steps defines:
#
# When /^I press "(.*)"$/ do |link|
# clicks_link(link)
# end
#
# Then this will also define:
#
# When /^within: (.*?), I press "(.*)"$/ |scoped, link|
# within(scope) {|s| s.clicks_link(link)}
# end
#
# When means you can do stuff like this:
#
# Scenario: click the 'home' in the crumbs bar
# When within: div#crumbs, I press "home"
# ...
#
__DIR__ = File.expand_path(File.dirname(__FILE__)) # thanks lachie
webrat_steps = File.read(File.join(__DIR__, 'webrat_steps.rb'))
within_steps = "# generated by parsing webrat_steps.rb\n"
webrat_steps.scan(/\nWhen \/\^(.*?)\$\/ do \|(.*?)\|\n\s+(.+?)\s*\nend\n/m) do
within_steps << <<-eod
When /^within: (.*?), #{$1}$/ do |scope, #{$2}|
within(scope) {|s| s.#{$3}}
end
eod
end
eval within_steps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment