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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)