Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on the second link with the same text using Capybara in Rails 3?

In a view file I have:

= link_to 'View', post
= link_to 'View', comment

In a spec file (I'm using Capybara):

click_on 'View'

It clicks on the first link, but I want it to click on the second one. How can I do it?

like image 786
Aleksandr Shvalev Avatar asked Jul 18 '11 13:07

Aleksandr Shvalev


2 Answers

You could try to find all entries and deal with an array:

page.all('a')[1].click

Would help to have a class or use within to scope your search ;)

like image 117
Gabriel F. Engel Avatar answered Nov 20 '22 13:11

Gabriel F. Engel


There's probably a few ways but I usually scope something like this.

within(".comment") do
  click_on("View")
end

There's quite possibly/probably alternatives as well. I usually do my acceptance testing from cucumber, so my steps typically look like

When I follow "View" within the comment element

Where I have a step that translates within the comment element to a scoped call to the step itself (which I think is built into the latest capybara web_steps)

like image 39
Keith Gaddis Avatar answered Nov 20 '22 15:11

Keith Gaddis