Created
June 15, 2011 12:55
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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