Read more

Finding ancestors with Capybara

Michael Leimstädtner
October 20, 2021Software engineer at makandra GmbH

Modern versions of Capybara include a finder method #ancestor which allows you to find a parental element using CSS or XPath.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

If you previously did something like this:

field.find(:xpath, './ancestor::div[contains(@class, "form-group")]')

..and prefer CSS, you may rewrite it:

field.ancestor('div.form-group')

Both versions will return the outermost matching element. Use the #order option find the closest parent:

field.ancestor('div.form-group', order: :reverse)
Posted by Michael Leimstädtner to makandra dev (2021-10-20 09:35)