← Back to Upcase

Finding a link to click with capybara in feature spec


(Jon Seidel) #1

I’m having trouble getting capybara’s find method to locate what (I think) I know is on the page:

Spec snippet

 scenario "edits an existing entry" do
    visit "/manage"
    click_on 'View Featured Companies'
    expect(page).to have_content 'Featured Company'
    expect(page).to have_content 'Featured News Article'
    expect(page).to_not have_content "No Featured Companies"
    expect(page).to_not have_content 'Edit Featured Company'
    save_and_open_page
    find('td').find('a[href*="edit"]').click  

I’ve also tried

find('a[href$="edit"]')

and several other variations withe the same result or with a “Can’t find it” error message

HTML snippet from save_and_open_page

<td>
<a href="/manage/featured_companies/1/edit"><img alt="Icn_edit" src="/manage/images/icn_edit.png" title="Edit" /></a>
<a href="/manage/featured_companies/1" data-confirm="Are you sure? This is permanent." data-method="delete" rel="nofollow"><img alt="Icn_trash" src="/manage/images/icn_trash.png" title="Destroy" /></a>

RSpec failure message

FeaturedCompanies
  edits an existing entry (FAILED - 1)

Failures:

  1) FeaturedCompanies edits an existing entry
     Failure/Error: save_and_open_page
     NoMethodError:
       undefined method `-@' for #<Thread:0x00000004ac0428 sleep>
     # ./spec/features/edit_featured_companies_spec.rb:29:in `block (2 levels) in <top (required)>'

Edit. I found this error; there was an extra ‘-’ on one line
OR this if I remove the save_and_open_page

  1) FeaturedCompanies edits an existing entry
     Failure/Error: find('td').find('a[href*="edit"]').click
     Capybara::ElementNotFound:
       Unable to find css "a[href*=\"edit\"]"
     # ./spec/features/edit_featured_companies_spec.rb:30:in `block (2 levels) in <top (required)>'

Edit. Have to use the following:

within('tablesorter') do    
  find('a[href=$="edit"]').click
end

Getting a “Can’t find it” error message was strange enough, but what is this thing about a ‘-@’ method mean?


(Radoslav Stankov) #2

I wouldn’t search by url. Capybara can search by link title. In your code you have title="Edit" which will do the job:

click_on 'Edit'

If you still need to be scoped by td you can even do

within('td') do
  click_on 'Edit'
end

(Jon Seidel) #3

Thanks, @rstankov; I also thought those would work, but

click_on 'Edit'

returns

Unable to find link or button "Edit"

and

find('td').find('a[title="Edit"]').click

returns

Unable to find css "a[title=\"Edit\"]"

(Radoslav Stankov) #4

Are you sure the element is present and visible? In the past I had problems with button who where hidden and capybara not finding them.


(Jon Seidel) #5

About as sure as I can be; I do a save_and_open_page just before the click_on or find and I see this:

Short news title
</td>
<td>
<a href="/manage/featured_companies/1/edit"><img alt="Icn_edit" src="/manage/images/icn_edit.png" title="Edit" /></a>
<a href="/manage/featured_companies/1" data-confirm="Are you sure? This is permanent." data-method="delete" rel="nofollow"><img alt="Icn_trash" src="/manage/images/icn_trash.png" title="Destroy" /></a>

(Jon Seidel) #6

Found the problem… The title=‘Edit’ is in the img tag inside the a tag; therefore I had to use

within('tablesorter') do    
  find('a[href=$="edit"]').click
end

to get the thing to work (‘tablesorter’ is the class of the table in which the edit link is defined). Note that

find('td').find('a[href$="edit"]').click

doesn’t work