How to get parent node in Capybara?

Ruby on-RailsRubyCucumberBddCapybara

Ruby on-Rails Problem Overview


I'm working with many jQuery plugins, that often create DOM elements without id or other identification properties, and the only way to get them in Capybara (for clicking for example) - is to get their neighbor (another child of its ancestor) first. But I didn't find anywhere, does Capybara support such things for example:

find('#some_button').parent.fill_in "Name:", :with => name

?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I really found jamuraa's answer helpful, but going for full xpath gave me a nightmare of a string in my case, so I happily made use of the ability to concatenate find's in Capybara, allowing me to mix css and xpath selection. Your example would then look like this:

find('#some_button').find(:xpath,".//..").fill_in "Name:", :with => name

Capybara 2.0 update: find(:xpath,".//..") will most likely result in an Ambiguous match error. In that case, use first(:xpath,".//..") instead.

Solution 2 - Ruby on-Rails

I found the following that does work:

find(:xpath, '..')

Capybara has been updated to support this.

https://github.com/jnicklas/capybara/pull/505

Solution 3 - Ruby on-Rails

There isn't a way to do this with capybara and CSS. I've used XPath in the past to accomplish this goal though, which does have a way to get the parent element and is supported by Capybara:

find(:xpath, '//*[@id="some_button"]/..').fill_in "Name:", :with => name

Solution 4 - Ruby on-Rails

If you stumbled across this trying to figure out how to find any parent (as in ancestor) node (as hinted at in @vrish88's comment on @Pascal Lindelauf's answer):

find('#some_button').find(:xpath, 'ancestor::div[@id="some_div_id"]')

Solution 5 - Ruby on-Rails

This answer pertains to how to manipulate a sibling element which is what I believe the original question is alluding to

Your question hypothesis works with a minor tweak. If the dynamically generated field looks like this and does not have an id:

<div>
  <input></input>
  <button>Test</button>
</div>

Your query would then be:

find('button', text: 'Test').find(:xpath, "..").find('input').set('whatever')

If the dynamically generated input does come attached with an id element (be careful with these though as in angular, they are wont to change based on adding and deleting elements) it would be something like this:

find('button', text: 'Test').find(:xpath, "..").fill_in('#input_1', with: 'whatever')

Hope that helps.

Solution 6 - Ruby on-Rails

I'm using a different approach by finding the parent element first using the text within this parent element:

find("<parent element>", text: "text within your #some_button").fill_in "Name:", with: name

Maybe this is useful in a similiar situation.

Solution 7 - Ruby on-Rails

I needed to find an ancestor with a css class, though it was indeterminate if it the target ancestor had one or more css classes present, so I didn't see a way to make a deterministic xpath query. I worked this up instead:

def find_ancestor_with_class(field, cssClass)
  ancestor = field
  loop do
    ancestor = ancestor.find(:xpath, '..')
    break if ancestor.nil?

    break if ancestor.has_css? cssClass
  end

  ancestor
end

Warning: use this sparingly, it could cost you a lot of time in tests so make sure the ancestor is just a few hops away.

Solution 8 - Ruby on-Rails

As mentioned in comment by @Tyler Rick Capybara in these days have methods[ ancestor(selector) and sibling(selector)

Solution 9 - Ruby on-Rails

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsandrewView Question on Stackoverflow
Solution 1 - Ruby on-RailsPascal LindelaufView Answer on Stackoverflow
Solution 2 - Ruby on-RailsB SevenView Answer on Stackoverflow
Solution 3 - Ruby on-RailsjamuraaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsBen AlaviView Answer on Stackoverflow
Solution 5 - Ruby on-RailsT. SlaterView Answer on Stackoverflow
Solution 6 - Ruby on-RailsHarm de WitView Answer on Stackoverflow
Solution 7 - Ruby on-RailskrossView Answer on Stackoverflow
Solution 8 - Ruby on-RailsFotonView Answer on Stackoverflow
Solution 9 - Ruby on-RailsDmitriy KonovalovView Answer on Stackoverflow