Capybara: Select or click on a link in a drop-down list

advertisements

I have been looking for the answer but could't find any answer. Maybe I am missing to understand some fundamental concept of capybara.

I have a dropdown-menu, by use of "ul"

<ul class="dropdown-menu">
  <li id="dropdown-list">
    <span>myList</span>
    <ul>
      <li><%= link_to 'Link1', controller: 'link_1' %> </li>
      <li><%= link_to 'Link2', controller: 'link_2' %> </li>
    </ul>
  </li>
  <li id="a_different_dropdown">
    <span>A different dropdown list</span>
  </li>
</ul>

I want to click on Link2. The only thing I've been able to do is:

find('.dropdown-menu', :text => 'myList')

Conceptually: when I have a dropdown menu, do I have to split the process in many steps? I mean, do I have to click in the option myList to make sub-options visible?

I tried click, click_link and select methods but I was unable to open Link2 as the error was often:

Capybara::ElementNotFound:Unable to find link Link2


You probably have to click the dropdown list first. Items in the dropdown menu are usually not visible.

find('#dropdown-list').click

Then you could inspect the options in the list and see what's there.

I'm not really sure what your HTML source looks like, but a lot of the time selecting an option from a dropdown can be done in one line:

find('#dropdown-list option', :text => 'foo').click