Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent node in Capybara?

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 

?

like image 276
sandrew Avatar asked Feb 01 '11 11:02

sandrew


2 Answers

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.

like image 123
Pascal Lindelauf Avatar answered Sep 18 '22 06:09

Pascal Lindelauf


I found the following that does work:

find(:xpath, '..') 

Capybara has been updated to support this.

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

like image 33
B Seven Avatar answered Sep 20 '22 06:09

B Seven