Code

Finding Elements for Capybara Page Objects using XPath

Capybara Page Objects make finding elements really easy. But if none of the other finders are working for you then you may find yourself attempting to use the dreaded XPath option for drilling down to the right element.

Luckily, there are a lot of tools out there just for finding the XPath, this is not something that you will want to try and do by hand. Unless, you are hard-core, then totally do it by hand.

I recommend the XPath Helper tool in the Chrome webstore as the right plugin for the job. After attempting several other plugins for giving me the xpath in a super simple manner this tool made it so easy.

  1. def set_cascade_column column_name, selection
  2.         source.find(:xpath, “//div[@class=’step #{column_name}’]/ul[@class=’items’]/li/span[text()=’#{selection}’]”).click
  3. end

Now I can wrap my ugly, ugly, terrible xpath in a function within a CapybaraPageObject::Component and hide it where I don’t have to look at it.