Capybara, PhantomJs, Poltergeist, and Rspec Tips

Sometimes a Capybara test requires an xpath for more advanced finding of just the right dom node. For example, here’s a useful xpath snippet for finding the xpath to the page one link.

1 page_1 = find(:xpath, '//div[contains(@class,"pagination")]//a[normalize-space(.)="1"]')

If you use a context (“within”), then be sure to use “.//” and not ”/” as “/” means anywhere on the page, not just in the current context!

Another case where I had to use an xpath was to find the parent of a node. While the Capybara node object has a method parent, that does not give you the same sort of dome node parent that jQuery would. Thus, you can use an xpath like this, which finds an anchor with attribute data-something having value in ruby variable data_value.

1 the_node = find(:xpath, "//a[@data-something='#{data_value}']/..")

This is a companion discussion topic for the original entry at

Thanks for all the detail! Great post, would like to see more blog posts like this that go into more detail on developer toolchains.

Great post mate, thanks for pointing out the hover API

Thanks for all the awesome detail! Lots of great stuff in here

Love your posts, you're my hero (telecomuting from Maui! If you need a mountain fix, come stay at my Cabin in Montana). For anyone running simplecov with zeus and parallel_tests, you may need this custom_plan:

Thanks for the notes! Been slammed with consulting, but need to get back to some more posts I've been planning on writing.