Skip to content

Instantly share code, notes, and snippets.

@mattscilipoti
Created June 15, 2011 12:55
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 mattscilipoti/1027017 to your computer and use it in GitHub Desktop.
Save mattscilipoti/1027017 to your computer and use it in GitHub Desktop.
Selector-Free Cucumber Scenarios (from: http://bjeanes.com/2010/09/19/selector-free-cucumber-scenarios)
# I'm in features/support/selectors.rb
module HtmlSelectorsHelper
def selector_for(scope)
case scope
when /the body/
"html > body"
else
begin
scope =~ /the (.*)/
components = $1.split(/\s+/)
selector = "//*[@id='#{components.join('_')}']"
page.find(:xpath, selector)
selector
rescue Object => e
raise "Can't find mapping from \"#{scope}\" to a selector.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
end
end
World(HtmlSelectorsHelper)
# I'm in features/step_definitions/web_ext_steps.rb
When /^(.*) within ([^:"]+)$/ do |step, scope|
with_scope(selector_for(scope)) do
When step
end
end
# Multi-line version of above
When /^(.*) within ([^:"]+):$/ do |step, scope, table_or_string|
with_scope(selector_for(scope)) do
When "#{step}:", table_or_string
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment