How to use capybara in rspec to click on a dropdown option

capybara, logout, rspec, ruby-on-rails, testing

Solution

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

Problem

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"> <a class="dropdown-toggle" data-toggle="dropdown"> Welcome <%= current_user.first_name + " "+ current_user.last_name%> <b class="caret"></b> </a> <ul class="dropdown-menu"> <a href="#"> <%= link_to "Sign out", destroy_user_session_path, :method => :delete%> </a> </ul> </li> ``` 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 end end ``` 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 ?

Original source