Adding support for getting an element's parent #505

Closed
wants to merge 1 commit into
from

Projects

None yet

10 participants

andyw8 commented Sep 24, 2011

It seemed Capybara lacked a simple way to get an element's parent ( http://stackoverflow.com/questions/4861863/how-to-get-parent-node-in-capybara ) so I've added support for this.

Collaborator

Just do find(:xpath, '..'). No need to expose these kinds of traversal methods in Capybara.

@jnicklas jnicklas closed this Nov 15, 2011
cj commented Mar 7, 2012

@jnicklas is there a way to do that but lazy find the body? I'm trying to get back to the body from inside a within, so that I can check it loaded a popup....

cj commented Mar 7, 2012

@jnicklas looks like find(:xpath, '//body') works :D

dgm commented Jun 24, 2012

How is this not a useful addition? Not everyone is comfortable with xpath... I'd prefer to have a jquery like navigation available. Even more useful would be something like http://api.jquery.com/closest/

Sorry for dragging an old issue up.

I don't agree with jnicklas. capybaras purpose is to allow us to test against a page, which may include finding an element then traversing to another element to check something. It may be slightly less common than click "somebutton" but its still valuable.

dfl commented Oct 9, 2012

+1 I would like this feature too, even if it's just syntactic sugar

dfl commented on f0b88ea Oct 9, 2012

this doesn't work if you chain it... seems like you can't call subsequent finds on the returned node.
NoMethodError: undefined method `[]' for #Capybara::Document

Collaborator

When the hell did we merge this? This is definitely, definitely not the right way to implement that method, no idea how that got in there.

Collaborator

I don't think we ever merged it. It's part of #505, and GitHub's UI is just confusing.

Collaborator
jnicklas commented Oct 9, 2012

I'm not against this feature, though it is just syntactic sugar, so I find it a bit redundant. This pull request though is just completely wrong. So if someone wants this, please send a better pull request.

janko-m commented Apr 7, 2015

What does Capybara::Node::Base#parent attribute currently do? It seems to be always returning Capybara::Document instead of the actual tag's parent.

Collaborator
twalpole commented Apr 7, 2015

@janko-m Its the context for the find which located the current element.

Collaborator
abotalov commented Apr 7, 2015

@janko-m Base#parent is internally used to get an ancestor using which the current element was located. For example in:

el = find('a')
el.find('b').parent # => returns el
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment