Skip to content

Instantly share code, notes, and snippets.

@samullen
Created December 26, 2011 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samullen/1521583 to your computer and use it in GitHub Desktop.
Save samullen/1521583 to your computer and use it in GitHub Desktop.
User defined finders for Capybara
Capybara.add_selector(:link) do
xpath {|rel| ".//a[contains(@rel, '#{rel}')]"}
end
<a href="javascript:void(0);" rel="toggle-chart">Toggle chart</a>
When /^I toggle the charts$/ do
page.find(:link, "toggle-chart").click
end
@samullen
Copy link
Author

@shinzui offered up via Twitter the "contains" option from XPath. Using that method allows you to have multiple values set in the "rel" attribute (e.g. rel="foo bar baz"). You can then do find(:link, "foo") and not have to match all the values in the "rel" attribute. You can find other XPath functions here: http://www.w3.org/TR/xpath/#corelib

Another note: If you are up to date on Capybara, you can use css matchers instead and avoid all this mess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment