capybara click_link with :href match

jackpipe picture jackpipe · Feb 19, 2013 · Viewed 33.1k times · Source

I have the following test in a request spec:

page.should have_link('Edit user', :href => edit_users_path(@user))

This checks that a specific user in an index view has an edit link. I would like to click the same link, with something like:

click_link('Edit user', :href => edit_users_path(@user))

Unfortunately click_link doesn't accept options.

Is there a good way to do this, it seems like a fairly obvious use case?

Should I be including an #id in the table <tr> or <td> and doing something like:

within(#user_id)
  click_link 'Edit user'
end

I'd prefer not to tinker with the view just to get the tests to work.

Answer

jvnill picture jvnill · Feb 19, 2013

you can use find find(:xpath, "//a[@href='/foo']").click