khoamle
khoamle

Reputation: 726

capybara find show page link unable to find path

I'm trying to test the presence of an image link which is a rails show page.

<a href="/entries/1"></a>

I have tried few capybara but doesn't work. And I know the id is present.

Steps code

find_link('/entries/#{@photo_upload_entry.id}').visible?

find_link('/entries/1').visible?

expect(page).to have_link("/entries/#{@photo_upload_entry.id}")

expect(page).to have_link("/entries/1")

Error from cucumber test

Unable to find link "/entries/\#{@photo_upload_entry.id" (Capybara::ElementNotFound)

Unable to find link "/entries/1" (Capybara::ElementNotFound)

expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)

expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)

Upvotes: 0

Views: 2320

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

From the capybara docs - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_link-instance_method - a link can be found by its id or text (it can also actually be found by title attribute or the alt attribute of a contained image - so apparently the docs need updating). If you also want to match the href it can be passed in as an option. You mention in your question that it's an "image link" but your example doesn't show an img element in it. If there is an image element in there and it has an alt attribute (required attribute according to html spec) you could match on that so any of the following would find/determine existence of the element

find_link('alt of contained img')  # will only find if visible so visible? not really needed
find_link('alt of contained img', href: '/entries/1')
expect(page).to have_link('alt value', href: '/entries/1')

if the image you refer to is an icon applied via CSS or something like that, you can pass an empty string for the locator, and pass the href option as the thing to match against. Something like the following depending on whether you want a reference to the link or just to assert it's existence

find_link('', href: '/entries/1')
expect(page).to have_link('', href: '/entries/1')

Upvotes: 1

Related Questions