Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.closest() in Capybara

I'd like to find closest parent of an html element in cucumber. just like the .closest() function of jQuery does.

this is my (pseudo) code:

  aspect = find('.dropdown li:contains('+selector+')')
  dropdown = aspect.closest('.dropdown') #<-- the closest() function does not exist

  if not aspect.hasClass('.selected')
    dropdown.click
    sleep 1
    aspect.click
  end

can anybody tell me how to accomplish this using Capybara?

Cheers!

Manuel

like image 504
manuels Avatar asked Aug 10 '11 20:08

manuels


1 Answers

This is not a universal solution, but if all you want to do is click the element, I would suggest using jQuery directly:

page.execute_script('$(...).closest(...).click()')

Other than that, Capybara does not have a .closest method, but in many cases, being more creative with selectors (possibly using XPath) might do the trick.

like image 58
Jo Liss Avatar answered Oct 09 '22 10:10

Jo Liss