Take the 2-minute tour ×

I am using ruby on rails 3.2.3 and capybara to help creating request spec. My goal is to create a spec request that test the log out. The web page:

 <li class="dropdown">
        Welcome <%= current_user.first_name + " "+ current_user.last_name%>
        <b class="caret"></b>
    <ul class="dropdown-menu">
         <a href="#">
        <%= link_to "Sign out", destroy_user_session_path, :method => :delete%> 

For the test, I have

describe "sign out" do
    it "should let user to sign out" do
      login_as user, :scope => :user
      # click_on "Welcome #{user.first_name} #{user.last_name}"
      # Now click on Sign out

I don't know how to click on the Sign out using capybara because it is in a drop down menu, therefore not visible on page. Does anyone know ?

Is there any other way to click on an element in a dropdown menu ?

share|improve this question
I have resolve the same issue (on Twitter Bootstrap button drop down menu) by select the specific link tag: find('a', :text => option_title).click –  Frankovskyi Bogdan Mar 22 '13 at 18:49
add comment

4 Answers

I've tested a dropdown just by clicking both links

click_link "Welcome #{current_user.first_name} #{current_user.last_name}"
click_link 'sub link'
share|improve this answer
How do you click_link "dropdown link". –  qusr Jun 30 '12 at 5:37
Added some code –  DVG Jun 30 '12 at 15:08
that did not work. –  qusr Jun 30 '12 at 16:32
You of course may have to replace the variable current_user with the user object created for your test –  DVG Jun 30 '12 at 21:08
Change it "my example" do to it "my example", js: true doBy default this will launch Firefox and run the example with selenium, so you can see it run. You might add a sleep to the end so you have time to take in what is happening –  DVG Jul 1 '12 at 15:38
show 5 more comments

A much easier solution is simply this:

select("option you want to select from menu", :from => "label name")

NOTE: The "label name" is the actual text of the <label>. However, you can also use the "id" or "name" attribute of the <select> here as well, which I think is better since you may change the label text at some point and if you do, your tests will still pass.

share|improve this answer
"select" does not work for non-html select box elements. Question is about "button drop down menu" (like twitter bootsrap one) which is not html select box. –  Frankovskyi Bogdan Mar 22 '13 at 18:18
This answer actually addresses the title of the question. I guess that's why it has the most votes. –  yuяi Jul 30 '13 at 21:03
The code isn't a select, it's bootstrap's logout code. So select won't work –  Dean Aug 28 '13 at 2:32
add comment
up vote 2 down vote accepted

Hello I figured it out eventually. I had to use xpath to find the link. Based on the html above, here is what I did:

In rspec, I wrote:

page.find(:xpath, "//a[@href='/users/sign_out']").click

The reason I use "//a[@href='/users/sign_out']" is because the link_to "Sign out", destroy_user_session_path, :method => :delete is compiled to <a href="/users/sign_out" ... on the web page.

And it works :-)

Oh by the way, I found this useful link: http://www.suffix.be/blog/click_image_in_link_capybara

share|improve this answer
I've been able to test a very similar dropdown just by doing two clicks, one to open the dropdown and one to click the link. –  DVG Jun 30 '12 at 1:58
That's what I thought too. Can you post some code ? It would be great if that can be applied to the html above. Thanks. –  qusr Jun 30 '12 at 2:32
I believe the missing piece here is enabling Javascript for the request spec; usually capybara-webkit (github.com/thoughtbot/capybara-webkit). Once this is enabled, then the clicks will work as your browser has them work. –  Jesse Wolgamott Jun 30 '12 at 16:55
Hi, this way it actually works (the test is able to click) without enabling js. –  qusr Jul 1 '12 at 16:28
it is giving Capybara::ElementNotFound: ERROR.... –  Mohd Anas Dec 9 '13 at 7:06
show 1 more comment

DVG suggestion works with Selenium, but not with Webkit.

share|improve this answer
add comment

Your Answer


