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
.
This is a companion discussion topic for the original entry at http://www.railsonmaui.com//tips/rails/capybara-phantomjs-poltergeist-rspec-rails-tips.html