Skip to content

Instantly share code, notes, and snippets.

@ches
Created September 12, 2011 05:27
Show Gist options
  • Save ches/1210639 to your computer and use it in GitHub Desktop.
Save ches/1210639 to your computer and use it in GitHub Desktop.
Capybara selector/Cucumber step to click any element with given text
# Brandon Keepers' "click on any element with given text" Capybara selector/Cucumber
# step pairing, adapted for Capybara 1.0+.
# http://collectiveidea.com/blog/archives/2010/08/03/clicking-any-element-with-cucumber-and-capybara/
Capybara.add_selector(:element) do
xpath { |locator| "//*[normalize-space(text())=#{XPath::Expression::StringLiteral.new(locator)}]" }
end
When 'I click "$locator"' do |locator|
msg = "No element found with the content of '#{locator}'"
find(:element, locator, :message => msg).click
end
# Or a possible simplified alternative:
When 'I click "$locator"' do |locator|
find(:xpath, XPath::HTML.content(locator)).click
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment